4 from typing import NamedTuple
7 import speech_recognition as sr # type: ignore
11 logger = logging.getLogger(__name__)
13 parser = config.add_commandline_args(
14 f"Google Assistant ({__file__})",
15 "Args related to contacting the Google Assistant",
18 "--google_assistant_bridge",
20 default="http://kiosk.house:3000",
22 help="How to contact the Google Assistant bridge"
25 "--google_assistant_username",
27 metavar="GOOGLE_ACCOUNT",
28 default="scott.gasch",
29 help="The user account for talking to Google Assistant"
33 class GoogleResponse(NamedTuple):
37 audio_transcription: str
41 success: {self.success}
42 response: {self.response}
43 audio_transcription: {self.audio_transcription}
44 audio_url: {self.audio_url}"""
47 def tell_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
48 """Alias for ask_google."""
49 return ask_google(cmd, recognize_speech=recognize_speech)
52 def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
53 """Send a command string to Google via the google_assistant_bridge as the
54 user google_assistant_username and return the response. If recognize_speech
55 is True, perform speech recognition on the audio response from Google so as
56 to translate it into text (best effort, YMMV).
58 logging.debug(f"Asking google: '{cmd}'")
61 "user": config.config['google_assistant_username'],
63 url = f"{config.config['google_assistant_bridge']}/assistant"
64 r = requests.post(url, json=payload)
68 audio_transcription = ""
69 if r.status_code == 200:
71 success = bool(j["success"])
72 response = j["response"] if success else j["error"]
74 logger.debug('Google request succeeded.')
76 logger.debug(f"Google said: '{response}'")
77 audio = f"{config.config['google_assistant_bridge']}{j['audio']}"
79 recognizer = sr.Recognizer()
80 r = requests.get(audio)
81 if r.status_code == 200:
83 speech = sr.AudioData(
89 audio_transcription = recognizer.recognize_google(
92 logger.debug(f"Transcription: '{audio_transcription}'")
93 except sr.UnknownValueError as e:
95 logger.warning('Unable to parse Google assistant\'s response.')
96 audio_transcription = None
99 f'HTTP request to {url} with {payload} failed; code {r.status_code}'
101 return GoogleResponse(
105 audio_transcription=audio_transcription,