Files
furrybot/main.py
2025-10-30 19:06:36 +03:00

240 lines
9.2 KiB
Python

# main.py
import logging
import localization as lang
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
import os
import sqlite3
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
DB_PATH = 'furry.db'
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
cursor = conn.cursor()
def init_db():
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_lang (
chatid INTEGER PRIMARY KEY,
lang TEXT DEFAULT 'no'
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS filters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chatid INTEGER,
trigger TEXT,
content_type TEXT,
text_content TEXT,
file_id TEXT,
caption TEXT,
UNIQUE(chatid, trigger)
)
''')
conn.commit()
init_db()
print(lang.getstr("ru", "test"))
print(lang.getstr("en", "test"))
tokenfile = open("../token.txt", "r")
bottoken = str(tokenfile.read())
tokenfile.close()
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
chat = update.effective_chat
chat_id = chat.id
try:
cursor.execute("SELECT lang FROM user_lang WHERE chatid = ?", (chat_id,))
row = cursor.fetchone()
if row[0] == "ru":
await update.message.reply_text(lang.getstr("ru", "test2"))
elif row[0] == "en":
await update.message.reply_text(lang.getstr("en", "test"))
else:
await update.message.reply_text('Select your language using "/lang en" command.\n\nВыберите ваш язык командой "/lang ru".')
except:
await update.message.reply_text('Select your language using "/lang en" command.\n\nВыберите ваш язык командой "/lang ru".')
cursor.execute("INSERT INTO user_lang (chatid, lang) VALUES (?, ?)", (chat_id, "no"))
conn.commit()
async def set_lang(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
chat = update.effective_chat
chat_id = chat.id
try:
userlang = str(context.args[0])
try:
cursor.execute("INSERT INTO user_lang (chatid, lang) VALUES (?, ?)", (chat_id, userlang))
except:
pass
cursor.execute("UPDATE user_lang SET lang = ? WHERE chatid = ?", (userlang, chat_id))
conn.commit()
cursor.execute("SELECT lang FROM user_lang WHERE chatid = ?", (chat_id,))
row = cursor.fetchone()
await update.message.reply_text(lang.getstr(row[0], "succlang"))
except:
await update.message.reply_text('/lang en\n/lang ru')
async def filtercmd(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
chat_id = chat.id
cursor.execute("SELECT lang FROM user_lang WHERE chatid = ?", (chat_id,))
row = cursor.fetchone()
user_lang = row[0] if row else "en"
if not context.args:
cursor.execute("SELECT trigger, content_type FROM filters WHERE chatid = ?", (chat_id,))
filters_list = cursor.fetchall()
if filters_list:
filter_text = lang.getstr(user_lang, "filter_list") + "\n"
for i, (trigger, content_type) in enumerate(filters_list, 1):
type_emoji = {
'text': '📝',
'photo': '🖼',
'video': '🎥',
'document': '📎',
'audio': '🎵',
'voice': '🎤',
'sticker': '🤡'
}.get(content_type, '')
filter_text += f"{i}. {type_emoji} {trigger}\n"
await update.message.reply_text(filter_text)
else:
await update.message.reply_text(lang.getstr(user_lang, "no_filters"))
return
action = context.args[0].lower()
if action == "set":
if len(context.args) < 2:
await update.message.reply_text(lang.getstr(user_lang, "filter_set_usage"))
return
if not update.message.reply_to_message:
await update.message.reply_text(lang.getstr(user_lang, "filter_reply_required"))
return
trigger = context.args[1].lower()
replied_message = update.message.reply_to_message
content_type = 'text'
text_content = None
file_id = None
caption = None
if replied_message.text:
content_type = 'text'
text_content = replied_message.text
elif replied_message.photo:
content_type = 'photo'
file_id = replied_message.photo[-1].file_id
caption = replied_message.caption
elif replied_message.video:
content_type = 'video'
file_id = replied_message.video.file_id
caption = replied_message.caption
elif replied_message.document:
content_type = 'document'
file_id = replied_message.document.file_id
caption = replied_message.caption
elif replied_message.audio:
content_type = 'audio'
file_id = replied_message.audio.file_id
caption = replied_message.caption
elif replied_message.voice:
content_type = 'voice'
file_id = replied_message.voice.file_id
elif replied_message.sticker:
content_type = 'sticker'
file_id = replied_message.sticker.file_id
else:
await update.message.reply_text(lang.getstr(user_lang, "filter_unsupported_type"))
return
try:
cursor.execute(
"INSERT INTO filters (chatid, trigger, content_type, text_content, file_id, caption) VALUES (?, ?, ?, ?, ?, ?)",
(chat_id, trigger, content_type, text_content, file_id, caption)
)
conn.commit()
await update.message.reply_text(lang.getstr(user_lang, "filter_set_success").format(trigger=trigger))
except sqlite3.IntegrityError:
await update.message.reply_text(lang.getstr(user_lang, "filter_already_exists").format(trigger=trigger))
elif action == "remove":
if len(context.args) < 2:
await update.message.reply_text(lang.getstr(user_lang, "filter_remove_usage"))
return
trigger = context.args[1].lower()
cursor.execute("DELETE FROM filters WHERE chatid = ? AND trigger = ?", (chat_id, trigger))
conn.commit()
if cursor.rowcount > 0:
await update.message.reply_text(lang.getstr(user_lang, "filter_remove_success").format(trigger=trigger))
else:
await update.message.reply_text(lang.getstr(user_lang, "filter_not_found").format(trigger=trigger))
else:
await update.message.reply_text(lang.getstr(user_lang, "filter_usage"))
async def handle_messages(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.message.text and update.message.text.startswith('/'):
return
if update.message.from_user.is_bot:
return
chat = update.effective_chat
chat_id = chat.id
message_text = update.message.text or update.message.caption or ""
if not message_text:
return
cursor.execute("SELECT trigger, content_type, text_content, file_id, caption FROM filters WHERE chatid = ?", (chat_id,))
filters_list = cursor.fetchall()
for trigger, content_type, text_content, file_id, caption in filters_list:
if trigger.lower() in message_text.lower():
try:
if content_type == 'text':
await update.message.reply_text(text_content)
elif content_type == 'photo':
await update.message.reply_photo(file_id, caption=caption)
elif content_type == 'video':
await update.message.reply_video(file_id, caption=caption)
elif content_type == 'document':
await update.message.reply_document(file_id, caption=caption)
elif content_type == 'audio':
await update.message.reply_audio(file_id, caption=caption)
elif content_type == 'voice':
await update.message.reply_voice(file_id)
elif content_type == 'sticker':
await update.message.reply_sticker(file_id)
break # Only trigger one filter per message
except Exception as e:
logging.error(f"Error sending media: {e}")
await update.message.reply_text(lang.getstr("en", "filter_send_error"))
break
def main() -> None:
application = Application.builder().token(bottoken).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("lang", set_lang))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_messages))
application.add_handler(CommandHandler("filter", filtercmd))
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()