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.

104 lines
3.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Libraries
import paho.mqtt.client as mqtt
import json
from time import sleep
from logic import tts, read_script
from config import characters, directions
# === SETUP OF MQTT PART 1 ===
# Location of the MQTT server
HOST = 'localhost'
PORT = 1883
# Subscribe to relevant MQTT topics
def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
client.subscribe('hermes/intent/jocavdh:play_intro_act') # to check for intent to play the act
client.subscribe('hermes/intent/jocavdh:question_continue_act') # to check for the intent to continue to the next act
client.subscribe("hermes/nlu/intentNotRecognized") # to check if the speaker didn't understand the user, trigger for fallback function
# === FUNCTIONS THAT ARE TRIGGERED WHEN AN INTENT IS DETECTED ===
# Function which is triggered when the intent play_intro_act is activated
def on_play_intro_act(client,data,msg):
data = json.loads(msg.payload)
sessionId = data['sessionId']
script_lines = read_script('play_scripts/demo.txt') # pick a random introduction script
for character, line, direction in script_lines:
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)
print('say this sentence')
# if action == 'start_interrogation':
# call(["python3", "play_interrogation_act.py"])
if action == 'listen_audience':
print('listen to the audience')
client.publish('hermes/dialogueManager/endSession', json.dumps({
'sessionId': sessionId
}))
client.publish('hermes/dialogueManager/startSession', json.dumps({
'siteId': 'default',
'init': {'type': 'action', 'canBeEnqueued': True, 'intentFilter':['jocavdh:question_continue_act']}
}))
break
# Function which is triggered when the intent question_continue_act is activated
def on_question_continue_act(client,data,msg):
data = json.loads(msg.payload)
answer_value = data['slots'][0]['value']['value']
print(answer_value)
voice = "dfki-obadiah"
speaker = 'default'
if answer_value == 'no':
print('no')
if answer_value == 'yes':
call(["python3", "play_interrogation_act.py"])
print('The play is over.')
# Function which is triggered when no intent is recognized
def onIntentNotRecognized(client, data, msg):
data = json.loads(msg.payload)
line_number = 1
print('We continue on line')
print(line_number)
on_introduce(client,data,msg, line_number)
# === SETUP OF MQTT PART 2 ===
# Initialise MQTT client
client = mqtt.Client()
client.connect(HOST, PORT, 60)
client.on_connect = on_connect
# Connect each MQTT topic to which you subscribed to a handler function
client.message_callback_add('hermes/intent/jocavdh:play_intro_act', on_play_intro_act)
client.message_callback_add('hermes/intent/jocavdh:question_continue_act', on_question_continue_act)
client.message_callback_add("hermes/nlu/intentNotRecognized", onIntentNotRecognized)
# Keep checking for new MQTT messages
client.loop_forever()