-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
154 lines (140 loc) · 4.41 KB
/
chat.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Operation vars and funcs
failsafe = True
offline = False
# Global Imports
import curses
from curses import textpad
# Local Imports
import intent
import voice
import sys
import os
import time
import requests as r
import json
import mechanics
import asyncconsole
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({'strava': 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('> ')
if response == '\n':
print('Just the line...?')
return response
def query_clara(text):
data = {'input': text}
rawResp = r.post(claraLocation + 'api/v1/io/blocking/pal', data).text
#print(rawResp)
resp = json.loads(rawResp)
return resp
def process(text):
global speak, claraLocation, query_user, gps_info
action = intent.process(text)
toReturn = 'None'
image = 'None'
if action == None:
# Chat mode
resp = query_clara(text)
toReturn = resp['message']
if 'image' in resp.keys():
image = resp['image']
else:
image = None
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)
output = mechanics.functions[action['intent_type']](action)
else:
output = 'Unfortunately, the skill requested doesn\'t work with your current setup.'
try: # For Python3
unicode
except:
unicode = str
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
elif output['cmd'] == 'LOCATION.REFRESH':
gps_info = json.load(open('data/gps.json'))
return {'message': toReturn, 'image': image}
def main(stdscr):
# demo code
console = asyncconsole.AsyncConsole(stdscr)
exit = False
while not exit and console.readline():
query = console.input_string
console.addline('Q: ' + query)
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)
console.addline('R: ' + returned['message'])
if speak:
voice.speak(returned['message'])
if __name__ == '__main__':
type = 'curses'
if type == 'curses':
curses.wrapper(main)
else:
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'])