-
Notifications
You must be signed in to change notification settings - Fork 142
/
main.py
177 lines (142 loc) · 6.23 KB
/
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
import requests
from functions.online_ops import find_my_ip, get_latest_news, get_random_advice, get_random_joke, get_trending_movies, get_weather_report, play_on_youtube, search_on_google, search_on_wikipedia, send_email, send_whatsapp_message
import pyttsx3
import speech_recognition as sr
from decouple import config
from datetime import datetime
from functions.os_ops import open_calculator, open_camera, open_cmd, open_notepad, open_discord
from random import choice
from utils import opening_text
from pprint import pprint
USERNAME = config('USER')
BOTNAME = config('BOTNAME')
engine = pyttsx3.init('sapi5')
# Set Rate
engine.setProperty('rate', 190)
# Set Volume
engine.setProperty('volume', 1.0)
# Set Voice (Female)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# Text to Speech Conversion
def speak(text):
"""Used to speak whatever text is passed to it"""
engine.say(text)
engine.runAndWait()
# Greet the user
def greet_user():
"""Greets the user according to the time"""
hour = datetime.now().hour
if (hour >= 6) and (hour < 12):
speak(f"Good Morning {USERNAME}")
elif (hour >= 12) and (hour < 16):
speak(f"Good afternoon {USERNAME}")
elif (hour >= 16) and (hour < 19):
speak(f"Good Evening {USERNAME}")
speak(f"I am {BOTNAME}. How may I assist you?")
# Takes Input from User
def take_user_input():
"""Takes user input, recognizes it using Speech Recognition module and converts it into text"""
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening....')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language='en-in')
if not 'exit' in query or 'stop' in query:
speak(choice(opening_text))
else:
hour = datetime.now().hour
if hour >= 21 and hour < 6:
speak("Good night sir, take care!")
else:
speak('Have a good day sir!')
exit()
except Exception:
speak('Sorry, I could not understand. Could you please say that again?')
query = 'None'
return query
if __name__ == '__main__':
greet_user()
while True:
query = take_user_input().lower()
if 'open notepad' in query:
open_notepad()
elif 'open discord' in query:
open_discord()
elif 'open command prompt' in query or 'open cmd' in query:
open_cmd()
elif 'open camera' in query:
open_camera()
elif 'open calculator' in query:
open_calculator()
elif 'ip address' in query:
ip_address = find_my_ip()
speak(f'Your IP Address is {ip_address}.\n For your convenience, I am printing it on the screen sir.')
print(f'Your IP Address is {ip_address}')
elif 'wikipedia' in query:
speak('What do you want to search on Wikipedia, sir?')
search_query = take_user_input().lower()
results = search_on_wikipedia(search_query)
speak(f"According to Wikipedia, {results}")
speak("For your convenience, I am printing it on the screen sir.")
print(results)
elif 'youtube' in query:
speak('What do you want to play on Youtube, sir?')
video = take_user_input().lower()
play_on_youtube(video)
elif 'search on google' in query:
speak('What do you want to search on Google, sir?')
query = take_user_input().lower()
search_on_google(query)
elif "send whatsapp message" in query:
speak(
'On what number should I send the message sir? Please enter in the console: ')
number = input("Enter the number: ")
speak("What is the message sir?")
message = take_user_input().lower()
send_whatsapp_message(number, message)
speak("I've sent the message sir.")
elif "send an email" in query:
speak("On what email address do I send sir? Please enter in the console: ")
receiver_address = input("Enter email address: ")
speak("What should be the subject sir?")
subject = take_user_input().capitalize()
speak("What is the message sir?")
message = take_user_input().capitalize()
if send_email(receiver_address, subject, message):
speak("I've sent the email sir.")
else:
speak("Something went wrong while I was sending the mail. Please check the error logs sir.")
elif 'joke' in query:
speak(f"Hope you like this one sir")
joke = get_random_joke()
speak(joke)
speak("For your convenience, I am printing it on the screen sir.")
pprint(joke)
elif "advice" in query:
speak(f"Here's an advice for you, sir")
advice = get_random_advice()
speak(advice)
speak("For your convenience, I am printing it on the screen sir.")
pprint(advice)
elif "trending movies" in query:
speak(f"Some of the trending movies are: {get_trending_movies()}")
speak("For your convenience, I am printing it on the screen sir.")
print(*get_trending_movies(), sep='\n')
elif 'news' in query:
speak(f"I'm reading out the latest news headlines, sir")
speak(get_latest_news())
speak("For your convenience, I am printing it on the screen sir.")
print(*get_latest_news(), sep='\n')
elif 'weather' in query:
ip_address = find_my_ip()
city = requests.get(f"https://ipapi.co/{ip_address}/city/").text
speak(f"Getting weather report for your city {city}")
weather, temperature, feels_like = get_weather_report(city)
speak(f"The current temperature is {temperature}, but it feels like {feels_like}")
speak(f"Also, the weather report talks about {weather}")
speak("For your convenience, I am printing it on the screen sir.")
print(f"Description: {weather}\nTemperature: {temperature}\nFeels like: {feels_like}")