-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
39 lines (31 loc) · 1.29 KB
/
bot.py
File metadata and controls
39 lines (31 loc) · 1.29 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
import os
import tempfile
import requests
import telebot
from dotenv import load_dotenv
from speech_to_text import speech_to_text
from summary_with_openai import summary_with_gpt3
load_dotenv()
API_TOKEN = os.getenv("TELEGRAM_TOKEN")
def start_bot():
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.send_message(message.chat.id, "Hi! Send me an audio and I will summarize it for you")
@bot.message_handler(content_types=['voice'])
def handle_voice(message):
file_id = message.voice.file_id
file_info = bot.get_file(file_id)
file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(API_TOKEN, file_info.file_path))
if not file:
return "bad request"
with tempfile.TemporaryDirectory() as tmpdirname:
bot.send_message(message.chat.id, "Summarizing the audio...")
audio_path = tmpdirname + file_id + ".oga"
with open(audio_path, 'wb') as audio_file:
audio_file.write(file.content)
transcription = speech_to_text(audio_path)
summary = summary_with_gpt3(transcription)
bot.send_message(message.chat.id, summary)
os.remove(audio_path)
bot.infinity_polling()