Skip to content

Commit

Permalink
Handle ChatMigrated Exception and Event (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckreisl committed Jun 12, 2024
1 parent 9a10b97 commit 35e3097
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cs2posts/bot/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def create_and_add(self, chat_id: int) -> Chat:
self.add(chat)
return chat

def migrate(self, chat: Chat, new_chat_id: int) -> Chat:
self.chats.remove(chat)
chat.chat_id = new_chat_id
self.add(chat)
return chat

def get_running_chats(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_running]

Expand Down
31 changes: 31 additions & 0 deletions cs2posts/bot/cs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from telegram.constants import ChatType
from telegram.constants import ParseMode
from telegram.error import BadRequest
from telegram.error import ChatMigrated
from telegram.error import Forbidden
from telegram.ext import Application
from telegram.ext import CallbackContext
Expand Down Expand Up @@ -83,6 +84,8 @@ def __init__(self, *args, **kwargs) -> None:
filters.StatusUpdate.NEW_CHAT_MEMBERS, self.new_chat_member),
MessageHandler(
filters.StatusUpdate.LEFT_CHAT_MEMBER, self.left_chat_member),
MessageHandler(
filters.StatusUpdate.MIGRATE, self.migrate_chat),
])

# self.app.add_error_handler(self.error)
Expand Down Expand Up @@ -163,6 +166,27 @@ async def left_chat_member(self, update: Update, context: ContextTypes.DEFAULT_T
self.chats.remove(chat)
self.local_chat_store.save(self.chats)

async def migrate_chat(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message.migrate_from_chat_id is None:
# Someone two events are fired for the same chat migration
# ignore the second event where migrate_from_chat_id is None
return

logger.info(
f'Migrating chat from {update.message.migrate_from_chat_id} to {update.message.chat_id} ...')

chat = self.chats.get(update.message.migrate_from_chat_id)
if chat is None:
if self.chats.get(update.message.chat_id) is not None:
logger.info('Chat already migrated. Nothing to do.')
return
return

logger.info(f'Chat migrated to {update.message.chat_id} ...')
chat = self.chats.migrate(chat, update.message.chat_id)
self.local_chat_store.save(self.chats)
logger.info("Chat migrated successfully.")

@spam_protected
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
logger.info(f'Starting bot for chat_id={update.message.chat_id} ...')
Expand Down Expand Up @@ -381,6 +405,13 @@ async def send_message(self, context: CallbackContext, msg: TelegramMessage, cha
f'Bot is blocked by user we delete the chat {chat.chat_id=}')
logger.error(f"Reason: {e}")
self.chats.remove(chat)
except ChatMigrated as e:
logger.error(
f'Chat migrated we update the chat {chat.chat_id=}')
logger.error(f"Reason: {e}")
chat = self.chats.migrate(chat, e.new_chat_id)
self.local_chat_store.save(self.chats)
await self.send_message(context, msg, chat)
except Exception as e:
logger.exception(f'Could not send message to chat {chat.chat_id=}')
logger.exception(f"Reason: {e}")
Expand Down

1 comment on commit 35e3097

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__init__.py00100% 
crawler.py300100% 
cs2.py82890%30, 37, 42, 48, 103, 116–118
post.py58493%61–63, 66
store.py982475%21, 25, 29, 33, 42, 49–50, 57–58, 62, 68–69, 76, 87–88, 90, 104, 107–110, 117, 123, 132
bot
   __init__.py00100% 
   chats.py72593%80–83, 101
   constants.py40100% 
   content.py76692%28, 49, 62, 90, 100, 106
   cs2.py2816875%37–42, 44–46, 170, 173, 175, 178–183, 185–188, 293–296, 299–300, 302–303, 305, 307, 310–311, 314–315, 317–318, 320, 322, 325–326, 329–330, 332–333, 335, 337, 340–341, 351, 353–355, 357–359, 361–363, 365, 377, 409, 411–414, 420
   decorators.py330%1, 3, 5
   message.py1585763%62, 85, 96, 109, 111, 121, 123–124, 126, 128–130, 139–142, 144, 156–159, 193–194, 196, 198–199, 201–202, 204, 206–211, 213–215, 217–220, 222–227, 229–230, 232, 234, 237–238, 253–255
   options.py1052080%35, 42–44, 49–51, 53, 70, 74–75, 79, 81, 83, 86–88, 90, 165–166
   settings.py130100% 
   spam.py710100% 
   utils.py24770%17, 21–23, 31–33
parser
   __init__.py00100% 
   parser.py14192%21
   steam2telegram_html.py260100% 
   steam_list.py520100% 
   steam_news_image.py160100% 
   steam_news_youtube.py120100% 
   steam_update_heading.py210100% 
TOTAL121620383% 

Tests Skipped Failures Errors Time
121 0 💤 0 ❌ 0 🔥 3.724s ⏱️

Please sign in to comment.