#!/usr/bin/env python3
+# © Copyright 2021-2022, Scott Gasch
+
"""A module to serve as a local client library around HTTP calls to
-the Google Assistant via a local gateway."""
+the Google Assistant via a local gateway.
+"""
import logging
import warnings
-from typing import NamedTuple, Optional
+from dataclasses import dataclass
+from typing import Optional
import requests
import speech_recognition as sr # type: ignore
)
-class GoogleResponse(NamedTuple):
- """A response wrapper."""
+@dataclass
+class GoogleResponse:
+ """A Google response wrapper dataclass."""
+
+ success: bool = False
+ """Did the request succeed (True) or fail (False)?"""
+
+ response: str = ''
+ """The response as a text string, if available."""
- success: bool
- response: str
- audio_url: str
- audio_transcription: Optional[str] # None if not available.
+ audio_url: str = ''
+ """A URL that can be used to fetch the raw audio response."""
+
+ audio_transcription: Optional[str] = None
+ """A transcription of the audio response, if available. Otherwise
+ None"""
def __repr__(self):
return f"""
def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
- """Send a command string to Google via the google_assistant_bridge as the
- user google_assistant_username and return the response. If recognize_speech
- is True, perform speech recognition on the audio response from Google so as
- to translate it into text (best effort, YMMV).
+ """Send a command string to Google via the google_assistant_bridge as
+ the user google_assistant_username and return the response. If
+ recognize_speech is True, perform speech recognition on the audio
+ response from Google so as to translate it into text (best effort,
+ YMMV). e.g.::
+
+ >>> google_assistant.ask_google('What time is it?')
+ success: True
+ response: 9:27 PM.
+ audio_transcription: 9:27 p.m.
+ audio_url: http://kiosk.house:3000/server/audio?v=1653971233030
+
"""
logging.debug("Asking google: '%s'", cmd)
payload = {