You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Fuction recognize intents and connect them to the right actions
import paho.mqtt.client as mqtt
import json
from time import sleep
from logic import tts, read_script
from config import characters, directions
HOST = 'localhost'
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
# Subscribe to the text detected topic
client.subscribe("hermes/asr/textCaptured")
client.subscribe("hermes/nlu/intentNotRecognized")
client.subscribe('hermes/intent/jocavdh:ask')
client.subscribe('hermes/intent/jocavdh:answer_yes')
client.subprocess('hermes/dialogueManager/sessionEnded')
client.subprocess('hermes/dialogueManager/sessionStarted')
def on_introduce(client,data,msg):
data = json.loads(msg.payload)
sessionId = data['sessionId']
for character, line, direction in read_script('play_scripts/demo.txt'):
input_text = line
voice = characters.get(character)[0]
speaker = characters.get(character)[1]
#speaker = 'default'
# Some way to do something with the stage directions will come here
action = directions.get(direction[0])
tts(voice, input_text, speaker)
client.publish('hermes/dialogueManager/endSession', json.dumps({
'sessionId': sessionId
}))
client.publish('hermes/dialogueManager/startSession', json.dumps({
'siteId': 'default',
'init': {'type': 'action', 'canBeEnqueued': False, 'intentFilter':['jocavdh:answer_yes']}
}))
def on_answer(client,data,msg):
data = json.loads(msg.payload)
answer_value = data['slots'][0]['value']['value']
print(answer_value)
if answer_value == 'yes':
input_text = 'Lorem ipsum'
if answer_value == 'no':
input_text = 'nope nope nope'
voice = "dfki-obadiah"
speaker = 'default'
tts(voice, input_text, speaker)
print('The play is over.')
client = mqtt.Client()
client.connect(HOST, PORT, 60)
client.on_connect = on_connect
client.connected_flag=False
client.message_callback_add('hermes/intent/jocavdh:ask', on_introduce)
client.message_callback_add('hermes/intent/jocavdh:answer_yes', on_answer)
client.message_callback_add('hermes/intent/jocavdh:no', on_answer)
print('main')
listening = False
client.loop_forever()