import re import os from aiy.cloudspeech import CloudSpeechClient import dialogflow_v2 as dialogflow import neopixel import time import board import robot_util pixels = neopixel.NeoPixel(board.D12,30) print('setup...') client = CloudSpeechClient('/home/pi/cloud_speech.json') session_client = dialogflow.SessionsClient() session = session_client.session_path('XXXX', 12345) def getDialogResponse(text): text_input = dialogflow.types.TextInput(text=text, language_code='EN') query_input = dialogflow.types.QueryInput(text=text_input) response = session_client.detect_intent(session=session, query_input=query_input) text = response.query_result.fulfillment_text return response # example for adding a custom chat handler main_chat_handler = None # Reboot function, to be added to the extended chat commands. def reboot(*args): os.system('sudo shutdown -r now') exit() def setup(robot_config, chat_handler): global main_chat_handler # Any chat related setup code goes here # Add a reboot command to the extended chat commands if robot_config.getboolean('tts', 'ext_chat'): import extended_command extended_command.add_command('.reboot', reboot) main_chat_handler = chat_handler def handle_chat(args): # Your custom chat handling code goes here print(args['message']) spokenText = args['message'] response = getDialogResponse(spokenText) responseText = response.query_result.fulfillment_text responseIntent = response.query_result.intent.display_name if responseIntent == 'Default Fallback Intent': robot_util.sendChatMessage("No Bull****!") pixels.fill((0, 255, 0)) time.sleep(1) pixels.fill((0, 0, 0)) else: robot_util.sendChatMessage("Bull****!") pixels.fill((255, 0, 0)) time.sleep(1) pixels.fill((0, 0, 0)) # Call the main chat handler main_chat_handler(args)