2015-07-08 21:58:18 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2017-07-23 22:33:08 +02:00
|
|
|
"""This module contains an object that represents Telegram errors."""
|
2015-08-22 04:15:29 +02:00
|
|
|
|
2015-12-25 22:28:07 +01:00
|
|
|
|
|
|
|
def _lstrip_str(in_s, lstr):
|
|
|
|
"""
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
in_s (:obj:`str`): in string
|
|
|
|
lstr (:obj:`str`): substr to strip from left side
|
2015-12-25 22:28:07 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
str:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if in_s.startswith(lstr):
|
|
|
|
res = in_s[len(lstr):]
|
|
|
|
else:
|
|
|
|
res = in_s
|
|
|
|
return res
|
2015-09-11 01:08:24 +02:00
|
|
|
|
2015-07-08 21:58:18 +02:00
|
|
|
|
|
|
|
class TelegramError(Exception):
|
2015-09-07 20:53:09 +02:00
|
|
|
def __init__(self, message):
|
|
|
|
super(TelegramError, self).__init__()
|
|
|
|
|
2015-12-25 22:28:07 +01:00
|
|
|
msg = _lstrip_str(message, 'Error: ')
|
|
|
|
msg = _lstrip_str(msg, '[Error]: ')
|
2016-05-26 02:15:17 +02:00
|
|
|
msg = _lstrip_str(msg, 'Bad Request: ')
|
2015-12-25 22:28:07 +01:00
|
|
|
if msg != message:
|
|
|
|
# api_error - capitalize the msg...
|
|
|
|
msg = msg.capitalize()
|
|
|
|
self.message = msg
|
2015-09-07 20:53:09 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s' % (self.message)
|
2016-01-23 23:26:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Unauthorized(TelegramError):
|
2017-05-12 17:38:36 +02:00
|
|
|
pass
|
2016-01-23 23:26:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidToken(TelegramError):
|
2016-05-15 03:46:40 +02:00
|
|
|
|
2016-01-23 23:26:27 +01:00
|
|
|
def __init__(self):
|
|
|
|
super(InvalidToken, self).__init__('Invalid token')
|
|
|
|
|
|
|
|
|
2016-05-26 02:24:29 +02:00
|
|
|
class NetworkError(TelegramError):
|
2016-05-26 02:15:17 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-05-26 03:09:18 +02:00
|
|
|
class BadRequest(NetworkError):
|
2016-01-23 23:26:27 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TimedOut(NetworkError):
|
2016-05-15 03:46:40 +02:00
|
|
|
|
2016-01-23 23:26:27 +01:00
|
|
|
def __init__(self):
|
|
|
|
super(TimedOut, self).__init__('Timed out')
|
2016-07-25 20:50:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ChatMigrated(TelegramError):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
new_chat_id (:obj:`int`):
|
2016-07-25 20:50:33 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
2016-07-25 20:50:33 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
def __init__(self, new_chat_id):
|
2016-10-04 00:27:45 +02:00
|
|
|
super(ChatMigrated,
|
|
|
|
self).__init__('Group migrated to supergroup. New chat id: {}'.format(new_chat_id))
|
2016-07-25 20:50:33 +02:00
|
|
|
self.new_chat_id = new_chat_id
|
2016-10-04 00:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RetryAfter(TelegramError):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
retry_after (:obj:`int`):
|
2016-10-04 00:27:45 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
2016-10-04 00:27:45 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
def __init__(self, retry_after):
|
2016-10-04 00:27:45 +02:00
|
|
|
super(RetryAfter,
|
|
|
|
self).__init__('Flood control exceeded. Retry in {} seconds'.format(retry_after))
|
2016-10-15 22:38:11 +02:00
|
|
|
self.retry_after = float(retry_after)
|