2017-12-22 10:18:04 +01:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
2017-12-22 10:43:43 +01:00
|
|
|
import subprocess
|
2017-12-22 10:18:04 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
import google.oauth2.credentials
|
|
|
|
|
|
|
|
from google.assistant.library import Assistant
|
|
|
|
from google.assistant.library.event import EventType
|
|
|
|
from google.assistant.library.file_helpers import existing_file
|
|
|
|
|
2017-12-22 10:21:31 +01:00
|
|
|
from platypush.backend import Backend
|
2017-12-22 10:18:04 +01:00
|
|
|
|
|
|
|
class AssistantGoogleBackend(Backend):
|
|
|
|
""" Class for the Google Assistant backend. It creates and event source
|
|
|
|
that posts recognized phrases on the main bus """
|
|
|
|
|
|
|
|
def __init__(self, credentials_file=os.path.join(
|
|
|
|
os.path.expanduser('~/.config'),
|
2017-12-22 10:43:43 +01:00
|
|
|
'google-oauthlib-tool', 'credentials.json'),
|
|
|
|
on_conversation_start=None, on_conversation_end=None, **kwargs):
|
2017-12-22 10:18:04 +01:00
|
|
|
""" Params:
|
|
|
|
credentials_file -- Path to the Google OAuth credentials file
|
2017-12-22 10:43:43 +01:00
|
|
|
(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)
|
|
|
|
"""
|
2017-12-22 10:18:04 +01:00
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.credentials_file = credentials_file
|
2017-12-22 10:43:43 +01:00
|
|
|
self.on_conversation_start = on_conversation_start
|
|
|
|
self.on_conversation_end = on_conversation_end
|
2017-12-22 10:18:04 +01:00
|
|
|
|
2017-12-22 10:43:43 +01:00
|
|
|
with open(self.credentials_file, 'r') as f:
|
2017-12-22 10:18:04 +01:00
|
|
|
self.credentials = google.oauth2.credentials.Credentials(token=None,
|
|
|
|
**json.load(f))
|
|
|
|
|
|
|
|
self.assistant = None
|
|
|
|
|
|
|
|
def _process_event(self, event):
|
2017-12-22 10:43:43 +01:00
|
|
|
logging.debug('Received assistant event: {}'.format(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)
|
2017-12-22 10:18:04 +01:00
|
|
|
|
|
|
|
def send_message(self, msg):
|
2017-12-22 10:43:43 +01:00
|
|
|
# Cant' send a message on an event source, ignoring
|
|
|
|
pass
|
2017-12-22 10:18:04 +01:00
|
|
|
|
|
|
|
def on_stop(self):
|
|
|
|
if self.producer:
|
|
|
|
self.producer.flush()
|
|
|
|
self.producer.close()
|
|
|
|
|
|
|
|
if self.consumer:
|
|
|
|
self.consumer.close()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
super().run()
|
|
|
|
|
|
|
|
with Assistant(self.credentials) as self.assistant:
|
2017-12-22 10:43:43 +01:00
|
|
|
for event in self.assistant.start():
|
2017-12-22 10:18:04 +01:00
|
|
|
self._process_event(event)
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|