Stop assistant interaction if a phrase match is found
This commit is contained in:
parent
b63843a130
commit
6769707580
3 changed files with 42 additions and 20 deletions
|
@ -10,18 +10,21 @@ logger = logging.getLogger(__name__)
|
||||||
class AssistantEvent(Event):
|
class AssistantEvent(Event):
|
||||||
""" Base class for assistant events """
|
""" Base class for assistant events """
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, assistant=None, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
try:
|
|
||||||
|
if assistant:
|
||||||
|
self._assistant = assistant
|
||||||
|
else:
|
||||||
self._assistant = get_backend('assistant.google')
|
self._assistant = get_backend('assistant.google')
|
||||||
except KeyError:
|
|
||||||
try:
|
if not self._assistant:
|
||||||
self._assistant = get_plugin('assistant.google.pushtotalk')
|
self._assistant = get_plugin('assistant.google.pushtotalk')
|
||||||
except Exception:
|
|
||||||
|
if not self._assistant:
|
||||||
logger.warning('google.assistant not configured/initialized')
|
logger.warning('google.assistant not configured/initialized')
|
||||||
self._assistant = None
|
self._assistant = None
|
||||||
|
|
||||||
|
|
||||||
class ConversationStartEvent(AssistantEvent):
|
class ConversationStartEvent(AssistantEvent):
|
||||||
"""
|
"""
|
||||||
Event triggered when a new conversation starts
|
Event triggered when a new conversation starts
|
||||||
|
@ -82,15 +85,18 @@ class SpeechRecognizedEvent(AssistantEvent):
|
||||||
Event triggered when a speech is recognized
|
Event triggered when a speech is recognized
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, phrase, *args, **kwargs):
|
def __init__(self, phrase, *args, assistant=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
:param phrase: Recognized user phrase
|
:param phrase: Recognized user phrase
|
||||||
:type phrase: str
|
:type phrase: str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
super().__init__(phrase=phrase, *args, **kwargs)
|
super().__init__(*args, phrase=phrase, assistant=assistant, **kwargs)
|
||||||
self.recognized_phrase = phrase.strip().lower()
|
self.recognized_phrase = phrase.strip().lower()
|
||||||
|
|
||||||
|
if not self._assistant and assistant:
|
||||||
|
self._assistant = assistant
|
||||||
|
|
||||||
def matches_condition(self, condition):
|
def matches_condition(self, condition):
|
||||||
"""
|
"""
|
||||||
Overrides matches condition, and stops the conversation to prevent the
|
Overrides matches condition, and stops the conversation to prevent the
|
||||||
|
@ -98,8 +104,12 @@ class SpeechRecognizedEvent(AssistantEvent):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
result = super().matches_condition(condition)
|
result = super().matches_condition(condition)
|
||||||
|
|
||||||
if result.is_match and self._assistant and 'phrase' in condition.args:
|
if result.is_match and self._assistant and 'phrase' in condition.args:
|
||||||
self._assistant.stop_conversation()
|
if hasattr(self._assistant, 'play_response'):
|
||||||
|
self._assistant.play_response = False
|
||||||
|
elif hasattr(self._assistant, 'stop_conversation'):
|
||||||
|
self._assistant.stop_conversation()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -182,4 +192,3 @@ class TimerEndEvent(AlertEndEvent):
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,7 @@ class SampleAssistant(object):
|
||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
self.conversation_stream = conversation_stream
|
self.conversation_stream = conversation_stream
|
||||||
self.display = display
|
self.display = display
|
||||||
|
self.play_response = True
|
||||||
|
|
||||||
# Opaque blob provided in AssistResponse that,
|
# Opaque blob provided in AssistResponse that,
|
||||||
# when provided in a follow-up AssistRequest,
|
# when provided in a follow-up AssistRequest,
|
||||||
|
@ -131,6 +132,7 @@ class SampleAssistant(object):
|
||||||
logging.info('End of audio request detected.')
|
logging.info('End of audio request detected.')
|
||||||
logging.info('Stopping recording.')
|
logging.info('Stopping recording.')
|
||||||
self.conversation_stream.stop_recording()
|
self.conversation_stream.stop_recording()
|
||||||
|
self.play_response = True
|
||||||
|
|
||||||
if self.detected_speech and self.on_speech_recognized:
|
if self.detected_speech and self.on_speech_recognized:
|
||||||
self.on_speech_recognized(self.detected_speech)
|
self.on_speech_recognized(self.detected_speech)
|
||||||
|
@ -144,9 +146,13 @@ class SampleAssistant(object):
|
||||||
if len(resp.audio_out.audio_data) > 0:
|
if len(resp.audio_out.audio_data) > 0:
|
||||||
if not self.conversation_stream.playing:
|
if not self.conversation_stream.playing:
|
||||||
self.conversation_stream.stop_recording()
|
self.conversation_stream.stop_recording()
|
||||||
self.conversation_stream.start_playback()
|
|
||||||
logging.info('Playing assistant response.')
|
if self.play_response:
|
||||||
self.conversation_stream.write(resp.audio_out.audio_data)
|
self.conversation_stream.start_playback()
|
||||||
|
logging.info('Playing assistant response.')
|
||||||
|
|
||||||
|
if self.play_response:
|
||||||
|
self.conversation_stream.write(resp.audio_out.audio_data)
|
||||||
if resp.dialog_state_out.conversation_state:
|
if resp.dialog_state_out.conversation_state:
|
||||||
conversation_state = resp.dialog_state_out.conversation_state
|
conversation_state = resp.dialog_state_out.conversation_state
|
||||||
logging.debug('Updating conversation state.')
|
logging.debug('Updating conversation state.')
|
||||||
|
|
|
@ -146,21 +146,21 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
||||||
def on_conversation_start(self):
|
def on_conversation_start(self):
|
||||||
""" Conversation start handler """
|
""" Conversation start handler """
|
||||||
def handler():
|
def handler():
|
||||||
get_bus().post(ConversationStartEvent())
|
get_bus().post(ConversationStartEvent(assistant=self.assistant))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
def on_conversation_end(self):
|
def on_conversation_end(self):
|
||||||
""" Conversation end handler """
|
""" Conversation end handler """
|
||||||
def handler(with_follow_on_turn):
|
def handler(with_follow_on_turn):
|
||||||
get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn))
|
get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn, assistant=self.assistant))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
def on_speech_recognized(self):
|
def on_speech_recognized(self):
|
||||||
""" Speech recognized handler """
|
""" Speech recognized handler """
|
||||||
def handler(phrase):
|
def handler(phrase):
|
||||||
get_bus().post(SpeechRecognizedEvent(phrase=phrase))
|
get_bus().post(SpeechRecognizedEvent(phrase=phrase, assistant=self.assistant))
|
||||||
self.interactions.append({'request': phrase})
|
self.interactions.append({'request': phrase})
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
@ -168,14 +168,14 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
||||||
def on_volume_changed(self):
|
def on_volume_changed(self):
|
||||||
""" Volume changed event """
|
""" Volume changed event """
|
||||||
def handler(volume):
|
def handler(volume):
|
||||||
get_bus().post(VolumeChangedEvent(volume=volume))
|
get_bus().post(VolumeChangedEvent(volume=volume, assistant=self.assistant))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
def on_response(self):
|
def on_response(self):
|
||||||
""" Response handler """
|
""" Response handler """
|
||||||
def handler(response):
|
def handler(response):
|
||||||
get_bus().post(ResponseEvent(response_text=response))
|
get_bus().post(ResponseEvent(response_text=response, assistant=self.assistant))
|
||||||
|
|
||||||
if not self.interactions:
|
if not self.interactions:
|
||||||
self.interactions.append({'response': response})
|
self.interactions.append({'response': response})
|
||||||
|
@ -184,6 +184,13 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
|
def on_device_action(self):
|
||||||
|
""" Device action handler """
|
||||||
|
def handler(request):
|
||||||
|
self.logger.info('Received request: {}'.format(request))
|
||||||
|
|
||||||
|
return handler
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def start_conversation(self, language=None):
|
def start_conversation(self, language=None):
|
||||||
"""
|
"""
|
||||||
|
@ -219,7 +226,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
||||||
display=None,
|
display=None,
|
||||||
channel=self.grpc_channel,
|
channel=self.grpc_channel,
|
||||||
deadline_sec=self.grpc_deadline,
|
deadline_sec=self.grpc_deadline,
|
||||||
device_handler=None,
|
device_handler=self.on_device_action(),
|
||||||
on_conversation_start=self.on_conversation_start(),
|
on_conversation_start=self.on_conversation_start(),
|
||||||
on_conversation_end=self.on_conversation_end(),
|
on_conversation_end=self.on_conversation_end(),
|
||||||
on_volume_changed=self.on_volume_changed(),
|
on_volume_changed=self.on_volume_changed(),
|
||||||
|
@ -237,7 +244,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
||||||
""" Stop a conversation """
|
""" Stop a conversation """
|
||||||
if self.assistant:
|
if self.assistant:
|
||||||
self.conversation_stream.stop_playback()
|
self.conversation_stream.stop_playback()
|
||||||
get_bus().post(ConversationEndEvent())
|
get_bus().post(ConversationEndEvent(assistant=self.assistant))
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue