-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhotfix_bot.py
More file actions
83 lines (68 loc) · 2.95 KB
/
Copy pathhotfix_bot.py
File metadata and controls
83 lines (68 loc) · 2.95 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
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
import telebot
import hashlib
import os
import psycopg2 as postgres
import json
with open("/app/configs/telebot_token.txt", 'r') as f:
TELEGRAM_TOKEN = f.read().strip()
bot = telebot.TeleBot(TELEGRAM_TOKEN)
with open("/app/configs/db_config.json", 'r') as f:
db_config = json.load(f)
con = postgres.connect(
host=db_config["host"],
port=db_config["port"],
user=db_config["user"],
password=db_config["password"],
database=db_config["database"],
)
cur = con.cursor()
allowed_users = [361380081, 763104478, 1175315313]
media_path = "/app/pages/videos/uploaded"
cur_photoes = {}
@bot.message_handler(commands=['start'])
def start(message):
if message.from_user.id not in allowed_users:
bot.send_message(message.chat.id, f"{message.from_user.id} is not allowed to use this bot, chao.")
return
bot.send_message(message.chat.id, "Welcome to the manual bot! use /add_publisher to add publisher or send me a photo to add avatar to somebody.")
@bot.message_handler(commands=['add_publisher'])
def add_publisher(message):
if message.from_user.id not in allowed_users:
bot.send_message(message.chat.id, "You are not allowed to use this command.")
return
if len(message.text.split()) < 2:
bot.send_message(message.chat.id, "Usage: /add_publisher <username>")
return
username = ' '.join(message.text.split()[1:])
cur.execute('INSERT INTO "user" (username, passwdhash, userrights) VALUES (%s, 0, \'public_author\')', (username,))
con.commit()
bot.send_message(message.chat.id, f"Publisher {username} added successfully.")
@bot.message_handler(content_types=['photo'])
def handle_photo(message: telebot.types.Message):
if message.from_user.id not in allowed_users:
bot.send_message(message.chat.id, "You are not allowed to use this bot.")
return
file_info = bot.get_file(message.photo[-1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
file_name = os.path.join("/app/pages/images/uploaded", f"{message.photo[-1].file_id}.jpg")
with open(file_name, 'wb') as f:
f.write(downloaded_file)
file_name = os.path.join("/images/uploaded", f"{message.photo[-1].file_id}.jpg")
bot.send_message(message.chat.id, f"Photo saved successfully to {file_name}.")
bot.send_message(message.chat.id, "Please provide the user ID this photo is for.")
global cur_photoes
cur_photoes[message.chat.id] = file_name
bot.register_next_step_handler(message, handle_user_id)
def handle_user_id(response: telebot.types.Message):
username = response.text.strip()
cur.execute (
'update "user" set avatar_pic = %s where username = %s', (cur_photoes[response.chat.id], username)
)
con.commit()
del cur_photoes[response.chat.id]
bot.send_message(response.chat.id, f"User ID {username} recorded for photo.")
try:
bot.polling(none_stop=True)
except Exception as e:
print(f"Bot polling failed: {e}")
bot.stop_polling()