-
Notifications
You must be signed in to change notification settings - Fork 4
/
apiai_toolkit.py
60 lines (52 loc) · 1.6 KB
/
apiai_toolkit.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
import apiai
import random
import json
import requests
CLIENT_ACCESS_TOKEN = "f60d8f1610c44081af5d42d61631e91a"
ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)
SESSION_ID = str(random.randint(2,999))
def format_message(action,reply_message,outcome):
"""
formats output message depending on the action
"""
try:
reply_message = reply_message.replace(action,outcome[0])
except IndexError:
reply_message = "Oops !! That is not a proper word I couldn't find anything about it in the Dictionary"
return reply_message
def get_intent_action_entity(response):
"""
given apiai response returns intent,entity,action
"""
action = None
entitiy = None
try:
intent = response['result']['metadata']['intentName']
except KeyError:
intent = None
try:
action = response["result"]["action"]
except KeyError:
action = None
try:
entitiy = response["result"]["parameters"]['word']
except KeyError:
try:
for context in response["result"]["contexts"]:
if context["name"] == "word":
entitiy = context["parameters"]['word']
break
except KeyError as e:
print (e)
entitiy = None
return intent,action,entitiy
def send_message(message):
"""
Sends user text message to api.ai and gets NLU information
"""
request = ai.text_request()
request.session_id = SESSION_ID
request.query = message
response = request.getresponse()
raw_response = response.read().decode("utf-8")
return json.loads(raw_response)