Amazing-Python-Scripts

Форк
0
143 строки · 4.0 Кб
1
import pyttsx3
2
import datetime
3
import speech_recognition as sr
4
import pyaudio
5
import wikipedia
6
import webbrowser
7
import smtplib
8
import pywhatkit as kit
9
import pyjokes
10
import time
11
import sys
12

13
print("Your Assistant is starting......")
14

15
engine = pyttsx3.init('sapi5')
16
voices = engine.getProperty('voices')
17
engine.setProperty('voice', voices[1].id)
18
engine.setProperty('rate', 190)
19

20
# when you start using the assistant enter your name here
21
Master = input("Enter your name: ")
22

23

24
def speak(audio):
25
    engine.say(audio)
26
    engine.runAndWait()
27

28

29
def wish_user():
30
    hour = int(datetime.datetime.now().hour)
31
    if hour >= 0 and hour < 12:
32
        speak("Good Morning!" + Master)
33

34
    elif hour >= 12 and hour < 16:
35
        speak("Good Afternoon!" + Master)
36

37
    else:
38
        speak("Good Evening" + Master)
39

40
    speak("I am your Desktop-Assistant! How may I help you?")
41

42

43
def user_command():
44
    # takes microphone command and converts to string
45

46
    r = sr.Recognizer()
47
    with sr.Microphone() as source:
48
        print("I am listening....")
49
        r.pause_threshold = 1
50
        audio = r.listen(source)
51

52
    try:
53
        print("Recognising.....")
54
        query = r.recognize_google(audio, language='en-in')
55
        print(f"User said: {query}\n")
56

57
    except Exception as e:
58
        print("I am sorry I don't understand, Say that again please...")
59
        return "None"
60
    return query
61

62

63
def mailSent(to, content):
64
    server = smtplib.SMTP('smtp.gmail.com', 587)
65
    server.ehlo()
66
    server.starttls()
67
    # when you start working with the assistant, save this on your device
68
    server.login('your email', 'your app password')
69
    # check READme.md for creating an app password
70
    server.sendmail('Your email', to, content)
71

72
    server.close()
73

74

75
if __name__ == '__main__':
76

77
    wish_user()
78
    while True:
79
        query = user_command().lower()
80

81
        if 'wikipedia' in query:
82
            speak('Give me sometime I am looking into Wikipedia')
83
            query = query.replace("wikipedia", "")
84
            results = wikipedia.summary(query, sentences=5)
85
            speak("This is what I found!")
86
            speak(results)
87

88
        elif 'open youtube' in query:
89
            webbrowser.open("youtube.com")
90

91
        elif 'search google' in query:
92
            webbrowser.open("google.com")
93

94
        elif 'play music' in query:
95
            webbrowser.open("spotify.com")
96
            # you can use API as well, with the help of spotipy module
97

98
        elif 'time' in query:
99
            time = datetime.datetime.now().strftime("%H:%M")
100
            speak(f"Its {time} now")
101

102
        elif 'date today' in query:
103
            date = datetime.datetime.today()
104
            speak(f"Today is {date}")
105

106
        elif 'send email' in query:
107
            try:
108
                speak("please tell me the content of the email")
109
                content = user_command()
110
                speak(content)
111
                to = input()
112
                speak(to)
113
                mailSent(to, content)
114
                speak(f"successfully sent the email to {to}")
115
            except Exception as e:
116
                print(e)
117
                speak("sorry! i was unable to send the mail")
118

119
        elif 'send whatsapp message' in query:  # you should be logged in into whatsapp web for this
120
            speak("To whom should I send the message?")
121
            number = int(input())
122
            speak("Tell me the message please")
123
            message = user_command()
124
            speak("At what time should I send?")
125
            speak("At what time? (24 hours system)")
126
            hr = int(input("Hours: "))
127
            mins = int(input("Minutes: "))
128
            kit.sendwhatmsg(number, message, hr, mins)
129
            # this should be in the format ("+91xxxxxxxxxx","This is message", 15, 20)
130

131
        elif 'open facebook' in query:
132
            webbrowser.open("facebook.com")
133

134
        elif 'make me laugh' in query:
135
            joke = pyjokes.get_joke()
136
            speak(joke)
137

138
        elif 'no thanks' in query:
139
            speak("thanks for using me! Have a good day")
140
            sys.exit()
141

142
        time.sleep(5)
143
        speak("do you have any other work?")
144

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.