spelling, simplify usage of migrate_chat_data

Harshil 2022-04-20 01:02:23 +05:30
parent 1192f86b6f
commit 3a626ad0b3

@ -18,7 +18,7 @@ async def put(update, context):
# Store value # Store value
context.user_data[key] = value context.user_data[key] = value
# Send the key to the user # Send the key to the user
update.message.reply_text(key) await update.message.reply_text(key)
async def get(update, context): async def get(update, context):
"""Usage: /get uuid""" """Usage: /get uuid"""
@ -27,7 +27,7 @@ async def get(update, context):
# Load value and send it to the user # Load value and send it to the user
value = context.user_data.get(key, 'Not found') value = context.user_data.get(key, 'Not found')
update.message.reply_text(value) await update.message.reply_text(value)
if __name__ == '__main__': if __name__ == '__main__':
application = Application.builder().token('TOKEN').build() application = Application.builder().token('TOKEN').build()
@ -61,18 +61,7 @@ When a group migrates, Telegram will send an update that just states the new inf
async def chat_migration(update, context): async def chat_migration(update, context):
message = update.message message = update.message
application = context.application application = context.application
application.migrate_chat_data(message=message)
# 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
if old_id in application.chat_data:
application.migrate_chat_data(
old_chat_id=old_id,
new_chat_id=new_id
)
... ...
def main(): def main():
@ -83,11 +72,13 @@ def main():
... ...
``` ```
See also: [`migrate_chat_data`](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.application.html#telegram.ext.Application.migrate_chat_data)
To be entirely sure that the update will be processed by this handler, either add it first or put it in its own group. To be entirely sure that the update will be processed by this handler, either add it first or put it in its own group.
### ChatMigrated Errors ### ChatMigrated Errors
If you try e.g. sending a message to the old chat id, Telegram will respond by a Bad Request including the new chat id. You can access it using an error handler: If you try e.g. sending a message to the old chat id, Telegram will respond by a `BadRequest` including the new chat id. You can access it using an error handler:
```python ```python
async def error(update, context): async def error(update, context):
@ -101,7 +92,7 @@ Unfortunately, Telegram does *not* pass along the old chat id, so there is curre
```python ```python
async def my_callback(update, context): async def my_callback(update, context):
applcation = context.applcation application = context.application
... ...
try: try:
@ -114,7 +105,7 @@ async def my_callback(update, context):
# Transfer data # Transfer data
if chat_id in application.chat_data: if chat_id in application.chat_data:
applcation.migrate_chat_data( application.migrate_chat_data(
old_chat_id=chat_id, old_chat_id=chat_id,
new_chat_id=new_id new_chat_id=new_id
) )