Skip to content

Conversation

overquota
Copy link
Contributor

When converting telegram group (OLD_CHAT_ID) to supergroup (NEW_CHAT_ID), chat id changes
After this, when trying to post message to OLD_CHAT_ID, Telegram API returns error with message:
{u'error_code': 400, u'ok': False, u'description': u'Bad Request: group chat is migrated to a supergroup chat', u'parameters': {u'migrate_to_chat_id': NEW_CHAT_ID}}

So this pull requests creates new exception ChatMigrated which returns NEW_CHAT_ID
With this functionality we can postpone fix our inconsistency data in database when user converts group to supergroup and we cannot update this info in database at the same time (eg datacenter failed or network lag)

Sorry for my English :)

if not data.get('ok') and data.get('description'):
return data['description']
if not data.get('ok'):
if data.get('parameters') and data['parameters'].get('migrate_to_chat_id'):
Copy link
Member

@tsnoam tsnoam Jul 18, 2016

Choose a reason for hiding this comment

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

(edited)
please save data.get('parameters') into a variable, so you won't need to get it 3 times. same goes for migrate_to_chat_id.

@tsnoam
Copy link
Member

tsnoam commented Jul 18, 2016

@overquota
Thank you for your contribution. Generally it looks good and we'd be happy to merge it. However:

1 Please see the comments I've put on the code.
2. The CI failed due to yapf test (an improved pep8 and style enforcer). Please check the "contribution guide" for more info about that.
3. You should also add yourself to the AUTHORS.rst file as described in the "contribution guide". :-)

@tsnoam tsnoam added enhancement 📋 pending-reply work status: pending-reply labels Jul 18, 2016
@tsnoam tsnoam self-assigned this Jul 18, 2016
@overquota
Copy link
Contributor Author

Hi! Thank you for comments!
the TelegramError exception waits only string in args, so i decided to do str(chat_id)
I rewrited TelegramError exception, so now it know how to process optional args (kwargs) and chat_id in it
Unfortunately I don't know is it a good decision

if migrate_to_chat_id:
raise ChatMigrated(description, chat_id=int(migrate_to_chat_id))
else:
return description
Copy link
Member

Choose a reason for hiding this comment

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

The above code is correct with the JSON reply.
However, it is better to be more liberal with the way we interpret the replies from Telegram servers, especially that they like making changes without any prior notification.
So... please do something more similar to your original commit:

if parameters:
    if migrate... :
        raise
if description:
    return description

@tsnoam
Copy link
Member

tsnoam commented Jul 19, 2016

Hi @overquota ,
This looks much better, though there are now new comments on the code.

@overquota
Copy link
Contributor Author

overquota commented Jul 20, 2016

Hmm, last change breaked CI, but I can't reproduce it:

python-telegram-bot $ cat test.py

#!/usr/bin/env python
from telegram import Bot
import sys

print sys.version
bot = Bot('1234:abcd1234')
bot.getMe()
python-telegram-bot $ ./test.py
2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
Traceback (most recent call last):
  File "./test.py", line 7, in <module>
    bot.getMe()
  File "/Users/aarseniev/tmp/python-telegram-bot/telegram/bot.py", line 121, in decorator
    result = func(self, *args, **kwargs)
  File "/Users/aarseniev/tmp/python-telegram-bot/telegram/bot.py", line 172, in getMe
    result = request.get(url)
  File "/Users/aarseniev/tmp/python-telegram-bot/telegram/utils/request.py", line 204, in get
    result = _request_wrapper('GET', url)
  File "/Users/aarseniev/tmp/python-telegram-bot/telegram/utils/request.py", line 185, in _request_wrapper
    raise Unauthorized()
telegram.error.Unauthorized: Unauthorized

@overquota
Copy link
Contributor Author

Seems like CI failing on some temp reasons (timeout?)

@rahiel
Copy link
Contributor

rahiel commented Jul 22, 2016

Seems like CI failing on some temp reasons (timeout?)

@overquota thanks for your work! Our Travis tests sometimes fail for no good reason, but they're all good now.

@tsnoam, time to merge?

@tsnoam tsnoam merged commit f1ee54f into python-telegram-bot:master Jul 25, 2016
@jh0ker jh0ker added this to the 5.1 milestone Sep 20, 2016
@jh0ker jh0ker removed the 📋 pending-reply work status: pending-reply label Sep 20, 2016
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2020
@Bibo-Joshi Bibo-Joshi added 🔌 enhancement pr description: enhancement and removed enhancement labels Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔌 enhancement pr description: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants