-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
240 lines (199 loc) · 7.99 KB
/
controller.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from flask import Flask, request, jsonify
from pprint import pprint
from weather import Weather
from covid import Covid
from stocks import Stocks
from government import Government
from search import Search
from news import News
from chatbot import witBot
from gmail import Gmail
import threading
import time
import vonage
'''
API key + Secret & phone number temporarily removed for privacy
'''
app = Flask(__name__)
client = vonage.Client(key='', secret='')
weather_instance = Weather()
covid_instance = Covid()
stock_instance = Stocks()
government_instance = Government()
search_instance = Search()
news_instance = News()
bot_instnace = witBot()
mail_instance = Gmail()
recipient_number = ''
name = "Yash"
@app.route('/webhooks/inbound-sms', methods=['GET', 'POST'])
def inbound_sms():
if request.is_json:
pprint(request.get_json())
else:
data = dict(request.form) or dict(request.args)
pprint(data)
recipient_number = data['msisdn']
text = data['text']
controller(text)
return ('', 204)
def controller(text):
understood = bot_instnace.understand(text)
print(understood)
if(understood[0] == 'NULL'):
if(text.lower() == 'hey'):
send(recipient_number,"Hey, I'm Sophia your new personal assistant. You can text me any time night or day if you need any assistance.")
time.sleep(2)
send(recipient_number,"Silly me I'm on a new phone oculd you text me your name please?")
if('yash' in text.lower()):
send(recipient_number, "Hello Yash, I am all set up for you :) You can always ask me for help on how to use me just type in 'help'")
time.sleep(2)
send(recipient_number, "oh yeah. I almost forgot, here are the start up stats you wanted:")
time.sleep(2)
initialize(recipient_number)
if(understood[0] == 'Emails'):
mail_instance.add_whitelist(understood[1]['wit$email'])
send(recipient_number, 'Great! I will look out for emails from ' + understood[1]['wit$email'])
if(understood[0] == 'Weather'):
send(recipient_number, ('Hey Yash, its ' + str(weather_instance.weatherGet()) + ' degrees. ' + temp_message(int(weather_instance.weatherGet()))))
if (understood[0] == 'Covid'):
temp_val = covid_instance.covidGet()
send(recipient_number, 'There are ' + str(temp_val[0]) + ' people infected with COVID-19 in Ontario')
time.sleep(1)
send(recipient_number, 'Sadly ' + str(temp_val[1]) + ' people have died Ontario from COVID')
time.sleep(1)
if(understood[0] == 'Stocks'):
if(understood[1]['Ticker'].lower() == 'watchlist'):
send(recipient_number, 'let me take a look')
send(recipient_number, 'here is how your watchist is doing \n USD to PHP is' + str(stock_instance.get_data('USD_PHP')) + '\n KRW to USD is' + str(stock_instance.get_data('KRW_USD')) + '\n JPY to USD is' + str(stock_instance.get_data('JPY_USD')))
else:
send(recipient_number, 'Alright, so I have looked it up and the value of USD to PHP is ' + str(stock_instance.get_data(understood[1]['Ticker'])))
if(understood[0] == 'thanks'):
send(recipient_number, 'Any Time :)')
if(understood[0] == 'government'):
send(recipient_number, 'Sure let me see')
send(recipient_number, is_government_update())
if(understood[0] == 'search'):
send(recipient_number, 'let me look it up')
time.sleep(3)
send(recipient_number, search_instance.search_info(text))
if(understood[0] == 'news'):
send(recipient_number, 'Here you go\nNEWS UPDATE: ' + news_update())
# Stocks
def load_stock(recipient_number):
old_porfolio = [['USD_PHP', 0],['KRW_USD', 0],['JPY_USD', 0]]
while(True):
old_porfolio = check_stock(old_porfolio)
time.sleep(3600)
def check_stock(old_porfolio):
n = 0
change = 0
for i in old_porfolio:
temp_price = stock_instance.get_data(i[0])
if(i[1] != 0):
change = (temp_price-i[1])/i[1]
if((abs(change) > 0.00005) or (i[1] == 0)):
send(recipient_number, 'Hey you might want to check on your posiiton in ' + i[0] + ', the value just shifted to ' + str(temp_price))
time.sleep(1)
i[1] = temp_price
n+=1
return old_porfolio
# Covid
def load_covid(recipient_number):
old_covid = [0,0]
while(True):
old_covid = check_covid(recipient_number, old_covid)
time.sleep(600)
def check_covid(recipient_number, old_covid):
temp_val = covid_instance.covidGet()
temp_infect = int(temp_val[0]) - int(old_covid[0])
if(abs(temp_infect)> 10):
send(recipient_number, 'There are ' + str(temp_val[0]) + ' people infected with COVID-19 in Ontario')
time.sleep(1)
temp_dead = int(temp_val[1]) - int(old_covid[1])
if(abs(temp_dead) > 1):
send(recipient_number, 'Sadly ' + str(temp_val[1]) + ' people have died Ontario from COVID')
time.sleep(1)
send(recipient_number, "it's pretty bad out there! Be Careful : )")
time.sleep(1)
return temp_val
# Weather
def temp_message(temp):
if(temp<5):
return "It'll be cold so stay warm."
if(temp>25):
return "Oh its warm, have fun outside."
def load_weather(recipient_number):
old_val = None
while(True):
old_val = check_weather(recipient_number, old_val)
print('weather')
time.sleep(60)
def check_weather(recipient_number, old_val):
temp_val = weather_instance.weatherGet()
if(old_val != temp_val):
send(recipient_number, 'Heads up the temperature in Toronto is currently ' + str(temp_val) + 'degrees')
return temp_val
# Government
def load_government(recipient_number):
while(True):
temp_update = government_instance.get_alert()
if(temp_update != 'NULL'):
send(recipient_number, 'Yash, here is a urgent government alert that just got sent out: ' + temp_update)
time.sleep(60)
def is_government_update():
temp_update = government_instance.get_alert()
if(temp_update != 'NULL'):
return 'There is : ' + temp_update
return 'No there is not'
# News
def load_news(recipient_number):
old_news = None
while(True):
old_news = check_news(recipient_number, old_news)
print('weather')
time.sleep(20)
def check_news(recipient_number, old_news):
temp_news = str(news_instance.get_polished_articles())
if(old_news != temp_news):
send(recipient_number, 'News Update: ' + str(news_instance.get_polished_articles()))
return temp_news
def news_update():
news = news_instance.get_polished_articles()
news_text = news[0]['title']
return news_text
# Email
def load_mail(recipient_number):
print('sdf')
old_mail = None
while(True):
old_mail = check_mail(recipient_number, old_mail)
time.sleep(10)
def check_mail(recipient_number, old_mail):
temp_mail = mail_instance.read_latest_emails()
if(temp_mail != old_mail):
print(temp_mail)
send(recipient_number, 'Hey you should probably check this out\n' + temp_mail)
return temp_mail
# Send
def send(recipient_number, text):
result = client.send_message({
'from': '15877603813',
'to': recipient_number,
'text': '{}'.format(text),
})
def initialize(recipient_number):
weather_tread = threading.Thread(target=load_weather, args=(recipient_number, ))
weather_tread.start()
covid_tread = threading.Thread(target=load_covid, args=(recipient_number, ))
covid_tread.start()
stock_tread = threading.Thread(target=load_stock, args=(recipient_number, ))
stock_tread.start()
government_tread = threading.Thread(target=load_government, args=(recipient_number, ))
government_tread.start()
news_tread = threading.Thread(target=load_news, args=(recipient_number, ))
news_tread.start()
mail_tread = threading.Thread(target=load_mail, args=(recipient_number, ))
mail_tread.start()
app.run(port=3000)
# Created By: Yash Trivedi