Skip to content

Instantly share code, notes, and snippets.

@longjie
Last active September 14, 2020 14:41
Show Gist options
  • Save longjie/309813472c39d33018ce34453e9a587b to your computer and use it in GitHub Desktop.
Save longjie/309813472c39d33018ce34453e9a587b to your computer and use it in GitHub Desktop.
This is a simple python file for the demo "gWall: gWall: LCD+RasPi+Google Assistant" (https://www.youtube.com/watch?v=Y-H_3o7vKcc)
#!/usr/bin/env python
# Copyright (C) 2017 Google Inc.
# Copyright (C) 2017 Ryosuke Tajima
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import argparse
import os.path
import json
import uinput
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
device = uinput.Device(
[uinput.KEY_ENTER, uinput.KEY_M, uinput.KEY_W, uinput.KEY_D, uinput.KEY_X])
def process_event(assistant, event):
"""Pretty prints events.
Prints all events that occur with two spaces between each new
conversation and a single space between turns of a conversation.
Args:
event(event.Event): The current event to process.
"""
if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
device.emit_click(uinput.KEY_ENTER)
print()
print(event)
# Process for google calendar in Chrome
if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
print(event.args['text'])
if 'calendar' in event.args['text']:
assistant.stop_conversation()
if 'day' in event.args['text']:
device.emit_click(uinput.KEY_D)
elif 'week' in event.args['text']:
device.emit_click(uinput.KEY_W)
elif 'month' in event.args['text']:
device.emit_click(uinput.KEY_M)
elif 'custom' in event.args['text']:
device.emit_click(uinput.KEY_X)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--credentials', type=existing_file,
metavar='OAUTH2_CREDENTIALS_FILE',
default=os.path.join(
os.path.expanduser('~/.config'),
'google-oauthlib-tool',
'credentials.json'
),
help='Path to store and read OAuth2 credentials')
args = parser.parse_args()
with open(args.credentials, 'r') as f:
credentials = google.oauth2.credentials.Credentials(token=None,
**json.load(f))
with Assistant(credentials) as assistant:
for event in assistant.start():
process_event(assistant, event)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment