Skip to content

Commit

Permalink
feat: add handling for when bot is added during chat creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuhanming committed May 4, 2021
1 parent 39b9987 commit a1dbd98
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from src.add_handlers import add_conv_handler
from src.chat_handlers import (
add_me,
chat_created_handler,
chat_members,
left_chat_member_handler,
new_chat_member_handler,
Expand Down Expand Up @@ -38,6 +39,9 @@ def main() -> None:
dispatcher.add_handler(
MessageHandler(Filters.text & ~Filters.command, unknown_message)
)
dispatcher.add_handler(
MessageHandler(Filters.status_update.chat_created, chat_created_handler)
)
dispatcher.add_handler(
MessageHandler(Filters.status_update.new_chat_members, new_chat_member_handler)
)
Expand Down
32 changes: 30 additions & 2 deletions src/chat_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ def generate_user_list(chat: dict, users: list[dict]) -> str:
return message


def chat_created_handler(update: Update, _: CallbackContext) -> None:
"""Adds all new non-bot users to the current chat group."""
if update.message is None:
# Fail silently
return
chat = update.message.chat
if chat.type == "private":
# Fail silently
return

chat_dict = SERVICES.chat_service.create_if_not_exists(
title=chat.title, telegram_id=str(chat.id)
)

user = update.message.from_user
if user is not None and not user.is_bot:
user_dict = SERVICES.user_service.create_if_not_exists(
full_name=user.full_name, telegram_id=str(user.id)
)
SERVICES.belong_service.add_user_to_chat_if_not_inside(
user_id=user_dict["id"], chat_id=chat_dict["id"]
)

update.message.reply_text(
"You've just added the Coding Question Bot! Use /add_me to join the tracking for this chat!"
)


def new_chat_member_handler(update: Update, _: CallbackContext) -> None:
"""Adds all new non-bot users to the current chat group."""
if update.message is None:
Expand Down Expand Up @@ -84,7 +112,7 @@ def chat_members(update: Update, _: CallbackContext) -> None:
update.message = unwrap(update.message)

chat = update.message.chat
if chat.type != "group":
if chat.type == "private":
update.message.reply_text("I'm only talking to you here!")
return
chat_dict = SERVICES.chat_service.get_chat_by_telegram_id(telegram_id=str(chat.id))
Expand All @@ -100,7 +128,7 @@ def add_me(update: Update, _: CallbackContext) -> None:
user = unwrap(update.effective_user)

chat = update.message.chat
if chat.type != "group":
if chat.type == "private":
update.message.reply_text("Please use this command in a chat group!")
return

Expand Down

0 comments on commit a1dbd98

Please sign in to comment.