Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
from sic_framework.devices import Nao
from sic_framework.devices.nao import NaoNaoqiTextToSpeechRequest
from sic_framework.services.dialogflow.dialogflow_service import DialogflowService(DialogflowConf, DialogflowConfGetIntentRequest, GetIntentRequest, \RecognitionResult,
                                                          RecognitionResultQueryResult, QueryResult

...

Dialogflow)

First, we create a connection to the Nao robot to be able to access its microphone (using nao.mic after it is initialized). and load the key json file. In order to use this intent in an application, we need to set the language, project ID (agent name), keyfile and the keyfilesample rate. To do this, we create the Dialogflow configuration object. Make sure you read the documentation at the start of this page to obtain the files and ID key file for your Dialogflow project.

Code Block
class
DemoDialogflow(SICApplication):# connect to   def run(self) -> None:
        
        the robot
nao = Nao(device_idip='nao', application=self192.168.178.45')

# load the key json   
        conf = DialogflowConf(keyfile="dialogflow-key.json",
                    project_id='dialogflow-test-project-376814', 
                    file
keyfile_json = json.load(open("../../../dialogflow-tutorial.json"))

# set up the config
conf = DialogflowConf(keyfile_json=keyfile_json, sample_rate_hertz=16000)

Having done this setup, we can self.connect to the Dialogflow service (make sure it is up and running, or you will get a timeout). The parameters inputs_to_service=[nao.mic] initate the Dialogflow object and connect the output of NaoqiMicrophone as the input of Dialogflow. The parameters ip='localhost' and conf=conf pass the Nao microphone as an input ip adress of the device the DialogflowComponent is running on and our configuration to be able to authenticate to Dialogflow.

Code Block
# initiate Dialogflow    object
 dialogflow = self.connect(DialogflowService, device_idDialogflow(ip='locallocalhost', conf=conf)

# connect the output of NaoqiMicrophone as the input of              inputs_to_service=[DialogflowComponent
dialogflow.connect(nao.mic], conf=conf))

Finally, we need to register a callback function to act whenever Dialogflow output is available. Whenever Dialogflow detects a new word, we will receive a RecognitionResult message. The, on_recognition_resultdialog function simply prints the detected speech when it’s considered final.

Code Block
# register a callback function to act upon arrival of recognition_result
dialogflow.register_callback(on_recognition_resultdialog)

Now we can start actually getting intents from the user! We need to set a chat ID, with which Dialogflow identifies the conversation. This can be a random number (or the same one if you want to continue a conversation). Then, we request Dialogflow to get an intent. It will start sending the Nao’s microphone audio to Dialogflow. As you start talking, the on_recognition_resultdialog should print interim final transcripts.

Whenever you are done, and if Dialogflow successfully detected your intent, it should print it on screen! The Dialogflow agent’s response should also be printed.

Code Block
             chat_id
x = np.random.randint(10000)

   
      for i in range(25):

         print(" -----> Conversation turn", i)
   
      reply = dialogflow.request(GetIntentRequest(chat_idx))

    print(reply.intent)

    if reply.fulfillment_message:
        iftext = reply.fulfillment_message:
 
            print("Reply:", reply.fulfillment_messagetext)
           if reply.response.query_result.intent:
              print("Intent:", reply.response.query_result.intent.display_namenao.tts.request(NaoqiTextToSpeechRequest(text))

Here is the definition for on_recognition_resultdialog

Code Block
def on_recognition_resultdialog(message):
    if message.response:
        print(if message.response.recognition_result.transcript)

To start your application, we need to include

Code Block
if __name__ == '__main__':is_final:
      test_app = DemoDialogflow()     test_app.run(print("Transcript:", message.response.recognition_result.transcript)

And that's it! You should now be able to talk to your robot. See also https://bitbucket.org/socialroboticshub/dockerframework/src/v3/sicmaster/sic_framework/tests/demo_dialogflow.py for a more complex complete example. Make sure to set your own agent name and keyfile path!