Merge branch 'bot-api-2.0' into dispatcher-rework

This commit is contained in:
Jannes Höke 2016-04-21 17:20:05 +02:00
commit cfdfdeb4fc
12 changed files with 584 additions and 372 deletions

View file

@ -3,5 +3,4 @@ telegram.bot module
.. automodule:: telegram.bot .. automodule:: telegram.bot
:members: :members:
:undoc-members:
:show-inheritance: :show-inheritance:

View file

@ -65,7 +65,7 @@ class TelegramObject(object):
data = dict() data = dict()
for key, value in self.__dict__.items(): for key, value in self.__dict__.items():
if value: if value or value == '':
if hasattr(value, 'to_dict'): if hasattr(value, 'to_dict'):
data[key] = value.to_dict() data[key] = value.to_dict()
else: else:

File diff suppressed because it is too large Load diff

View file

@ -24,23 +24,40 @@ from telegram import TelegramObject
class InlineKeyboardButton(TelegramObject): class InlineKeyboardButton(TelegramObject):
"""This object represents a Telegram InlineKeyboardButton.""" """This object represents a Telegram InlineKeyboardButton.
Attributes:
text (str):
url (str):
callback_data (str):
switch_inline_query (str):
Args:
text (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
url (Optional[str]):
callback_data (Optional[str]):
switch_inline_query (Optional[str]):
"""
def __init__(self, def __init__(self,
text, text,
url=None, **kwargs):
callback_data=None,
switch_inline_query=None):
# Required # Required
self.text = text self.text = text
# Optionals # Optionals
self.url = url self.url = kwargs.get('url')
self.callback_data = callback_data self.callback_data = kwargs.get('callback_data')
self.switch_inline_query = switch_inline_query self.switch_inline_query = kwargs.get('switch_inline_query')
@staticmethod @staticmethod
def de_json(data): def de_json(data):
data = super(InlineKeyboardButton, InlineKeyboardButton).de_json(data)
if not data: if not data:
return None return None

View file

@ -24,7 +24,15 @@ from telegram import ReplyMarkup, InlineKeyboardButton
class InlineKeyboardMarkup(ReplyMarkup): class InlineKeyboardMarkup(ReplyMarkup):
"""This object represents a Telegram InlineKeyboardMarkup.""" """This object represents a Telegram InlineKeyboardMarkup.
Attributes:
inline_keyboard (List[List[:class:`telegram.InlineKeyboardMarkup`]]):
Args:
inline_keyboard (List[List[:class:`telegram.InlineKeyboardMarkup`]]):
"""
def __init__(self, def __init__(self,
inline_keyboard): inline_keyboard):

View file

@ -62,6 +62,8 @@ class InlineQuery(TelegramObject):
Returns: Returns:
telegram.InlineQuery: telegram.InlineQuery:
""" """
data = super(InlineQuery, InlineQuery).de_json(data)
if not data: if not data:
return None return None

View file

@ -30,8 +30,8 @@ class InlineQueryResultArticle(InlineQueryResult):
Attributes: Attributes:
id (str): id (str):
title (str): title (str):
input_message_content (telegram.InputMessageContent): input_message_content (:class:`telegram.InputMessageContent`):
reply_markup (telegram.ReplyMarkup): reply_markup (:class:`telegram.ReplyMarkup`):
url (str): url (str):
hide_url (bool): hide_url (bool):
description (str): description (str):
@ -42,7 +42,7 @@ class InlineQueryResultArticle(InlineQueryResult):
Args: Args:
id (str): Unique identifier for this result, 1-64 Bytes id (str): Unique identifier for this result, 1-64 Bytes
title (str): title (str):
reply_markup (telegram.ReplyMarkup): reply_markup (:class:`telegram.ReplyMarkup`):
Keyword Args: Keyword Args:
url (Optional[str]): url (Optional[str]):

View file

@ -25,6 +25,32 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, \
class InlineQueryResultAudio(InlineQueryResult): class InlineQueryResultAudio(InlineQueryResult):
"""Represents a link to an mp3 audio file. By default, this audio file will
be sent by the user. Alternatively, you can use input_message_content to
send a message with the specified content instead of the audio.
Attributes:
id (str):
audio_url (str):
title (str):
performer (Optional[str]):
audio_duration (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
Args:
audio_url (str):
title (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
performer (Optional[str]):
audio_duration (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
"""
def __init__(self, def __init__(self,
id, id,
audio_url, audio_url,

View file

@ -19,6 +19,8 @@
"""This module contains functions to validate function arguments""" """This module contains functions to validate function arguments"""
from telegram.error import InvalidToken
try: try:
type(basestring) type(basestring)
except NameError: except NameError:
@ -36,3 +38,11 @@ def validate_string(arg, name):
""" """
if not isinstance(arg, basestring) and arg is not None: if not isinstance(arg, basestring) and arg is not None:
raise ValueError(name + ' is not a string') raise ValueError(name + ' is not a string')
def validate_token(token):
"""a very basic validation on token"""
left, sep, _right = token.partition(':')
if (not sep) or (not left.isdigit()) or (len(left) < 3):
raise InvalidToken()
return token

View file

@ -186,8 +186,9 @@ class BotTest(BaseTest, unittest.TestCase):
def testInvalidSrvResp(self): def testInvalidSrvResp(self):
with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'): with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'):
# bypass the valid token check # bypass the valid token check
bot_cls = type('bot_cls', (telegram.Bot, ), {'_valid_token': lambda self, token: token}) bot = telegram.Bot.__new__(telegram.Bot)
bot = bot_cls('12') bot.base_url = 'https://api.telegram.org/bot{0}'.format('12')
bot.getMe() bot.getMe()

View file

@ -70,7 +70,7 @@ class InlineQueryTest(BaseTest, unittest.TestCase):
inlinequery = telegram.InlineQuery.de_json(self.json_dict).to_dict() inlinequery = telegram.InlineQuery.de_json(self.json_dict).to_dict()
self.assertTrue(self.is_dict(inlinequery)) self.assertTrue(self.is_dict(inlinequery))
# self.assertDictEqual(inlinequery, self.json_dict) self.assertDictEqual(inlinequery, self.json_dict)
if __name__ == '__main__': if __name__ == '__main__':