Adding KeyboardButton #232

This commit is contained in:
Leandro Toledo 2016-04-14 02:01:05 -03:00
parent 751402a0d3
commit 429ea92254
11 changed files with 107 additions and 29 deletions

View file

@ -73,6 +73,7 @@ from .inputtextmessagecontent import InputTextMessageContent
from .inputlocationmessagecontent import InputLocationMessageContent
from .inputvenuemessagecontent import InputVenueMessageContent
from .inputcontactmessagecontent import InputContactMessageContent
from .keyboardbutton import KeyboardButton
from .update import Update
from .bot import Bot
@ -112,7 +113,7 @@ def JobQueue(*args, **kwargs):
__author__ = 'devs@python-telegram-bot.org'
__version__ = '3.4'
__all__ = ('Audio',
__all__ = ['Audio',
'Bot',
'Chat',
'ChatAction',
@ -153,6 +154,7 @@ __all__ = ('Audio',
'InputMessageContent',
'InputTextMessageContent',
'InputVenueMessageContent',
'KeyboardButton',
'Location',
'Message',
'NullHandler',
@ -168,4 +170,4 @@ __all__ = ('Audio',
'User',
'UserProfilePhotos',
'Video',
'Voice',)
'Voice']

View file

@ -33,7 +33,6 @@ logging.getLogger(__name__).addHandler(NullHandler())
class Bot(TelegramObject):
"""This object represents a Telegram Bot.
Attributes:
@ -71,6 +70,7 @@ class Bot(TelegramObject):
"""
Returns:
"""
@functools.wraps(func)
def decorator(self, *args, **kwargs):
"""
@ -81,6 +81,7 @@ class Bot(TelegramObject):
result = func(self, *args, **kwargs)
return result
return decorator
@property
@ -129,6 +130,7 @@ class Bot(TelegramObject):
logger.debug(result)
logger.debug('Exiting: %s', func.__name__)
return result
return decorator
def message(func):
@ -136,6 +138,7 @@ class Bot(TelegramObject):
Returns:
A telegram.Message instance representing the message posted.
"""
@functools.wraps(func)
def decorator(self, *args, **kwargs):
"""
@ -143,6 +146,7 @@ class Bot(TelegramObject):
"""
url, data = func(self, *args, **kwargs)
return Bot._post_message(url, data, kwargs)
return decorator
@staticmethod

View file

@ -21,7 +21,6 @@
This module contains a object that represents a Telegram ChosenInlineResult
"""
from telegram import TelegramObject, User

View file

@ -163,25 +163,25 @@ class Emoji(object):
SQUARED_SOS = n(b'\xF0\x9F\x86\x98')
SQUARED_UP_WITH_EXCLAMATION_MARK = n(b'\xF0\x9F\x86\x99')
SQUARED_VS = n(b'\xF0\x9F\x86\x9A')
REGIONAL_INDICATOR_SYMBOL_LETTER_D_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_E\
REGIONAL_INDICATOR_SYMBOL_LETTER_D_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_E \
= n(b'\xF0\x9F\x87\xA9\xF0\x9F\x87\xAA')
REGIONAL_INDICATOR_SYMBOL_LETTER_G_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_B\
REGIONAL_INDICATOR_SYMBOL_LETTER_G_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_B \
= n(b'\xF0\x9F\x87\xAC\xF0\x9F\x87\xA7')
REGIONAL_INDICATOR_SYMBOL_LETTER_C_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_N\
REGIONAL_INDICATOR_SYMBOL_LETTER_C_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_N \
= n(b'\xF0\x9F\x87\xA8\xF0\x9F\x87\xB3')
REGIONAL_INDICATOR_SYMBOL_LETTER_J_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_P\
REGIONAL_INDICATOR_SYMBOL_LETTER_J_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_P \
= n(b'\xF0\x9F\x87\xAF\xF0\x9F\x87\xB5')
REGIONAL_INDICATOR_SYMBOL_LETTER_K_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_R\
REGIONAL_INDICATOR_SYMBOL_LETTER_K_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_R \
= n(b'\xF0\x9F\x87\xB0\xF0\x9F\x87\xB7')
REGIONAL_INDICATOR_SYMBOL_LETTER_F_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_R\
REGIONAL_INDICATOR_SYMBOL_LETTER_F_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_R \
= n(b'\xF0\x9F\x87\xAB\xF0\x9F\x87\xB7')
REGIONAL_INDICATOR_SYMBOL_LETTER_E_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_S\
REGIONAL_INDICATOR_SYMBOL_LETTER_E_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_S \
= n(b'\xF0\x9F\x87\xAA\xF0\x9F\x87\xB8')
REGIONAL_INDICATOR_SYMBOL_LETTER_I_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_T\
REGIONAL_INDICATOR_SYMBOL_LETTER_I_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_T \
= n(b'\xF0\x9F\x87\xAE\xF0\x9F\x87\xB9')
REGIONAL_INDICATOR_SYMBOL_LETTER_U_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_S\
REGIONAL_INDICATOR_SYMBOL_LETTER_U_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_S \
= n(b'\xF0\x9F\x87\xBA\xF0\x9F\x87\xB8')
REGIONAL_INDICATOR_SYMBOL_LETTER_R_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_U\
REGIONAL_INDICATOR_SYMBOL_LETTER_R_PLUS_REGIONAL_INDICATOR_SYMBOL_LETTER_U \
= n(b'\xF0\x9F\x87\xB7\xF0\x9F\x87\xBA')
SQUARED_KATAKANA_KOKO = n(b'\xF0\x9F\x88\x81')
SQUARED_KATAKANA_SA = n(b'\xF0\x9F\x88\x82')

View file

@ -62,13 +62,11 @@ class TelegramError(Exception):
class Unauthorized(TelegramError):
def __init__(self):
super(Unauthorized, self).__init__('Unauthorized')
class InvalidToken(TelegramError):
def __init__(self):
super(InvalidToken, self).__init__('Invalid token')
@ -78,6 +76,5 @@ class NetworkError(TelegramError):
class TimedOut(NetworkError):
def __init__(self):
super(TimedOut, self).__init__('Timed out')

View file

@ -26,7 +26,6 @@ from telegram.utils.request import download as _download
class File(TelegramObject):
"""This object represents a Telegram File.
Attributes:

View file

@ -51,9 +51,9 @@ class InlineKeyboardButton(TelegramObject):
if not data:
return []
inline_keyboard = list()
inline_keyboards = list()
for inline_keyboard in data:
inline_keyboard.append(InlineKeyboardButton.
de_json(inline_keyboard))
inline_keyboards.append(InlineKeyboardButton.
de_json(inline_keyboard))
return inline_keyboard
return inline_keyboards

View file

@ -25,7 +25,6 @@ from telegram.utils.validate import validate_string
class InlineQueryResultAudio(InlineQueryResult):
def __init__(self,
id,
audio_url,
@ -44,6 +43,6 @@ class InlineQueryResultAudio(InlineQueryResult):
self.performer = performer
self.audio_duration = audio_duration
if reply_markup is not None:
self.reply_markup = 'ReplyMarkup' # TODO
self.reply_markup = 'ReplyMarkup' # TODO
if input_message_content is not None:
self.input_message_content = 'InputMessageContent'

View file

@ -85,7 +85,7 @@ class InputFile(object):
hasattr(self.input_file, 'name'):
self.filename = os.path.basename(self.input_file.name)
elif from_url:
self.filename = os.path.basename(self.input_file.url)\
self.filename = os.path.basename(self.input_file.url) \
.split('?')[0].split('&')[0]
try:
@ -94,7 +94,7 @@ class InputFile(object):
self.filename = self.mimetype.replace('/', '.')
except TelegramError:
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
DEFAULT_MIME_TYPE
DEFAULT_MIME_TYPE
@property
def headers(self):
@ -199,6 +199,6 @@ class InputFile(object):
file_content = data[file_type[0]]
return isinstance(file_content, file) or \
str(file_content).startswith('http')
str(file_content).startswith('http')
return False

View file

@ -0,0 +1,66 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# 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/].
"""This module contains a object that represents a Telegram KeyboardButton."""
from telegram import TelegramObject
class KeyboardButton(TelegramObject):
"""
This object represents one button of the reply keyboard. For simple
text buttons String can be used instead of this object to specify text
of the button.
Args:
text (str):
request_location (Optional[bool]):
request_contact (Optional[bool]):
"""
def __init__(self,
text,
request_contact=None,
request_location=None):
# Required
self.text = text
# Optionals
if request_contact:
self.request_contact = request_contact
if request_location:
self.request_location = request_location
@staticmethod
def de_json(data):
if not data:
return None
return KeyboardButton(**data)
@staticmethod
def de_list(data):
if not data:
return []
keyboards = list()
for keyboard in data:
keyboards.append(KeyboardButton.
de_json(keyboard))
return keyboards

View file

@ -20,14 +20,14 @@
"""This module contains a object that represents a Telegram
ReplyKeyboardMarkup."""
from telegram import ReplyMarkup
from telegram import ReplyMarkup, KeyboardButton
class ReplyKeyboardMarkup(ReplyMarkup):
"""This object represents a Telegram ReplyKeyboardMarkup.
Attributes:
keyboard (List[List[str]]):
keyboard (List[List[:class:`telegram.KeyboardButton`]]):
resize_keyboard (bool):
one_time_keyboard (bool):
selective (bool):
@ -64,4 +64,16 @@ class ReplyKeyboardMarkup(ReplyMarkup):
if not data:
return None
data['keyboard'] = [KeyboardButton.de_list(keyboard) for keyboard in
data['keyboard']]
return ReplyKeyboardMarkup(**data)
def to_dict(self):
data = super(ReplyKeyboardMarkup, self).to_dict()
data['keyboard'] = []
for keyboard in self.keyboard:
data['keyboard'].append([x.to_dict() for x in keyboard])
return data