2017-08-09 32 views
0

我試圖擴展谷歌助理,以便能夠使用我自己的本地命令,它正在工作,但語音仍然響應「我無法幫助你」或類似的東西。當我嘗試使用stop_conversation()方法我得到一個錯誤:調用stop_conversation時發生TypErrror()

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

我該如何解決這個問題?

+0

您是否將任何參數傳遞給'stop_conversation()'方法,因爲它表示您需要傳遞一個'Assistant Instance'作爲參數 –

回答

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

這是我爲我的谷歌助理項目模擬的代碼,我想這就是你要找的。 stop_conversation應該和助手函數一起調用,否則會溢出錯誤。

+1

1.應該發表評論,因爲這不會直接回答題。 2.在較小程度上,您應該提到這是由您創建的:https://stackoverflow.com/help/promotion –

+0

儘管此鏈接可能回答此問題,但最好在此處包含答案的基本部分並提供鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/評論/低質量帖子/ 17345444) –

+1

我已經擴大答案與詳細的代碼..謝謝你讓我知道。 –

相關問題