0

I am trying to extend google assistant to be able to use my own local commands, it is working, but the voice still responds with "I can't help you with that" or something similar. When I try to use the stop_conversation() method I get an error:

TypeError: unbound method stop_conversation() must be called with Assistant instance as first argument (got nothing instead)

How can I fix this?

SalamalCamel
  • 65
  • 2
  • 3
  • 11
  • Did you pass any arguments to the `stop_conversation()` method, as it is saying that you need to pass a `Assistant Instance` as the arguments – Professor_Joykill Aug 09 '17 at 12:48

1 Answers1

-2
#!/usr/bin/env python

# Copyright (C) 2017 Google Inc.
#
# 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 os
import json
import subprocess
import google.oauth2.credentials
import RPi.GPIO as GPIO
import time
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.setwarnings(False)
#Number of entities in 'var' and 'PINS' should be the same
var = ('kitchen lights', 'bathroom lights', 'bedroom lights')#Add whatever names you want. This is case is insensitive 
gpio = (23,24,25)#GPIOS for 'var'. Add other GPIOs that you want

for pin in gpio:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, 0)




def process_event(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:
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  
    print(event)



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



def main():
    global credentials
    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('/home/pi/.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:
        subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Startup.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(3)
        for event in assistant.start():
            process_event(event)
            usr=event.args
            if 'trigger'.lower() in str(usr).lower():
                assistant.stop_conversation()
                if 'shut down'.lower() in str(usr).lower():
                    subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Pi-Close.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    time.sleep(10)
                    os.system("sudo shutdown -h now")
                    #subprocess.call(["shutdown -h now"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    break
                else:
                    for num, name in enumerate(var):
                        if name.lower() in str(usr).lower():
                            pinout=gpio[num]
                            if 'on'.lower()in str(usr).lower():
                                GPIO.output(pinout, 1)
                                subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Device-On.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                            elif 'off'.lower() in str(usr).lower():
                                GPIO.output(pinout, 0)
                                subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Device-Off.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)








if __name__ == '__main__':

    main()

Here is a code that i moded for my google assistant project and i guess this is what you are looking for. The stop_conversation should be called along with assistant function, else it will spill out errors.

  • 1
    1. Should be a comment, as this doesn't directly answer the question. 2. To a lesser extent, you should have mentioned that this was created by you: https://stackoverflow.com/help/promotion – Ramon Snir Sep 15 '17 at 15:26
  • 1
    I have expanded the answer with detailed codes.. thanks for letting me know. – Shiva Siddharth Sep 15 '17 at 16:23