-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (42 loc) · 1.6 KB
/
main.py
File metadata and controls
56 lines (42 loc) · 1.6 KB
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
import telepot
from gtts import gTTS
import speech_recognition as sr
# Replace 'YOUR_BOT_TOKEN' with your actual Telegram bot token
bot = telepot.Bot('YOUR_BOT_TOKEN')
def handle(msg):
chat_id = msg['chat']['id']
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'voice':
file_id = msg['voice']['file_id']
file_info = bot.getFile(file_id)
file_path = file_info['file_path']
# Download the voice message
voice_file = bot.download_file(file_path)
with open('voice_message.ogg', 'wb') as file:
file.write(voice_file)
# Convert speech to text
recognizer = sr.Recognizer()
with sr.AudioFile('voice_message.ogg') as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data)
bot.sendMessage(chat_id, f'Text from voice: {text}')
# Convert text to speech
tts = gTTS(text=text, lang='en')
tts.save('text_to_speech.mp3')
# Send the audio file
with open('text_to_speech.mp3', 'rb') as audio_file:
bot.sendAudio(chat_id, audio_file)
elif content_type == 'text':
text_message = msg['text']
# Convert text to speech
tts = gTTS(text=text_message, lang='en')
tts.save('text_to_speech.mp3')
# Send the audio file
with open('text_to_speech.mp3', 'rb') as audio_file:
bot.sendAudio(chat_id, audio_file)
# Set the bot's message handler
bot.message_loop(handle)
print('Bot is listening...')
# Keep the program running
while True:
pass