diff --git a/app/handlers/info_handler.py b/app/handlers/info_handler.py index 203a5d4..18e728e 100644 --- a/app/handlers/info_handler.py +++ b/app/handlers/info_handler.py @@ -1,7 +1,12 @@ -# info.py +# info_handler.py from telebot import TeleBot from telebot.types import Message +from app.services.info_service import generate_info + def get_info(message: Message, bot: TeleBot): - bot.send_message(message.chat.id, "Hello, I'm a bot of Randomology ~") + name = message.from_user.first_name + id = message.from_user.id + info = generate_info(name, id) + bot.send_message(message.chat.id, info, parse_mode="MarkdownV2") diff --git a/app/handlers/member_handler.py b/app/handlers/member_handler.py new file mode 100644 index 0000000..cd70a3e --- /dev/null +++ b/app/handlers/member_handler.py @@ -0,0 +1,14 @@ + # member_hander.py + +from telebot import TeleBot +from telebot.types import Message + +from app.services.member_service import generate_welcome + +# invoke when new member joins +def get_welcome(message:Message, bot:TeleBot): + # typing ... + bot.send_chat_action(message.chat.id, "typing") + new_member = message.new_chat_members + welcome = generate_welcome(new_member[0].first_name, new_member[0].id) + bot.send_message(message.chat.id, welcome, parse_mode="MarkdownV2") diff --git a/app/handlers/text_handler.py b/app/handlers/text_handler.py index 8ab04ba..24cdf6e 100644 --- a/app/handlers/text_handler.py +++ b/app/handlers/text_handler.py @@ -1,4 +1,4 @@ -# random_text.py +# text_handler.py from telebot import TeleBot from telebot.types import Message diff --git a/app/services/info_service.py b/app/services/info_service.py new file mode 100644 index 0000000..88a683c --- /dev/null +++ b/app/services/info_service.py @@ -0,0 +1,7 @@ +# info_service.py + +from configs import templates + +# generate info +def generate_info(name, id): + return templates.INFO_MESSAGE.format(name=name, id=id) diff --git a/app/services/member_service.py b/app/services/member_service.py new file mode 100644 index 0000000..24243e9 --- /dev/null +++ b/app/services/member_service.py @@ -0,0 +1,7 @@ +# member_service.py + +from configs import templates + +# generate welcome message +def generate_welcome(name, id): + return templates.WELCOME_MESSAGE.format(name=name, id=id) diff --git a/bot.py b/bot.py index b6613aa..e848e8f 100644 --- a/bot.py +++ b/bot.py @@ -11,6 +11,7 @@ from app.handlers.audio_handler import get_random_sine from app.handlers.audio_handler import get_random_mix from app.handlers.audio_handler import get_random_voice +from app.handlers.member_handler import get_welcome from app.handlers.inline_handler import inline_dispatch # pyTelegramBotAPI @@ -44,6 +45,8 @@ def handlers(): bot.register_message_handler(get_random_mix, commands=['mix'], pass_bot=True) # random voice (a mix of noise and sine wave sent as voice) bot.register_message_handler(get_random_voice, commands=['voice'], pass_bot=True) + # chat member change + bot.register_message_handler(get_welcome, content_types=['new_chat_members'], pass_bot=True) # inline bot.register_inline_handler(inline_dispatch, lambda query: query, pass_bot=True) diff --git a/configs/templates.py b/configs/templates.py new file mode 100644 index 0000000..8873e00 --- /dev/null +++ b/configs/templates.py @@ -0,0 +1,12 @@ +# templates + +# welcome message, will be parsed as markdown +WELCOME_MESSAGE=""" +[{name}](tg://user?id={id}), 我们信任您加入本群时已经从系统管理员那里了解了日常注意事项,总结起来无外乎以下三点: +\#1\) ""?'\*\!\*:\!\$\?\*:\*?\*?:😈😈 +\#2\) Rhrhd dhod dbdog +\#3\) цзц Иатщв, Лоботомия\!\!\! +""" +INFO_MESSAGE=""" +您好,[{name}](tg://user?id={id}),我是[Randomology Bot](https://github.com/ravachol-yang/randomology) +""" diff --git a/tests/info_test.py b/tests/info_test.py new file mode 100644 index 0000000..743f524 --- /dev/null +++ b/tests/info_test.py @@ -0,0 +1,11 @@ +# info_test.py + +from app.services.info_service import generate_info +from configs import templates + +def test_welcome_template(): + assert isinstance(templates.INFO_MESSAGE, str) + +def test_info_generated(): + info = generate_info("test", 111) + assert isinstance(info, str) diff --git a/tests/member_test.py b/tests/member_test.py new file mode 100644 index 0000000..05fc5a8 --- /dev/null +++ b/tests/member_test.py @@ -0,0 +1,11 @@ +# member tests + +from app.services.member_service import generate_welcome +from configs import templates + +def test_welcome_template(): + assert isinstance(templates.WELCOME_MESSAGE, str) + +def test_welcome_generated(): + welcome = generate_welcome("test",111) + assert isinstance(welcome, str)