-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualchat.py
115 lines (105 loc) · 3.46 KB
/
virtualchat.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Operation vars and funcs
failsafe = True
offline = False
import intent
import voice
import sys
import os
import time
import requests as r
import json
import mechanics
if not offline:
config = json.load(open('config.json'))
else:
config = json.load(open('offline.json'))
deactivated_skills = config['deactivated-skills']
# Load intent data
intents = json.load(open('intents.json'))
# Load addtional user data
gps_info = json.load(open('data/gps.json'))
# Variable area
claraLocation = config['servers']['clara']['url']
speak = False
# Handles loading of requirements and permissions
def load_additional_config(intentName):
intent = None
for i in intents:
if i['name'] == intentName:
intent = i
break
to_return = {}
try:
permissions = intent['permissions']
for i in permissions:
if i == 'STRAVA_KEYS':
to_return.update(config['keys']['strava'])
if i == 'TODOIST_KEY':
to_return.update(config['keys']['todoist'])
except:
# No special information needed
do_nothing = True
return to_return
# Utility functions
def query_user(message):
print(message)
if speak:
voice.speak(message)
response = raw_input('> ')
return response
def query_clara(text):
data = {'input': text}
rawResp = r.post(claraLocation + 'api/v1/io/blocking', data).text
resp = json.loads(rawResp)
return resp
def process(text):
global speak, claraLocation, query_user
action = intent.process(text)
toReturn = 'None'
image = 'None'
if action == None:
# Chat mode
resp = query_clara(text)
toReturn = resp['message']
# image = resp['image']
else:
params = action
params.update({'SPEAK.VOICE_STATUS': speak, 'IO.QUERY_USER': query_user, 'CLARA.QUERY': query_clara, 'QUERY': text, 'LOCATION.GPS': gps_info});
if not action['intent_type'] in deactivated_skills:
additional_params = load_additional_config(action['intent_type'])
params.update(additional_params)
# Set up and send query
remote = config['remote']
remoteMachine = config['identities']['pal-command']
remoteLocation = config['servers']['pal-command']
remoteUrl = 'http://' + remoteLocation['server'] + ':' + remoteLocation['port'] + '/'
r.post(remoteUrl + 'add/' + remoteMachine, json={'command': action })
output = 'Request sent.'
else:
output = 'Unfortunately, the skill requested doesn\'t work with your current setup.'
if isinstance(output, (str, unicode)):
toReturn = output
else:
toReturn = output['message']
if output['cmd'] == 'SPEAK.VOICE_ON':
speak = True
elif output['cmd'] == 'SPEAK.VOICE_OFF':
speak = False
return {'message': toReturn, 'image': image}
if __name__ == '__main__':
exit = False
while not exit:
query = raw_input('> ')
if query.lower() == 'quit':
exit = True
returned = {}
if failsafe:
try:
returned = process(query)
except:
returned = {'message': 'Unable to perform desired action. Processing error occured.', 'image': 'None'}
else:
returned = process(query)
print(returned['message'])
if speak:
voice.speak(returned['message'])