4 from typing import NamedTuple
8 import speech_recognition as sr # type: ignore
12 logger = logging.getLogger(__name__)
14 parser = config.add_commandline_args(
15 f"Google Assistant ({__file__})",
16 "Args related to contacting the Google Assistant",
19 "--google_assistant_bridge",
21 default="http://kiosk.house:3000",
23 help="How to contact the Google Assistant bridge"
26 "--google_assistant_username",
28 metavar="GOOGLE_ACCOUNT",
29 default="scott.gasch",
30 help="The user account for talking to Google Assistant"
34 class GoogleResponse(NamedTuple):
38 audio_transcription: str
42 success: {self.success}
43 response: {self.response}
44 audio_transcription: {self.audio_transcription}
45 audio_url: {self.audio_url}"""
48 def tell_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
49 """Alias for ask_google."""
50 return ask_google(cmd, recognize_speech=recognize_speech)
53 def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
54 """Send a command string to Google via the google_assistant_bridge as the
55 user google_assistant_username and return the response. If recognize_speech
56 is True, perform speech recognition on the audio response from Google so as
57 to translate it into text (best effort, YMMV).
59 logging.debug(f"Asking google: '{cmd}'")
62 "user": config.config['google_assistant_username'],
64 url = f"{config.config['google_assistant_bridge']}/assistant"
65 r = requests.post(url, json=payload)
69 audio_transcription = ""
70 if r.status_code == 200:
72 success = bool(j["success"])
73 response = j["response"] if success else j["error"]
75 logger.debug('Google request succeeded.')
77 logger.debug(f"Google said: '{response}'")
78 audio = f"{config.config['google_assistant_bridge']}{j['audio']}"
80 recognizer = sr.Recognizer()
81 r = requests.get(audio)
82 if r.status_code == 200:
84 speech = sr.AudioData(
90 audio_transcription = recognizer.recognize_google(
93 logger.debug(f"Transcription: '{audio_transcription}'")
94 except sr.UnknownValueError as e:
96 logger.warning('Unable to parse Google assistant\'s response.')
97 audio_transcription = None
98 return GoogleResponse(
102 audio_transcription=audio_transcription,
105 message = f'HTTP request to {url} with {payload} failed; code {r.status_code}'
106 logger.error(message)
107 return GoogleResponse(
111 audio_transcription=audio_transcription,