convert to python module
BiRabittoh andronacomarco@gmail.com
Fri, 31 May 2024 00:41:48 +0200
7 files changed,
72 insertions(+),
58 deletions(-)
M
.vscode/launch.json
→
.vscode/launch.json
@@ -6,7 +6,7 @@ "version": "0.2.0",
"configurations": [ { "name": "Python: file corrente", - "type": "python", + "type": "debugpy", "request": "launch", "program": "${file}", "console": "integratedTerminal",@@ -14,9 +14,9 @@ "justMyCode": true
}, { "name": "Python: start bot", - "type": "python", + "type": "debugpy", "request": "launch", - "program": "main.py", + "module": "python-meme-bot.bot", "console": "integratedTerminal", "justMyCode": true }
M
Constants.py
→
python-meme-bot/constants.py
@@ -1,4 +1,4 @@
-from telegram import InlineKeyboardMarkup, InlineKeyboardButton +from telegram import InlineKeyboardMarkup, InlineKeyboardButton, User, Chat from telegram.ext import CallbackContext import logging@@ -85,11 +85,13 @@
N = 2 lang_markup = InlineKeyboardMarkup([buttons[n:n+N] for n in range(0, len(buttons), N)]) -def format_author(user): - +def format_author(user: User): if user.username is not None: return user.full_name + f" ({user.username})" return user.full_name + +def format_chat(chat: Chat): + return chat.title + ("" if chat.username is None else f" ({chat.username})") def get_lang(context: CallbackContext): try:
M
Slot.py
→
python-meme-bot/slot.py
@@ -1,8 +1,7 @@
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import CallbackContext from datetime import date -from Constants import get_localized_string as l -import Constants as c +from .constants import format_author, slot_machine_value, win_table, get_localized_string as l autospin_cap = 20 lastreset_default = date(1970, 1, 1)@@ -144,7 +143,7 @@ bet = set_bet(context, int(amount * 100))
else: bet = get_bet(context) - result = l("current_bet", context).format(c.format_author(update.effective_user), bet / 100) + result = l("current_bet", context).format(format_author(update.effective_user), bet / 100) return await context.bot.send_message(chat_id=update.effective_chat.id, text=result)@@ -159,23 +158,23 @@ if lastreset < today:
set_lastreset(context, today) cash = set_cash(context, cash_default) - result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100) + result = l("cash_reset", context).format(format_author(update.effective_user), cash / 100) return update.message.reply_text(text=result) else: return update.message.reply_text( - text=l("cash_reset_fail", context).format(c.format_author(update.effective_user), cash)) + text=l("cash_reset_fail", context).format(format_author(update.effective_user), cash)) - result = l("current_cash", context).format(c.format_author(update.effective_user), cash) + result = l("current_cash", context).format(format_author(update.effective_user), cash) return update.message.reply_text(text=result) def get_multiplier(value: int): try: - values = c.slot_machine_value[value] + values = slot_machine_value[value] except IndexError: - values = c.slot_machine_value[5] + values = slot_machine_value[5] try: - return c.win_table[values] + return win_table[values] except KeyError: return 0
M
main.py
→
python-meme-bot/bot.py
@@ -4,15 +4,15 @@ from io import BytesIO
from PIL import Image from dotenv import load_dotenv -from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, Message from telegram.error import TelegramError -from telegram.ext import ApplicationBuilder, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, \ - PicklePersistence, filters, PersistenceInput +from telegram.ext import ApplicationBuilder, CallbackQueryHandler, CommandHandler, MessageHandler, \ + PicklePersistence, filters, PersistenceInput, ContextTypes -from Api import get_random_image -from Constants import get_localized_string as l, format_author, format_lang, langs, get_lang, lang_markup -from Effects import img_to_bio, tt_bt_effect, bt_effect, splash_effect, wot_effect, text_effect -from Slot import spin, autospin, bet, cash +from .api import get_random_image +from .constants import format_chat, get_localized_string as l, format_author, format_lang, langs, get_lang, lang_markup +from .effects import img_to_bio, tt_bt_effect, bt_effect, splash_effect, wot_effect, text_effect +from .slot import spin, autospin, bet, cash load_dotenv() logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)@@ -116,11 +116,11 @@
return content, image, markup -def start(update: Update, context: CallbackContext): - context.bot.send_message(chat_id=update.effective_chat.id, text=l("welcome", context)) +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): + await update.message.reply_text(l("welcome", context)) -async def set_lewd(update: Update, context: CallbackContext): +async def set_lewd(update: Update, context: ContextTypes.DEFAULT_TYPE): try: output = False if context.chat_data["lewd"] else True except KeyError:@@ -128,26 +128,40 @@ output = True
context.chat_data['lewd'] = output message = l("lewd_toggle", context).format(l("enabled", context) if output else l("disabled", context)) - - return await context.bot.send_message(chat_id=update.effective_chat.id, text=message) + return await update.message.reply_text(message) -async def pic(update: Update, context: CallbackContext): +async def pic(update: Update, context: ContextTypes.DEFAULT_TYPE): image, markup = _get_image(context) return await update.message.reply_photo(photo=img_to_bio(image), parse_mode="markdown", reply_markup=markup) -def _get_author(message): - if message.forward_from is not None: - return format_author(message.forward_from) - - if message.forward_sender_name is not None: - return message.forward_sender_name - - if message.forward_from_chat is not None: - return message.forward_from_chat.title + ( - "" if message.forward_from_chat.username is None else f" ({message.forward_from_chat.username})") - +def _get_author(message: Message): + origin = message.forward_origin + + if origin is None: # message was not forwarded + return format_author(message.from_user) + + try: + return format_author(origin['sender_user']) # MessageOriginUser + except KeyError: + pass + + try: + return origin['sender_user_name'] # MessageOriginHiddenUser + except KeyError: + pass + + try: + format_chat(origin['sender_chat']) # MessageOriginChat + except KeyError: + pass + try: + format_chat(origin['chat']) # MessageOriginChannel + except KeyError: + pass + + logging.warn("Message was forwarded but I couldn't detect the original author.") return format_author(message.from_user)@@ -207,7 +221,7 @@
return input_text -async def ttbt(update: Update, context: CallbackContext): +async def ttbt(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, ttbt_check, context) if image is None:@@ -221,7 +235,7 @@
return await update.message.reply_photo(photo=image, reply_markup=markup) -async def tt(update: Update, context: CallbackContext): +async def tt(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, tt_check, context) if image is None:@@ -235,7 +249,7 @@
return await update.message.reply_photo(photo=image, reply_markup=markup) -async def bt(update: Update, context: CallbackContext): +async def bt(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, tt_check, context) if image is None:@@ -249,7 +263,7 @@
return await update.message.reply_photo(photo=image, reply_markup=markup) -async def splash(update: Update, context: CallbackContext): +async def splash(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, splash_check, context) if image is None:@@ -263,7 +277,7 @@
return await update.message.reply_photo(photo=image, reply_markup=markup) -async def wot(update: Update, context: CallbackContext): +async def wot(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, wot_check, context) if image is None:@@ -277,7 +291,7 @@
await update.message.reply_photo(photo=image, reply_markup=markup) -async def text(update: Update, context: CallbackContext): +async def text(update: Update, context: ContextTypes.DEFAULT_TYPE): content, image, markup = await _get_all(update, wot_check, context) if image is None:@@ -292,18 +306,18 @@
await update.message.reply_photo(photo=image, reply_markup=markup) -async def caps(update: Update, context: CallbackContext): +async def caps(update: Update, context: ContextTypes.DEFAULT_TYPE): _, reply, _ = await _get_reply(update.message.reply_to_message, ' '.join(context.args)) - await context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper()) + await update.message.reply_text(reply.upper()) -async def _set_lang(update: Update, context: CallbackContext, lang: str): +async def _set_lang(update: Update, context: ContextTypes.DEFAULT_TYPE, lang: str): context.chat_data["lang"] = lang - await context.bot.send_message(chat_id=update.effective_chat.id, - text=l("language_set", context).format(format_lang(lang))) + response = l("language_set", context).format(format_lang(lang)) + await update.message.reply_text(response) -async def lang(update: Update, context: CallbackContext): +async def lang(update: Update, context: ContextTypes.DEFAULT_TYPE): try: selected = str(context.args[0]) except IndexError:@@ -321,17 +335,16 @@
return await _set_lang(update, context, selected) -def unknown(update: Update, context: CallbackContext): - logging.info( - f"User {update.message.from_user.full_name} sent {update.message.text_markdown_v2} and I don't know what that means.") +def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE): + logging.info(f"User {update.message.from_user.full_name} sent {update.message.text_markdown_v2} and I don't know what that means.") -async def error_callback(update: Update, context: CallbackContext): +async def error_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): try: raise context.error except TelegramError as e: logging.error("TelegramError! " + str(e)) - await context.bot.send_message(chat_id=update.effective_chat.id, text=l('error', context)) + await update.message.reply_text(l('error', context)) def _add_effect_handler(application: ApplicationBuilder, command: str, callback):@@ -339,7 +352,7 @@ application.add_handler(CommandHandler(command, callback))
application.add_handler(MessageHandler(filters.Caption([f"/{command}"]), callback)) -async def keyboard_handler(update: Update, context: CallbackContext): +async def keyboard_handler(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.callback_query data = query.data@@ -398,7 +411,7 @@ application.add_handler(CommandHandler('bet', bet))
application.add_handler(CommandHandler('cash', cash)) # fallback - application.add_handler(MessageHandler(filters.Command(), unknown)) + application.add_handler(MessageHandler(filters.COMMAND, unknown)) application.run_polling()