Amazing-Python-Scripts
143 строки · 4.0 Кб
1import pyttsx32import datetime3import speech_recognition as sr4import pyaudio5import wikipedia6import webbrowser7import smtplib8import pywhatkit as kit9import pyjokes10import time11import sys12
13print("Your Assistant is starting......")14
15engine = pyttsx3.init('sapi5')16voices = engine.getProperty('voices')17engine.setProperty('voice', voices[1].id)18engine.setProperty('rate', 190)19
20# when you start using the assistant enter your name here
21Master = input("Enter your name: ")22
23
24def speak(audio):25engine.say(audio)26engine.runAndWait()27
28
29def wish_user():30hour = int(datetime.datetime.now().hour)31if hour >= 0 and hour < 12:32speak("Good Morning!" + Master)33
34elif hour >= 12 and hour < 16:35speak("Good Afternoon!" + Master)36
37else:38speak("Good Evening" + Master)39
40speak("I am your Desktop-Assistant! How may I help you?")41
42
43def user_command():44# takes microphone command and converts to string45
46r = sr.Recognizer()47with sr.Microphone() as source:48print("I am listening....")49r.pause_threshold = 150audio = r.listen(source)51
52try:53print("Recognising.....")54query = r.recognize_google(audio, language='en-in')55print(f"User said: {query}\n")56
57except Exception as e:58print("I am sorry I don't understand, Say that again please...")59return "None"60return query61
62
63def mailSent(to, content):64server = smtplib.SMTP('smtp.gmail.com', 587)65server.ehlo()66server.starttls()67# when you start working with the assistant, save this on your device68server.login('your email', 'your app password')69# check READme.md for creating an app password70server.sendmail('Your email', to, content)71
72server.close()73
74
75if __name__ == '__main__':76
77wish_user()78while True:79query = user_command().lower()80
81if 'wikipedia' in query:82speak('Give me sometime I am looking into Wikipedia')83query = query.replace("wikipedia", "")84results = wikipedia.summary(query, sentences=5)85speak("This is what I found!")86speak(results)87
88elif 'open youtube' in query:89webbrowser.open("youtube.com")90
91elif 'search google' in query:92webbrowser.open("google.com")93
94elif 'play music' in query:95webbrowser.open("spotify.com")96# you can use API as well, with the help of spotipy module97
98elif 'time' in query:99time = datetime.datetime.now().strftime("%H:%M")100speak(f"Its {time} now")101
102elif 'date today' in query:103date = datetime.datetime.today()104speak(f"Today is {date}")105
106elif 'send email' in query:107try:108speak("please tell me the content of the email")109content = user_command()110speak(content)111to = input()112speak(to)113mailSent(to, content)114speak(f"successfully sent the email to {to}")115except Exception as e:116print(e)117speak("sorry! i was unable to send the mail")118
119elif 'send whatsapp message' in query: # you should be logged in into whatsapp web for this120speak("To whom should I send the message?")121number = int(input())122speak("Tell me the message please")123message = user_command()124speak("At what time should I send?")125speak("At what time? (24 hours system)")126hr = int(input("Hours: "))127mins = int(input("Minutes: "))128kit.sendwhatmsg(number, message, hr, mins)129# this should be in the format ("+91xxxxxxxxxx","This is message", 15, 20)130
131elif 'open facebook' in query:132webbrowser.open("facebook.com")133
134elif 'make me laugh' in query:135joke = pyjokes.get_joke()136speak(joke)137
138elif 'no thanks' in query:139speak("thanks for using me! Have a good day")140sys.exit()141
142time.sleep(5)143speak("do you have any other work?")144