-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrain_api.py
More file actions
70 lines (53 loc) · 2.03 KB
/
Copy pathbrain_api.py
File metadata and controls
70 lines (53 loc) · 2.03 KB
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
import json
import urllib
import urllib2
import say_something as TALK
import perform_actions as ACTION
import os.path, sys
from codecs import open
try:
import apiai
except ImportError:
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
import apiai
CONFUSE_RESPONSE = "not sure what you mean"
CLIENT_ACCESS_TOKEN = '13096bfb50a44c9aa488935a68e83951'
SUBSCRIPTION_KEY = '514967bc-08d6-474c-8a3d-1ed4b7e774f1 '
ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)
def query(message):
print("querying message: '%s'" % message)
message_response = get_response(message)
if len(message_response) > 0:
TALK.say_message(message_response)
else:
ACTION.action_be_confused()
TALK.say_message(CONFUSE_RESPONSE)
def get_response(message):
request = ai.text_request()
request.lang = 'en' # optional, default value equal 'en'
request.query = message
response = request.getresponse()
return json.loads(response.read())['result']['fulfillment']['speech']
# if __name__ == '__main__':
# main("what is your name")
# # header = {
# # 'Content-Type' : ,
# # 'Authorization' : 'Bearer 7fe7f921ed594df7941e9efea29a7e0c',
# # 'ocp-apim-subscription-key' : 'de9c57d0-dc85-42ee-9960-e0518dfd6c6d'
# # }
# #https://api.api.ai/v1/query?v=20150910&query=%s&lang=en&timezone=Europe/London
# quoted_query = urllib.quote(message)
# url = "https://api.api.ai/v1/query?v=20150910&query=%s&lang=en&timezone=Europe/London" % ( quoted_query)
# print(url)
# req = urllib2.Request(url)
# req.add_header('Content-Type', 'application/json; charset=utf-8')
# req.add_header('Authorization', 'Bearer 7fe7f921ed594df7941e9efea29a7e0c')
# req.add_header('ocp-apim-subscription-key', 'de9c57d0-dc85-42ee-9960-e0518dfd6c6d')
# response = urllib2.urlopen(req)
# data = json.load(response)
# print(data)
# if data['result']['metadata']['speech'] != '':
# return data['result']['metadata']['speech']
# else:
# return CONFUSE_RESPONSE
# print(get_response("asdasde"))