forked from platypush/platypush
Assistant v.0.1
This commit is contained in:
parent
3a9afd1fed
commit
1ea8badd59
2 changed files with 27 additions and 7 deletions
0
platypush/backend/assistant/__init__.py
Normal file
0
platypush/backend/assistant/__init__.py
Normal file
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import google.oauth2.credentials
|
import google.oauth2.credentials
|
||||||
|
@ -17,26 +18,45 @@ class AssistantGoogleBackend(Backend):
|
||||||
|
|
||||||
def __init__(self, credentials_file=os.path.join(
|
def __init__(self, credentials_file=os.path.join(
|
||||||
os.path.expanduser('~/.config'),
|
os.path.expanduser('~/.config'),
|
||||||
'google-oauthlib-tool', 'credentials.json') , **kwargs):
|
'google-oauthlib-tool', 'credentials.json'),
|
||||||
|
on_conversation_start=None, on_conversation_end=None, **kwargs):
|
||||||
""" Params:
|
""" Params:
|
||||||
credentials_file -- Path to the Google OAuth credentials file
|
credentials_file -- Path to the Google OAuth credentials file
|
||||||
(default: ~/.config/google-oauthlib-tool/credentials.json) """
|
(default: ~/.config/google-oauthlib-tool/credentials.json)
|
||||||
|
on_conversation_start: Custom shell command to execute when a
|
||||||
|
conversation starts (default: none)
|
||||||
|
on_conversation_end: Custom shell command to execute when a
|
||||||
|
conversation ends (default: none)
|
||||||
|
"""
|
||||||
|
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.credentials_file = credentials_file
|
self.credentials_file = credentials_file
|
||||||
|
self.on_conversation_start = on_conversation_start
|
||||||
|
self.on_conversation_end = on_conversation_end
|
||||||
|
|
||||||
with open(args.credentials, 'r') as f:
|
with open(self.credentials_file, 'r') as f:
|
||||||
self.credentials = google.oauth2.credentials.Credentials(token=None,
|
self.credentials = google.oauth2.credentials.Credentials(token=None,
|
||||||
**json.load(f))
|
**json.load(f))
|
||||||
|
|
||||||
self.assistant = None
|
self.assistant = None
|
||||||
|
|
||||||
def _process_event(self, event):
|
def _process_event(self, event):
|
||||||
logging.info('Received assistant event: {}'.format(event))
|
logging.debug('Received assistant event: {}'.format(event))
|
||||||
# self.on_message(event)
|
|
||||||
|
if event.type == EventType.ON_CONVERSATION_TURN_STARTED and self.on_conversation_start:
|
||||||
|
subprocess.check_output(self.on_conversation_start,
|
||||||
|
stderr=subprocess.STDOUT, shell=True)
|
||||||
|
elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED and self.on_conversation_end:
|
||||||
|
subprocess.check_output(self.on_conversation_end,
|
||||||
|
stderr=subprocess.STDOUT, shell=True)
|
||||||
|
elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
|
||||||
|
phrase = event.args['text'].lower().strip()
|
||||||
|
logging.info('Speech recognized: {}'.format(phrase))
|
||||||
|
# self.on_message(event)
|
||||||
|
|
||||||
def send_message(self, msg):
|
def send_message(self, msg):
|
||||||
raise NotImplementedError("Cannot send messages on an event source")
|
# Cant' send a message on an event source, ignoring
|
||||||
|
pass
|
||||||
|
|
||||||
def on_stop(self):
|
def on_stop(self):
|
||||||
if self.producer:
|
if self.producer:
|
||||||
|
@ -50,7 +70,7 @@ class AssistantGoogleBackend(Backend):
|
||||||
super().run()
|
super().run()
|
||||||
|
|
||||||
with Assistant(self.credentials) as self.assistant:
|
with Assistant(self.credentials) as self.assistant:
|
||||||
for event in assistant.start():
|
for event in self.assistant.start():
|
||||||
self._process_event(event)
|
self._process_event(event)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue