Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
google-assistant-sdk: sync hotword sample
Browse files Browse the repository at this point in the history
library-commit: f11eb210784825fde0d41b121863df1008edffc9

Bug: 71360271
Change-Id: I9ee81a9b8694d3c6982fb9c263ee051427549d84
  • Loading branch information
proppy committed Dec 28, 2017
1 parent 36b596e commit f00df5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions google-assistant-sdk/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

0.4.1
-----
- Update outdated `hotword` sample.

0.4.0
-----
- Add Device actions handling to samples.
Expand Down
26 changes: 23 additions & 3 deletions google-assistant-sdk/googlesamples/assistant/library/hotword.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python

# Copyright (C) 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,15 +13,22 @@
# 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 google.auth.transport.requests
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_API_URL = 'https://embeddedassistant.googleapis.com/v1alpha2'


Expand All @@ -41,14 +49,18 @@ def process_device_actions(event, device_id):

def process_event(event, device_id):
"""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:
print()

print(event)

if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
event.args and not event.args['with_follow_on_turn']):
print()
Expand All @@ -59,8 +71,10 @@ def process_event(event, device_id):

def register_device(project_id, credentials, device_model_id, device_id):
"""Register the device if needed.
Registers a new assistant device if an instance with the given id
does not already exists for this model.
Args:
project_id(str): The project ID used to register device instance.
credentials(google.oauth2.credentials.Credentials): The Google
Expand All @@ -79,6 +93,7 @@ def register_device(project_id, credentials, device_model_id, device_id):
r = session.post(base_url, data=json.dumps({
'id': device_id,
'model_id': device_model_id,
'client_type': 'SDK_LIBRARY'
}))
if r.status_code != 200:
raise Exception('failed to register device: ' + r.text)
Expand All @@ -98,22 +113,27 @@ def main():
help='Path to store and read OAuth2 credentials')
parser.add_argument('--device_model_id', type=str,
metavar='DEVICE_MODEL_ID', required=True,
help='The device model ID registered with Google.')
help='The device model ID registered with Google')
parser.add_argument('--project_id', type=str,
metavar='PROJECT_ID', required=False,
help='The project ID used to register device '
+ 'instances.')
help=('The project ID used to register'
'device instances'))

args = parser.parse_args()
with open(args.credentials, 'r') as f:
credentials = google.oauth2.credentials.Credentials(token=None,
**json.load(f))

with Assistant(credentials, args.device_model_id) as assistant:
events = assistant.start()

print('device_model_id:', args.device_model_id + '\n' +
'device_id:', assistant.device_id + '\n')

if args.project_id:
register_device(args.project_id, credentials,
args.device_model_id, assistant.device_id)

for event in events:
process_event(event, assistant.device_id)

Expand Down
2 changes: 1 addition & 1 deletion google-assistant-sdk/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def samples_requirements():

setup(
name='google-assistant-sdk',
version='0.4.0',
version='0.4.1',
author='Google Assistant SDK team',
author_email='[email protected]',
description='Samples and Tools the Google Assistant SDK',
Expand Down

0 comments on commit f00df5f

Please sign in to comment.