-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
65 lines (50 loc) · 2.5 KB
/
bot.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
# Works with any video calling software as we are using the audio source.
# Let me know how it goes on instagram: @ashutoshshukla__
import speech_recognition as sr
import sys
import requests
import pprint
pp = pprint.PrettyPrinter()
# Enter your discord webhook URL of the bot.
WEBURL = '<<---Enter Here-->>'
# Initialize the speech recognition module.
r = sr.Recognizer()
# Lists All the input and output audio devices.
pp.pprint(sr.Microphone.list_microphone_names())
# Selecting the Realtek Stereo Mix input.
# The Index number may vary, check the list from above and find the device index which starts from 0.
mic = sr.Microphone(device_index=2)
# Array of all the words we will match from the heard text, for example "present","attendance","student names",etc.
matches = ["attendance", "present", "present sir",
"present ma'am", "Enter all your friends names XD"]
# These send a message to Discord that the bot is online and listening.
requests.post(WEBURL, data={"content": 'Im online and listening!'})
requests.post(
WEBURL, data={"content": f'Listening for following - {matches}'})
# Infinite loop, can be broken by entering Ctrl+C in terminal.
while True:
with mic as source:
try:
# Listening from the selected mics input.
r.adjust_for_ambient_noise(source, duration=0.2)
print('Listening...')
# Setting the phrase limit to 6 and converting speech to text in lowercase because its easier to compare.
audio = r.listen(source, phrase_time_limit=6)
text = r.recognize_google(audio).lower()
# Prints the matched line and sends the message to discord.
print(f'Heard Text: {text}')
if any(x in text for x in matches):
print(f'Matched Text: {text}')
requests.post(
WEBURL, data={"content": f'Shits going down!\nI heard: {text}'})
# If no one is talking or input audio is not clear this message will be persistent until something is heard.
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
# Any other unkown error will be speciefed by this.
except sr.RequestError as e:
print(
"Could not request results from Google Speech Recognition service; {0}".format(e))
# To exit the program.
except (KeyboardInterrupt, SystemExit):
requests.post(WEBURL, data={"content": 'Offline now!'})
sys.exit('User Ctrl+C quit')