add more inline buttons
Marco Andronaco andronacomarco@gmail.com
Mon, 12 Sep 2022 22:32:05 +0200
3 files changed,
50 insertions(+),
21 deletions(-)
M
Constants.py
→
Constants.py
@@ -1,5 +1,6 @@
+from telegram import InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import CallbackContext import logging -from telegram.ext import CallbackContext localization = { 'en': {@@ -25,7 +26,7 @@ 'current_cash': "{}, you currently have {:0.2f}$ in your account.",
'cash_reset': "{}, your cash has been reset to {:0.2f}$. You can do this once per day.", 'cash_reset_fail': "{}, you have 0$ in your account and you cannot reset it today. Come back tomorrow.", 'no_autospin': "Sorry, multiple spins are disabled in group chats.", - 'current_language': "Current language: {}.\nChoices: {}\nType \"/lang <code>\" to change it.", + 'current_language': "Current language: {}.\nChoices: {}\nTo change it, type \"/lang <code>\" or use one of the buttons below.", 'invalid_language': "Invalid language.", 'language_set': "Language set: {}", },@@ -52,7 +53,7 @@ 'current_cash': "{}, il tuo saldo attuale è {:0.2f}€.",
'cash_reset': "{}, il tuo saldo è stato ripristinato a {:0.2f}€. Puoi farlo una volta al giorno.", 'cash_reset_fail': "{}, il tuo saldo è 0€ e non puoi più resettarlo oggi. Riprova domani.", 'no_autospin': "Gli spin multipli sono disabilitati nelle chat di gruppo.", - 'current_language': "Lingua attuale: {}.\nAltre lingue: {}\nScrivi \"/lang <codice>\" per cambiarla.", + 'current_language': "Lingua attuale: {}.\nAltre lingue: {}\nPer cambiarla, scrivi \"/lang <codice>\" o usa uno dei tasti qui sotto.", 'invalid_language': "Questa lingua non esiste.", 'language_set': "Lingua impostata: {}", },@@ -61,20 +62,28 @@ langs = localization.keys()
default_lang = "en" n_entries = len(localization[default_lang].keys()) + +#markup = InlineKeyboardMarkup([[button, button][button, button]]) + +def format_lang(lang: str): + try: + return f"{localization[lang]['name']} {localization[lang]['emoji']}" + except KeyError: + return 'Unknown' + +buttons = [] for i in langs: assert(n_entries == len(localization[i].keys())) + buttons.append(InlineKeyboardButton(text=format_lang(i), callback_data=f"set_lang_{i}")) + +N = 2 +lang_markup = InlineKeyboardMarkup([buttons[n:n+N] for n in range(0, len(buttons), N)]) def format_author(user): if user.username is not None: return user.full_name + f" ({user.username})" return user.full_name - -def format_lang(lang: str): - try: - return f"{localization[lang]['name']} {localization[lang]['emoji']}" - except KeyError: - return 'Unknown' def get_lang(context: CallbackContext): try:
M
Games.py
→
Slot.py
@@ -25,7 +25,7 @@ def get_user_value(context: CallbackContext, key:str, default):
try: return context.user_data[key] except KeyError: - print(f"set {key} to {str(default)}") + #print(f"set {key} to {str(default)}") return set_user_value(context, key, default) def get_cash(context: CallbackContext):@@ -84,7 +84,7 @@ amount = 1
context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context)) if amount == 1: - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet), callback_data="callback_1")]]) + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet), callback_data="reroll_single")]]) _spin(context=context, id=update.effective_chat.id, markup=markup) else: amount = max(1, min(amount, autospin_cap))@@ -118,7 +118,7 @@
def cash(update: Update, context: CallbackContext): cash = get_cash(context) / 100 - if cash == 0: + if cash < cash_default / 2: lastreset = get_lastreset(context) today = date.today()
M
main.py
→
main.py
@@ -1,8 +1,8 @@
from PIL import Image from Api import get_random_image, rating_normal, rating_lewd from Effects import img_to_bio, tt_bt_effect, bt_effect, splash_effect, wot_effect, text_effect -from Constants import get_localized_string as l, format_author, format_lang, langs, get_lang -from Games import spin, bet, cash +from Constants import get_localized_string as l, format_author, format_lang, langs, get_lang, lang_markup +from Slot import spin, bet, cash from dotenv import load_dotenv load_dotenv()@@ -342,6 +342,10 @@
_, reply, _ = _get_reply(update.message.reply_to_message, ' '.join(context.args)) context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper()) +def _set_lang(update: Update, context: CallbackContext, lang: str): + context.chat_data["lang"] = lang + context.bot.send_message(chat_id=update.effective_chat.id, text=l("language_set", context).format(format_lang(lang))) + def lang(update: Update, context: CallbackContext): try: selected = str(context.args[0])@@ -351,15 +355,13 @@
if selected is None: lang = format_lang(get_lang(context)) choices = ", ".join(langs) + "." - return update.message.reply_text(text=l("current_language", context).format(lang, choices)) + return update.message.reply_text(text=l("current_language", context).format(lang, choices), reply_markup=lang_markup) if selected not in langs: update.message.reply_text(text=l("invalid_language", context)) return - - context.chat_data["lang"] = selected - - update.message.reply_text(text=l("language_set", context).format(format_lang(selected))) + + _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.")@@ -374,7 +376,26 @@
def _add_effect_handler(dispatcher, command: str, callback): dispatcher.add_handler(CommandHandler(command, callback)) dispatcher.add_handler(MessageHandler(Filters.caption(update=[f"/{command}"]), callback)) + +def keyboard_handler(update: Update, context: CallbackContext): + query = update.callback_query + match query.data: + case "reroll_single": + spin(update, context) + case "set_lang_en": + lang = "en" + _set_lang(update, context, lang) + return query.answer(l("language_set", context).format(format_lang(lang))) + case "set_lang_it": + lang = "it" + _set_lang(update, context, lang) + return query.answer(l("language_set", context).format(format_lang(lang))) + case other: + logging.error(f"unknown callback: {query.data}") + + return query.answer() + def main(): updater = Updater(token=os.getenv("token"),@@ -386,6 +407,7 @@ ))
dispatcher = updater.dispatcher dispatcher.add_error_handler(error_callback) + dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler)) # commands dispatcher.add_handler(CommandHandler('start', start))@@ -406,8 +428,6 @@ # games
dispatcher.add_handler(CommandHandler('spin', spin)) dispatcher.add_handler(CommandHandler('bet', bet)) dispatcher.add_handler(CommandHandler('cash', cash)) - dispatcher.add_handler(CallbackQueryHandler(callback=spin)) - #dispatcher.add_handler(CallbackQueryHandler(callback=autospin)) # secrets dispatcher.add_handler(CommandHandler('raw', raw))