From b8f5e5ef918e16aa03e285c7e52c6c049692ebee Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Tue, 19 Apr 2022 21:45:43 +0200 Subject: [PATCH] Updated Storing bot, user and chat related data (markdown) --- Storing-bot,-user-and-chat-related-data.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Storing-bot,-user-and-chat-related-data.md b/Storing-bot,-user-and-chat-related-data.md index 503795d..52d623a 100644 --- a/Storing-bot,-user-and-chat-related-data.md +++ b/Storing-bot,-user-and-chat-related-data.md @@ -103,11 +103,17 @@ async def my_callback(update, context): # Resend to new chat id await context.bot.send_message(new_id, text) - # Transfer data - if chat_id in application.chat_data: - application.migrate_chat_data( - old_chat_id=chat_id, - new_chat_id=new_id - ) + # Get old and new chat ids + old_id = message.migrate_from_chat_id or message.chat_id + new_id = message.migrate_to_chat_id or message.chat_id + + # transfer data, only if old data is still present + # this step is important, as Telegram sends *two* updates + # about the migration + if old_id in application.chat_data: + application.migrate_chat_data( + old_chat_id=old_id, + new_chat_id=new_id + ) ... ```