ChatMigrated exception (#353)

* ChatMigrated exception
This commit is contained in:
overquota 2016-07-25 21:50:33 +03:00 committed by Noam Meltzer
parent 90913724ca
commit f1ee54fa73
3 changed files with 25 additions and 3 deletions

View file

@ -22,6 +22,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `njittam <https://github.com/njittam>`_
- `Noam Meltzer <https://github.com/tsnoam>`_
- `Oleg Shlyazhko <https://github.com/ollmer>`_
- `overquota <https://github.com/overquota>`_
- `Rahiel Kasim <https://github.com/rahiel>`_
- `Shelomentsev D <https://github.com/shelomentsevd>`_
- `sooyhwang <https://github.com/sooyhwang>`_

View file

@ -85,3 +85,17 @@ class TimedOut(NetworkError):
def __init__(self):
super(TimedOut, self).__init__('Timed out')
class ChatMigrated(TelegramError):
def __init__(self, new_chat_id):
"""
Args:
new_chat_id (int):
Returns:
"""
super(ChatMigrated, self).__init__('Chat migrated')
self.new_chat_id = new_chat_id

View file

@ -28,7 +28,7 @@ import urllib3
from urllib3.connection import HTTPConnection
from telegram import (InputFile, TelegramError)
from telegram.error import Unauthorized, NetworkError, TimedOut, BadRequest
from telegram.error import Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated
_CON_POOL = None
""":type: urllib3.PoolManager"""
@ -135,8 +135,15 @@ def _parse(json_data):
except ValueError:
raise TelegramError('Invalid server response')
if not data.get('ok') and data.get('description'):
return data['description']
if not data.get('ok'):
description = data.get('description')
parameters = data.get('parameters')
if parameters:
migrate_to_chat_id = parameters.get('migrate_to_chat_id')
if migrate_to_chat_id:
raise ChatMigrated(migrate_to_chat_id)
if description:
return description
return data['result']