2

I'm using the Google Assistant SDK's hotword.py to create my own custom commands.

Everything works, however since these custom commands are not recognized by Google Assistant, I get one of Google Assistant's many "command not found" variations. I'm trying to call assistant.stop_assistant()

But I just get:

Traceback (most recent call last):
  File "hotword.py", line 100, in <module>
    main()
  File "hotword.py", line 96, in main
    process_event(event)
  File "hotword.py", line 65, in process_event
    assistant.stop_conversation()
NameError: name 'assistant' is not defined

How can I resolve this error?

Code:

from __future__ import print_function

import argparse
import os.path
import json
import os
import json
import threading
import RPi.GPIO as GPIO
from subprocess import call
from time import sleep
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

GPIO.setmode(GPIO.BCM)
GPIO.setup(7,GPIO.OUT)

def process_event(event):
   
    if event.type == EventType.ON_START_FINISHED:
        os.system('aplay /home/pi/assistant-extras/assistant_is_ready.wav')
        print("ready")

    if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        print()
        GPIO.output(7,GPIO.HIGH)
    if event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED:
        GPIO.output(7,GPIO.LOW)
        spoken_words = event.args["text"]
        print("Spoken text: " + spoken_words)
        if 'text editor'==spoken_words:
            os.system('lxterminal -e sudo leafpad')
            assistant.stop_conversation()

    print(event)

    if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
            event.args and not event.args['with_follow_on_turn']):
        print()


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(event)


if __name__ == '__main__':
    main()

1 Answer 1

0

assistant is not a global variable, so proccess_event() cannot see it. It would be a simple matter to pass it to proccess_event().

Change the signature to:

def process_event(event, assistant):

Then call it as:

for event in assistant.start():
    process_event(event, assistant)
1
  • Yes! That was it! Thank you very much. This was bothering me for hours!
    – Leo Takacs
    Commented Oct 23, 2017 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.