Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add member welcome and templates config #13

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/handlers/info_handler.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions app/handlers/member_handler.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion app/handlers/text_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# random_text.py
# text_handler.py
from telebot import TeleBot
from telebot.types import Message

Expand Down
7 changes: 7 additions & 0 deletions app/services/info_service.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions app/services/member_service.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions configs/templates.py
Original file line number Diff line number Diff line change
@@ -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)
"""
11 changes: 11 additions & 0 deletions tests/info_test.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions tests/member_test.py
Original file line number Diff line number Diff line change
@@ -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)
Loading