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
:members:
:undoc-members:
:show-inheritance:

View file

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

File diff suppressed because it is too large Load diff

View file

@ -24,23 +24,40 @@ from telegram import 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,
text,
url=None,
callback_data=None,
switch_inline_query=None):
**kwargs):
# Required
self.text = text
# Optionals
self.url = url
self.callback_data = callback_data
self.switch_inline_query = switch_inline_query
self.url = kwargs.get('url')
self.callback_data = kwargs.get('callback_data')
self.switch_inline_query = kwargs.get('switch_inline_query')
@staticmethod
def de_json(data):
data = super(InlineKeyboardButton, InlineKeyboardButton).de_json(data)
if not data:
return None

View file

@ -24,7 +24,15 @@ from telegram import ReplyMarkup, InlineKeyboardButton
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,
inline_keyboard):

View file

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

View file

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

View file

@ -25,6 +25,32 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, \
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,
id,
audio_url,

View file

@ -19,6 +19,8 @@
"""This module contains functions to validate function arguments"""
from telegram.error import InvalidToken
try:
type(basestring)
except NameError:
@ -36,3 +38,11 @@ def validate_string(arg, name):
"""
if not isinstance(arg, basestring) and arg is not None:
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):
with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'):
# bypass the valid token check
bot_cls = type('bot_cls', (telegram.Bot, ), {'_valid_token': lambda self, token: token})
bot = bot_cls('12')
bot = telegram.Bot.__new__(telegram.Bot)
bot.base_url = 'https://api.telegram.org/bot{0}'.format('12')
bot.getMe()

View file

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