platypush/platypush/message/event/assistant/__init__.py
Fabio Manganiello a14d6fe652 - Stop assistant playback only if there was a user request. This
prevents a PortAudio write on input-only stream error in the Assistant
SDK, see https://github.com/googlesamples/assistant-sdk-python/issues/185

- Make sure that user_request is always defined before returning it

- Use assistant.google.pushtotalk as a fallback if assistant.google is
not configured/available
2018-03-21 23:21:41 +01:00

52 lines
1.5 KiB
Python

import logging
import re
from platypush.context import get_backend
from platypush.message.event import Event, EventMatchResult
class AssistantEvent(Event):
""" Base class for assistant events """
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
self._assistant = get_backend('assistant.google')
except KeyError as e:
try:
self._assistant = get_backend('assistant.google.pushtotalk')
except KeyError as e:
logging.warning('google.assistant backend not configured/initialized')
self._assistant = None
class ConversationStartEvent(AssistantEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class ConversationEndEvent(AssistantEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class SpeechRecognizedEvent(AssistantEvent):
def __init__(self, phrase, *args, **kwargs):
super().__init__(phrase=phrase, *args, **kwargs)
self.recognized_phrase = phrase.strip().lower()
def matches_condition(self, condition):
result = super().matches_condition(condition)
if result.is_match and self._assistant and 'phrase' in condition.args:
self._assistant.stop_conversation()
return result
class HotwordDetectedEvent(AssistantEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# vim:sw=4:ts=4:et: