-
Notifications
You must be signed in to change notification settings - Fork 0
/
action-app_joke_tuto.py
executable file
·75 lines (58 loc) · 2.59 KB
/
action-app_joke_tuto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
from snipsTools import SnipsConfigParser
from hermes_python.hermes import Hermes
from hermes_python.ontology import *
import io
import requests
CONFIG_INI = "config.ini"
# If this skill is supposed to run on the satellite,
# please get this mqtt connection info from <config.ini>
# Hint: MQTT server is always running on the master device
MQTT_IP_ADDR = "localhost"
MQTT_PORT = 1883
MQTT_ADDR = "{}:{}".format(MQTT_IP_ADDR, str(MQTT_PORT))
class JokeTuto(object):
"""Class used to wrap action code with mqtt connection
Please change the name refering to your application
"""
def __init__(self):
# get the configuration if needed
try:
self.config = SnipsConfigParser.read_configuration_file(CONFIG_INI)
except :
self.config = None
# start listening to MQTT
self.start_blocking()
# --> Sub callback function, one per intent
def askJoke_callback(self, hermes, intent_message):
# terminate the session first if not continue
good_category = requests.get("https://api.chucknorris.io/jokes/categories").json()
category = None
if intent_message.slots:
category = intent_message.slots.category.first().value
# check if the category is valide
if category.encode("utf-8") not in good_category:
category = None
if category is None:
joke_msg = str(requests.get("https://api.chucknorris.io/jokes/random").json().get("value"))
else:
joke_msg = str(requests.get("https://api.chucknorris.io/jokes/random?category={}".format(category)).json().get("value"))
new_people = self.config.get("secret").get("protagonist")
if new_people is not None and new_people is not "":
joke_msg = joke_msg.replace('Chuck Norris', new_people)
print("test print in code.")
hermes.publish_end_session(intent_message.session_id, joke_msg)
# More callback function goes here...
# --> Master callback function, triggered everytime an intent is recognized
def master_intent_callback(self,hermes, intent_message):
coming_intent = intent_message.intent.intent_name
if coming_intent == 'hooray4me:askJoke':
self.askJoke_callback(hermes, intent_message)
# more callback and if condition goes here...
# --> Register callback function and start MQTT
def start_blocking(self):
with Hermes(MQTT_ADDR) as h:
h.subscribe_intents(self.master_intent_callback).start()
if __name__ == "__main__":
JokeTuto()