Listening to intents over MQTT using Python
In this tutorial, you will learn how to create a standalone Python application that listens to various events coming from the Snips Platform, in the form of MQTT messages.
Last updated
$ pip install paho-mqttimport paho.mqtt.client as mqtt
HOST = 'raspi3-mika2.local'
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
def on_message(client, userdata, msg):
print("Message received on topic {0}: {1}"\
.format(msg.topic, msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.loop_forever()def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
# Subscribe to the hotword detected topic
client.subscribe("hermes/hotword/default/detected")
def on_message(client, userdata, msg):
if msg.topic == 'hermes/hotword/default/detected':
print("Wakeword detected!")def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
# Subscribe to the hotword detected topic
client.subscribe("hermes/hotword/default/detected")
# Subscribe to intent topic
client.subscribe('hermes/intent/INTENT_NAME')
def on_message(client, userdata, msg):
if msg.topic == 'hermes/hotword/default/detected':
print("Hotword detected!")
elif msg.topic == 'hermes/intent/INTENT_NAME':
print("Intent detected!")def on_connect(client, userdata, flags, rc):
# Subscribe to any topic starting with 'hermes/intent/'
client.subscribe('hermes/intent/#')import json
def on_message(client, userdata, msg):
if msg.topic == 'hermes/intent/lightsTurnOnSet':
payload = json.loads(msg.payload)
name = payload["intent"]["intentName"]
slots = payload["slots"]
print("Intent {0} detected with slots {1}"\
.format(name, slots))