-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtweety_main.py
303 lines (258 loc) · 14 KB
/
tweety_main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'''Copyright (c) 2023, Tharun Kumar Reddy Polu
All rights reserved.
This source code is licensed under the GNU General Public License v3.0
License found in the LICENSE.md file in the root directory of this source tree.'''
# import required libraries and utility functions from tweety_functions.py file
from tweety_functions import *
if __name__ == '__main__':
# the below line of code clears the console
# before tweety turns up everytime
clear = lambda: os.system('cls')
clear()
# loading the assistant, starts with a Wish and gets the user info
load_tweety()
wish_me()
user_name = user_info()
tweety_speaks(f"Hope you are doing great Tell me how can I help you now?")
print(f"{user_name},Hope you are doing great. Tell me how can I help you now?")
# list to ask for further questions
repeat_intro = ["Do you have any other questions",
"Do i have any further questions if not i can take a short nap just kidding",
"How can i help you furthermore please say yes if you have any queries"]
# Below variable keeps track of tweety voice assistant usage
usage_count = 1
while True:
# check if tweety is ready to answer the second question asked by the user
if usage_count >= 2:
ask_next_query = random.choice(repeat_intro)
tweety_speaks(ask_next_query)
print(ask_next_query)
next_command = takeCommand().lower()
if next_command == "yes":
usage_count = 1
continue
else:
# print("in else") used for debugging purpose
break
command = str(takeCommand().lower())
print(command)
if command == '':
break
else:
if "tweety" in command:
wish_me()
tweety_speaks("Hey {} i am awake".format(user_name))
print("Hey {} i am awake".format(user_name))
if 'wikipedia' in command:
tweety_speaks('Searching Wikipedia...')
command = command.replace("wikipedia", "")
if command != '':
results = wikipedia.summary(command, sentences=3)
print("According to Wikipedia" + results)
tweety_speaks("According to Wikipedia" + results)
else:
tweety_speaks("please try again")
# web browser based actions
if 'stack overflow' in command:
execute_webbrowser("https://www.stackoverflow.com", "website")
if 'youtube' in command:
execute_webbrowser("https://www.youtube.com", "website")
if 'gmail' in command:
execute_webbrowser("https://www.gmail.com", "website")
if 'search' in command:
command = command.replace("search ", "")
if command != '':
parsed_command = "https://www.google.com/search?q=" + command
execute_webbrowser(parsed_command, "query")
else:
tweety_speaks("Please check there is nothing to search")
print("Please check there is nothing to search")
if "where is " in command:
location = command.replace("where is ", "")
tweety_speaks("Locating " + location)
location_search_link = "https://www.google.nl / maps / place/" + location + ""
execute_webbrowser(location_search_link, "query")
# send mail to custom recipient
if 'send a mail' in command:
try:
tweety_speaks("whom should i send the mail?")
print("whom should i send the mail?")
to = input()
tweety_speaks("what should i say?")
print("what should i say?")
mail_body = takeCommand()
mail_sent_status = send_email(to, mail_body)
if mail_sent_status == True:
tweety_speaks("The email has been sent!")
print("The email has been sent!")
except Exception as e:
print(e)
tweety_speaks("Sorry! I am unable to send the email")
if "send message" in command or "text message" in command:
# create an account on Twilio to access the service
acnt_sid = "Your account Sid key"
auth_token = "Your auth token"
client = Client(acnt_sid, auth_token)
message = client.messages.create(
body=takeCommand(),
from_="Sender Number",
to="Recipient Number"
)
print(message.sid)
tweety_speaks("Message sent successfully to the recipient")
# get the latest news updates using news api - Third Party API case
if 'news' in command:
try:
json_object = urlopen(
'''https://newsapi.org / v1 / articles?source = the-times-of-india&sortBy = top&apiKey =\\times of India Api key\\''')
headlines = json.load(json_object)
i = 1
tweety_speaks('here are some top news from the times of india')
print("=============== TIMES OF INDIA ============" + '\n')
for item in headlines['articles']:
print(str(i) + '. ' + item['title'] + '\n')
print(item['description'] + '\n')
tweety_speaks(str(i) + '. ' + item['title'] + '\n')
i += 1
except Exception as e:
print(str(e))
tweety_speaks("Oops! An error occurred while fetching the news")
# get the latest weather updates with city name using openweathermap - Third Party API case
if "weather" in command:
api_key = "Your Own API key"
base_url = "https://api.openweathermap.org/data/2.5/weather?"
tweety_speaks("what is the city name")
print("what is the city name")
city_name = takeCommand()
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = request.get(complete_url)
x = response.json()
if x["code"] != "404":
y = x["main"]
current_temperature = y["temp"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
tweety_speaks(" Temperature in kelvin unit is " +
str(current_temperature) +
"\n humidity in percentage is " +
str(current_humidiy) +
"\n description " +
str(weather_description))
print(" Temperature in kelvin unit = " +
str(current_temperature) +
"\n humidity (in percentage) = " +
str(current_humidiy) +
"\n description = " +
str(weather_description))
else:
tweety_speaks("Requested City Not Found")
# taking a note into a file - file handling case
if "write a note" in command or "take a note" in command:
tweety_speaks("what do i need to jot down")
note = takeCommand()
note_file = open("tweety_notes.txt", "w")
tweety_speaks("Should i include date and time")
include_dt = takeCommand()
if "yes" in include_dt or "yeah" in command or "sure" in command:
noting_time = datetime.datetime.now().strftime("% H:% M:% S")
note_list = [noting_time, " :- ", note]
note_file.writelines(note_list)
else:
note_file.write(note)
# reading the taken note in file - file handling case
if "show note" in command or "read notes" in command:
tweety_speaks("Speaking out notes")
if os.stat("tweety_notes.txt").st_size == 0:
tweety_speaks("Oops! the notes is empty")
else:
with open("tweety_notes.txt") as f:
contents = f.read()
print(contents)
tweety_speaks(contents)
# get answers to computational and geographical queries
if "what is" in command or "who is" in command:
execute_wolframalpha()
if "ask" in command:
tweety_speaks(
'I can answer most of your questions because i am still learning and now what question do you '
'want to ask now')
execute_wolframalpha()
if 'calculate' in command:
api_id = "Wolframalpha api id"
client = wolframalpha.Client(api_id)
idx = query.lower().split().index('calculate')
query = query.split()[idx + 1:]
res = client.query(' '.join(query))
answer = next(res.results).text
print("The result is " + answer)
tweety_speaks("The result is " + answer)
# most shooted commands/questions as per Google Assistant, Amazon Alexa, Windows Cortana
if 'time' in command:
str_time = datetime.datetime.now().strftime("%H:%M:%S")
tweety_speaks(f"the current time is {str_time}")
time.sleep(5)
if 'joke' in command or "tell me a joke" in command:
joke = pyjokes.get_joke()
tweety_speaks(joke)
print(joke)
if 'how are you' in command or 'how are you doing' in command:
how_are_you_responses = ["i am doing good thank you", "i am doing great indeed i feel better today",
"i am feeling happy as you are with me",
"i am good hope you are doing well too"]
s = random.choice(how_are_you_responses)
tweety_speaks(s)
print(s)
time.sleep(5)
if 'who are you' in command or 'what is your name' in command or 'what can you do' in command:
tweety_speaks(
'I am Tweety version 1 point O your personal virtual voice assistant. I am programmed to perform '
'minor tasks like opening youtube,google chrome,gmail, stackoverflow ,predict time,take a photo,'
'search wikipedia, predict weather in different cities , get top headline news from times of '
'india and you can also ask me computational or geographical questions too!')
if "who made you" in command or "who created you" in command or "who discovered you" in command or "reason for your invention" in command:
tweety_speaks("I have been created by Polu Tharun Kumar Reddy")
print("I have been created by Polu Tharun Kumar Reddy")
if "i love you" in command or "love" in command:
tweety_speaks("It is hard to understand")
if "will you be my gf" in command or "will you be my bf" in command:
tweety_speaks("I'm not sure about, may be you should give me some time")
if "who i am" in command or "who am i" in command:
tweety_speaks("If you can talk and think then definitely you are human")
if "why do you exist" in command or "why are you created" in command:
tweety_speaks("Thanks to Polu Tharun Kumar Reddy. further details is a top secret")
if 'what is love' in command:
tweety_speaks("It is 7th sense that destroy all other senses")
if "camera" in command or "take a photo" in command or "capture " in command:
ec.capture(0, "Tharun camera", "img.jpg")
if "empty recycle bin" in command:
winshell.recycle_bin().empty(confirm=False, show_progress=False, Sound=True)
tweety_speaks("Recycle Bin in clean now")
# system turnoff commands
if "restart" in command:
tweety_speaks("restarting the system")
execute_subprocess("/r")
if "hibernate" in command or "go to sleep" in command:
tweety_speaks("Hibernating...")
execute_subprocess('h')
if "lock screen" in command or "lock window" in command:
tweety_speaks("locking the current device")
ctypes.windll.user32.LockWorkStation()
if "good bye" in command or "exit" in command or "ok bye" in command or "stop" in command:
tweety_speaks('your personal assistant Tweety is shutting down,Good bye')
print('your personal assistant Tweety is shutting down,Good bye')
break
if "don't listen" in command or "stop listening" in command:
tweety_speaks("How much time you want me to halt from listening to you")
sleep_time = int(takeCommand)
time.sleep(sleep_time)
print("Stopped listening to your commands for" + sleep_time + "seconds")
if "log off" in command or "sign out" in command or "shut down" in command:
tweety_speaks(
"Hold on , your pc will log off in 10 sec make sure you exit from all currently working or "
"running applications")
execute_subprocess("/l")
# increment tweety usage count by 1 after every usage
usage_count += 1
# standard 5 seconds sleep time before next run
time.sleep(5)