From 8d87c11cf49e8215364ce52676be38a4366b0b81 Mon Sep 17 00:00:00 2001 From: Domenico Blanco Date: Sun, 3 Mar 2024 23:58:04 +0100 Subject: [PATCH] feat: add /start command (#47) --- src/main.py | 16 +++++++++++++++- src/welcome.json | 6 ++++-- tests/main_test.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 1b68b1a..7cf446d 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ from os import getenv from telegram import Update, User -from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, filters +from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, CommandHandler, filters from json import load from random import randrange @@ -36,6 +36,17 @@ async def send_welcome(update: Update, _: CallbackContext) -> None: f'- {welcome_file[lan_code]["utils"][randrange(0, len(welcome_file[lan_code]["utils"]))]}' await update.message.reply_text(f'{welcome_msg}') + +async def command_start(update: Update, _) -> None: + lan_code = 'it' if update.message.from_user.language_code == 'it' else 'en' + reply = '' + + with open("src/welcome.json", "r") as f: + reply = load(f)[lan_code]['start_message'] + + await update.message.reply_text(reply) + + def main() -> None: token = getenv("QDBotToken") app = ApplicationBuilder().token(token).build() @@ -43,6 +54,9 @@ def main() -> None: filters.ChatType.GROUPS & filters.StatusUpdate.NEW_CHAT_MEMBERS, send_welcome )) + app.add_handler(CommandHandler( + "start", command_start + )) app.run_polling() diff --git a/src/welcome.json b/src/welcome.json index 0702a56..b4495fc 100644 --- a/src/welcome.json +++ b/src/welcome.json @@ -6,7 +6,8 @@ "Non esitare a fare domande se hai bisogno di aiuto", "Speriamo che ti possa sentire come a casa tua", "Ricorda di rispettare le opinioni degli altri membri, anche se non sempre concordi. La diversità ci arricchisce" - ] + ], + "start_message": "Ciao! Sono il Welcome-Bot, aggiungimi a un gruppo per dare il benvenuto ai nuovi membri!" }, "en": { "sentences" : ["Welcome USER to our group ^-^","Hello USER!","Howdy USER!"], @@ -15,7 +16,8 @@ "Don't hesitate to ask questions if you need help", "We hope you can feel at home", "Remember to respect other members' opinions, even if you don't always agree. Diversity enriches us." - ] + ], + "start_message": "Hi! I'm the Welcome-Bot, add me to a group to welcome your new users!" }, "readme" : "https://t.me/c/1095167198/67194/67418" } diff --git a/tests/main_test.py b/tests/main_test.py index d841df2..2d98597 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -60,6 +60,24 @@ def get_wel(lan_code: str) -> str: 'mock_obj': [main, Application], 'mock_func': ['getenv', 'run_polling'], 'mock_ret': ['123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', True] + }, + { + 'func': main.command_start, + 'expected_res': None, + 'arg': (main.Update(0, message=Message(0, from_user=possible_users[4], chat=Chat(0, type='GROUP'), date=datetime.now())), None), + 'mock_obj': [Message], + 'mock_func': ['reply_text'], + 'mock_ret': [True], + 'is_async': True + }, + { + 'func': main.command_start, + 'expected_res': None, + 'arg': (main.Update(0, message=Message(0, from_user=possible_users[5], chat=Chat(0, type='GROUP'), date=datetime.now())), None), + 'mock_obj': [Message], + 'mock_func': ['reply_text'], + 'mock_ret': [True], + 'is_async': True } ]