-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alexa.py
131 lines (107 loc) · 3.27 KB
/
Alexa.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
import speech_recognition as sr
import pyttsx3
import pywhatkit
from datetime import datetime
import wikipedia
import pyjokes
import webbrowser
import pyautogui
import os
import random
import time
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty("voice",voices[1].id)
Alive = True
def talk(text):
engine.say(text)
engine.runAndWait()
def take_command():
try:
with sr.Microphone() as source:
print("Listening....")
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if "alexa" in command:
print(command)
except:
print("say that again")
talk("Say that again")
return "None"
return command
def wish_me():
hour = int(datetime.now().hour)
if hour >=0 and hour < 12 :
talk("Good Morning")
elif hour >=12 and hour <18 :
talk("Good Afternoon")
else:
talk("Good Night")
talk("I am Alexa. I am your dekstop assistant. Tell me how can i help you")
def run_alexa():
command = take_command()
if "play" in command:
song = command.replace("play","")
talk("playing")
print("playing....")
pywhatkit.playonyt(song)
elif "time" in command:
time = datetime.now().strftime("%I:%M %p")
talk(f"Now it is {time}")
print(f"Now it is {time}")
elif "wikipedia" in command:
person = command.replace("wikipedia","")
info = wikipedia.summary(person,5)
print(info)
talk(info)
elif "How are you" in command:
talk("I am fine.Thank you.How are you")
print("I am fine.Thank you.How are you")
if "doing good" or "fine" in command:
print("Thats great")
talk("Thats great")
else:
pass
elif "joke" in command:
talk(pyjokes.get_joke())
elif "open google" in command:
webbrowser.open("www.google.com")
if "write" in command:
pyautogui.typewrite(command)
pyautogui.press("enter")
else:
pass
elif "open youtube" in command:
webbrowser.open("www.youtube.com")
if "write" in command:
pyautogui.typewrite(command)
pyautogui.press("enter")
else:
pass
elif "music on" in command:
music_dir = "F:\\Music\\english"
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir,songs[random.randint(0,29)]))
elif "who has made you" in command:
talk("My tutor and mentor is Tanvir Rashik Shafim")
print("My tutor and mentor is Tanvir Rashik Shafim")
elif "your age" in command:
date = "17/01/2021"
birthday = datetime.strptime(date, "%d/%m/%Y")
now = datetime.now()
current_time = datetime(now.year, now.month, now.day)
Age = (current_time - birthday).days//365
print(f"I am {Age} years old")
elif "Stop" in command:
talk("Thank you for using me")
Alive = False
else:
talk("Sorry i didnt understand")
talk("Please say it again")
wish_me()
while Alive is True:
run_alexa()
#So it is the end.Thank you...