-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtelegram_bot.py
131 lines (98 loc) · 3.84 KB
/
telegram_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
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 os
import logging
from dotenv import load_dotenv
from telegram import Update
from telegram.ext import (
ContextTypes,
CommandHandler,
MessageHandler,
ApplicationBuilder,
filters,
)
from conf import users
from gpt_message_handler import handle_response
print("Starting up bot...")
load_dotenv()
TOKEN = os.getenv("TELEGRAM_TOKEN")
BOTNAME = os.getenv("BOTNAME")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
def get_message_content(message):
if message.text:
return message.text, "text"
elif message.photo:
return message.photo[-1].file_id, "photo"
elif message.document:
return message.document.file_id, "document"
elif message.voice:
return message.voice.file_id, "voice"
elif message.audio:
return message.audio.file_id, "audio"
elif message.video:
return message.video.file_id, "video"
else:
return None, "unknown"
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello there! I'm a bot. What's up?")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"Try typing anything and I will do my best to respond!"
)
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"This is a custom command, you can add whatever text you want here."
)
async def restart_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Restarted the bot.")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.effective_message
user = update.effective_user
message_id = message.message_id
logging.info(f"User {user.username} ({user.id}) sent a message.")
content, content_type = get_message_content(message)
if content_type == "unknown":
await message.reply_text("Sorry, I can't process this type of message yet.")
return
if content_type == "text":
response = await handle_response(content, user, message_id, content_type)
else:
# For media files, download the file
file = await context.bot.get_file(content)
file_path = await file.download_to_drive()
# Pass the file path to handle_response
response = await handle_response(file_path, user, message_id, content_type)
if type(response) == str and len(response) > 1000:
file_name = f"result.md"
with open(file_name, "w") as file:
file.write(response)
await message.reply_document(file_name)
if type(response) == str:
await message.reply_markdown(response)
else:
await message.reply_markdown(response)
def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE):
logging.error(msg="Exception while handling an update:", exc_info=context.error)
if __name__ == "__main__":
application = ApplicationBuilder().token(TOKEN).build()
# Commands with user restriction
allowed_users_filter = filters.User(username=users)
application.add_handler(
CommandHandler("start", start_command, filters=allowed_users_filter)
)
application.add_handler(
CommandHandler("help", help_command, filters=allowed_users_filter)
)
application.add_handler(
CommandHandler("custom", custom_command, filters=allowed_users_filter)
)
application.add_handler(
CommandHandler("restart", restart_command, filters=allowed_users_filter)
)
# Message handler with user restriction
application.add_handler(
MessageHandler(filters.ALL & allowed_users_filter, handle_message)
)
# Error handler
application.add_error_handler(error_handler)
application.run_polling()