Merge branch 'master' into october3

# Conflicts:
#	telegram/bot.py
#	telegram/callbackquery.py
#	telegram/chat.py
#	telegram/ext/messagehandler.py
#	telegram/inlinekeyboardbutton.py
#	telegram/inlinequeryresultcachedaudio.py
#	telegram/message.py
#	tests/test_filters.py
This commit is contained in:
Jacob Bom 2016-10-19 12:35:50 +02:00
commit 960862ccb1
50 changed files with 1481 additions and 847 deletions

3
.gitignore vendored
View file

@ -68,3 +68,6 @@ telegram.mp4
telegram.ogg
telegram.png
telegram.webp
# original files from merges
*.orig

View file

@ -15,7 +15,7 @@
import sys
import os
import shlex
import telegram
# import telegram
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
@ -59,9 +59,9 @@ author = u'Leandro Toledo'
# built documents.
#
# The short X.Y version.
version = telegram.__version__[:3]
version = '5.1' # telegram.__version__[:3]
# The full version, including alpha/beta/rc tags.
release = telegram.__version__
release = '5.1.0' # telegram.__version__
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.

View file

@ -0,0 +1,7 @@
telegram.ext.filters module
===========================
.. automodule:: telegram.ext.filters
:members:
:undoc-members:
:show-inheritance:

View file

@ -15,6 +15,7 @@ Submodules
telegram.ext.commandhandler
telegram.ext.inlinequeryhandler
telegram.ext.messagehandler
telegram.ext.filters
telegram.ext.regexhandler
telegram.ext.stringcommandhandler
telegram.ext.stringregexhandler

View file

@ -35,24 +35,30 @@ class Audio(TelegramObject):
Args:
file_id (str):
duration (int):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
performer (Optional[str]):
title (Optional[str]):
mime_type (Optional[str]):
file_size (Optional[int]):
**kwargs: Arbitrary keyword arguments.
"""
def __init__(self, file_id, duration, **kwargs):
def __init__(self,
file_id,
duration,
performer='',
title='',
mime_type='',
file_size=0,
**kwargs):
# Required
self.file_id = str(file_id)
self.duration = int(duration)
# Optionals
self.performer = kwargs.get('performer', '')
self.title = kwargs.get('title', '')
self.mime_type = str(kwargs.get('mime_type', ''))
self.file_size = int(kwargs.get('file_size', 0))
self.performer = performer
self.title = title
self.mime_type = str(mime_type)
self.file_size = int(file_size)
@staticmethod
def de_json(data, bot):

File diff suppressed because it is too large Load diff

View file

@ -24,16 +24,25 @@ from telegram import TelegramObject, Message, User
class CallbackQuery(TelegramObject):
"""This object represents a Telegram CallbackQuery."""
def __init__(self, id, from_user, chat_instance, bot=None, **kwargs):
def __init__(self,
id,
from_user,
chat_instance,
message=None,
data=None,
inline_message_id=None,
game_short_name=None,
bot=None,
**kwargs):
# Required
self.id = id
self.from_user = from_user
self.chat_instance = chat_instance
# Optionals
self.message = kwargs.get('message')
self.data = kwargs.get('data', '')
self.inline_message_id = kwargs.get('inline_message_id', '')
self.game_short_name = kwargs.get('game_short_name', '')
self.message = message
self.data = data
self.inline_message_id = inline_message_id
self.game_short_name = game_short_name
self.bot = bot
@ -70,3 +79,52 @@ class CallbackQuery(TelegramObject):
def answer(self, *args, **kwargs):
"""Shortcut for ``bot.answerCallbackQuery(update.callback_query.id, *args, **kwargs)``"""
return self.bot.answerCallbackQuery(self.id, *args, **kwargs)
def edit_message_text(self, *args, **kwargs):
"""
Shortcut for either ``bot.editMessageText(chat_id=update.callback_query.message.chat_id, \
message_id=update.callback_query.message.message_id, \
*args, **kwargs)``
or ``bot.editMessageText(inline_message_id=update.callback_query.inline_message_id, \
*args, **kwargs)``
"""
if self.inline_message_id:
return self.bot.edit_message_text(
inline_message_id=self.inline_message_id, *args, **kwargs)
else:
return self.bot.edit_message_text(
chat_id=self.message.chat_id, message_id=self.message.message_id, *args, **kwargs)
def edit_message_caption(self, *args, **kwargs):
"""
Shortcut for either
``bot.editMessageCaption(chat_id=update.callback_query.message.chat_id, \
message_id=update.callback_query.message.message_id, \
*args, **kwargs)``
or
``bot.editMessageCaption(inline_message_id=update.callback_query.inline_message_id, \
*args, **kwargs)``
"""
if self.inline_message_id:
return self.bot.edit_message_caption(
inline_message_id=self.inline_message_id, *args, **kwargs)
else:
return self.bot.edit_message_caption(
chat_id=self.message.chat_id, message_id=self.message.message_id, *args, **kwargs)
def edit_message_reply_markup(self, *args, **kwargs):
"""
Shortcut for either
``bot.editMessageReplyMarkup(chat_id=update.callback_query.message.chat_id, \
message_id=update.callback_query.message.message_id, \
*args, **kwargs)``
or
``bot.editMessageReplyMarkup(inline_message_id=update.callback_query.inline_message_id, \
*args, **kwargs)``
"""
if self.inline_message_id:
return self.bot.edit_message_reply_markup(
inline_message_id=self.inline_message_id, *args, **kwargs)
else:
return self.bot.edit_message_reply_markup(
chat_id=self.message.chat_id, message_id=self.message.message_id, *args, **kwargs)

View file

@ -37,28 +37,38 @@ class Chat(TelegramObject):
Args:
id (int):
type (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
type (Optional[str]):
title (Optional[str]):
username(Optional[str]):
first_name(Optional[str]):
last_name(Optional[str]):
bot (Optional[Bot]): The Bot to use for instance methods
"""
**kwargs (dict): Arbitrary keyword arguments.
"""
PRIVATE = 'private'
GROUP = 'group'
SUPERGROUP = 'supergroup'
CHANNEL = 'channel'
def __init__(self, id, type, bot=None, **kwargs):
def __init__(self,
id,
type,
title='',
username='',
first_name='',
last_name='',
all_members_are_admins=False,
bot=None,
**kwargs):
# Required
self.id = int(id)
self.type = type
# Optionals
self.title = kwargs.get('title', '')
self.username = kwargs.get('username', '')
self.first_name = kwargs.get('first_name', '')
self.last_name = kwargs.get('last_name', '')
self.all_members_are_admins = kwargs.get('all_members_are_admins', False)
self.title = title
self.username = username
self.first_name = first_name
self.last_name = last_name
self.all_members_are_admins = all_members_are_admins
self.bot = bot

View file

@ -32,8 +32,9 @@ class ChatMember(TelegramObject):
Args:
user (:class:`telegram.User`):
status (str):
"""
**kwargs (dict): Arbitrary keyword arguments.
"""
CREATOR = 'creator'
ADMINISTRATOR = 'administrator'
MEMBER = 'member'

View file

@ -33,11 +33,16 @@ class ChosenInlineResult(TelegramObject):
result_id (str):
from_user (:class:`telegram.User`):
query (str):
location (:class:`telegram.Location`):
inline_message_id (str):
Args:
result_id (str):
from_user (:class:`telegram.User`):
query (str):
location (Optional[:class:`telegram.Location`]):
inline_message_id (Optional[str]):
**kwargs (dict): Arbitrary keyword arguments.
"""
@ -69,7 +74,7 @@ class ChosenInlineResult(TelegramObject):
if not data:
return None
# Required
# Required
data['from_user'] = User.de_json(data.pop('from'), bot)
# Optionals
data['location'] = Location.de_json(data.get('location'), bot)

View file

@ -33,20 +33,19 @@ class Contact(TelegramObject):
Args:
phone_number (str):
first_name (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
last_name (Optional[str]):
user_id (Optional[int]):
**kwargs: Arbitrary keyword arguments.
"""
def __init__(self, phone_number, first_name, **kwargs):
def __init__(self, phone_number, first_name, last_name='', user_id=0, **kwargs):
# Required
self.phone_number = str(phone_number)
self.first_name = first_name
# Optionals
self.last_name = kwargs.get('last_name', '')
self.user_id = int(kwargs.get('user_id', 0))
self.last_name = last_name
self.user_id = int(user_id)
@staticmethod
def de_json(data, bot):

View file

@ -33,23 +33,22 @@ class Document(TelegramObject):
Args:
file_id (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
thumb (Optional[:class:`telegram.PhotoSize`]):
file_name (Optional[str]):
mime_type (Optional[str]):
file_size (Optional[int]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self, file_id, **kwargs):
def __init__(self, file_id, thumb=None, file_name='', mime_type='', file_size=0, **kwargs):
# Required
self.file_id = str(file_id)
# Optionals
self.thumb = kwargs.get('thumb')
self.file_name = kwargs.get('file_name', '')
self.mime_type = str(kwargs.get('mime_type', ''))
self.file_size = int(kwargs.get('file_size', 0))
self.thumb = thumb
self.file_name = file_name
self.mime_type = str(mime_type)
self.file_size = int(file_size)
@staticmethod
def de_json(data, bot):

View file

@ -26,7 +26,8 @@ from .choseninlineresulthandler import ChosenInlineResultHandler
from .commandhandler import CommandHandler
from .handler import Handler
from .inlinequeryhandler import InlineQueryHandler
from .messagehandler import MessageHandler, Filters
from .messagehandler import MessageHandler
from .filters import BaseFilter, Filters
from .regexhandler import RegexHandler
from .stringcommandhandler import StringCommandHandler
from .stringregexhandler import StringRegexHandler
@ -35,5 +36,5 @@ from .conversationhandler import ConversationHandler
__all__ = ('Dispatcher', 'JobQueue', 'Job', 'Updater', 'CallbackQueryHandler',
'ChosenInlineResultHandler', 'CommandHandler', 'Handler', 'InlineQueryHandler',
'MessageHandler', 'Filters', 'RegexHandler', 'StringCommandHandler',
'MessageHandler', 'BaseFilter', 'Filters', 'RegexHandler', 'StringCommandHandler',
'StringRegexHandler', 'TypeHandler', 'ConversationHandler')

216
telegram/ext/filters.py Normal file
View file

@ -0,0 +1,216 @@
#!/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 the Filters for use with the MessageHandler class """
class BaseFilter(object):
"""Base class for all Message Filters
Subclassing from this class filters to be combined using bitwise operators:
And:
>>> (Filters.text & Filters.entity(MENTION))
Or:
>>> (Filters.audio | Filters.video)
Also works with more than two filters:
>>> (Filters.text & (Filters.entity(URL) | Filters.entity(TEXT_LINK)))
If you want to create your own filters create a class inheriting from this class and implement
a `filter` method that returns a boolean: `True` if the message should be handled, `False`
otherwise. Note that the filters work only as class instances, not actual class objects
(so remember to initialize your filter classes).
"""
def __call__(self, message):
return self.filter(message)
def __and__(self, other):
return MergedFilter(self, and_filter=other)
def __or__(self, other):
return MergedFilter(self, or_filter=other)
def filter(self, message):
raise NotImplementedError
class MergedFilter(BaseFilter):
"""Represents a filter consisting of two other filters.
Args:
base_filter: Filter 1 of the merged filter
and_filter: Optional filter to "and" with base_filter. Mutually exclusive with or_filter.
or_filter: Optional filter to "or" with base_filter. Mutually exclusive with and_filter.
"""
def __init__(self, base_filter, and_filter=None, or_filter=None):
self.base_filter = base_filter
self.and_filter = and_filter
self.or_filter = or_filter
def filter(self, message):
if self.and_filter:
return self.base_filter(message) and self.and_filter(message)
elif self.or_filter:
return self.base_filter(message) or self.or_filter(message)
def __str__(self):
return ("<telegram.ext.filters.MergedFilter consisting of"
" {} {} {}>").format(self.base_filter, "and" if self.and_filter else "or",
self.and_filter or self.or_filter)
__repr__ = __str__
class Filters(object):
"""
Predefined filters for use with the `filter` argument of :class:`telegram.ext.MessageHandler`.
"""
class _All(BaseFilter):
def filter(self, message):
return True
all = _All()
class _Text(BaseFilter):
def filter(self, message):
return bool(message.text and not message.text.startswith('/'))
text = _Text()
class _Command(BaseFilter):
def filter(self, message):
return bool(message.text and message.text.startswith('/'))
command = _Command()
class _Audio(BaseFilter):
def filter(self, message):
return bool(message.audio)
audio = _Audio()
class _Document(BaseFilter):
def filter(self, message):
return bool(message.document)
document = _Document()
class _Photo(BaseFilter):
def filter(self, message):
return bool(message.photo)
photo = _Photo()
class _Sticker(BaseFilter):
def filter(self, message):
return bool(message.sticker)
sticker = _Sticker()
class _Video(BaseFilter):
def filter(self, message):
return bool(message.video)
video = _Video()
class _Voice(BaseFilter):
def filter(self, message):
return bool(message.voice)
voice = _Voice()
class _Contact(BaseFilter):
def filter(self, message):
return bool(message.contact)
contact = _Contact()
class _Location(BaseFilter):
def filter(self, message):
return bool(message.location)
location = _Location()
class _Venue(BaseFilter):
def filter(self, message):
return bool(message.venue)
venue = _Venue()
class _StatusUpdate(BaseFilter):
def filter(self, message):
return bool(message.new_chat_member or message.left_chat_member
or message.new_chat_title or message.new_chat_photo
or message.delete_chat_photo or message.group_chat_created
or message.supergroup_chat_created or message.channel_chat_created
or message.migrate_to_chat_id or message.migrate_from_chat_id
or message.pinned_message)
status_update = _StatusUpdate()
class _Forwarded(BaseFilter):
def filter(self, message):
return bool(message.forward_date)
forwarded = _Forwarded()
class _Game(BaseFilter):
def filter(self, message):
return bool(message.game)
game = _Game()
class entity(BaseFilter):
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
where their `type` matches `entity_type`.
Args:
entity_type: Entity type to check for. All types can be found as constants
in :class:`telegram.MessageEntity`.
Returns: function to use as filter
"""
def __init__(self, entity_type):
self.entity_type = entity_type
def filter(self, message):
return any([entity.type == self.entity_type for entity in message.entities])

View file

@ -17,96 +17,13 @@
# 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 the MessageHandler class """
import warnings
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class Filters(object):
"""
Convenient namespace (class) & methods for the filter funcs of the
MessageHandler class.
"""
@staticmethod
def text(message):
return message.text and not message.text.startswith('/')
@staticmethod
def command(message):
return message.text and message.text.startswith('/')
@staticmethod
def audio(message):
return bool(message.audio)
@staticmethod
def document(message):
return bool(message.document)
@staticmethod
def photo(message):
return bool(message.photo)
@staticmethod
def sticker(message):
return bool(message.sticker)
@staticmethod
def video(message):
return bool(message.video)
@staticmethod
def voice(message):
return bool(message.voice)
@staticmethod
def contact(message):
return bool(message.contact)
@staticmethod
def location(message):
return bool(message.location)
@staticmethod
def venue(message):
return bool(message.venue)
@staticmethod
def game(message):
return bool(message.game)
@staticmethod
def status_update(message):
return bool(message.new_chat_member or message.left_chat_member or message.new_chat_title
or message.new_chat_photo or message.delete_chat_photo
or message.group_chat_created or message.supergroup_chat_created
or message.channel_chat_created or message.migrate_to_chat_id
or message.migrate_from_chat_id or message.pinned_message)
@staticmethod
def forwarded(message):
return bool(message.forward_date)
@staticmethod
def entity(entity_type):
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
where their `type` matches `entity_type`.
Args:
entity_type: Entity type to check for. All types can be found as constants
in :class:`telegram.MessageEntity`.
Returns: function to use as filter
"""
def entities_filter(message):
return any([entity.type == entity_type for entity in message.entities])
return entities_filter
class MessageHandler(Handler):
"""
Handler class to handle telegram messages. Messages are Telegram Updates
@ -114,12 +31,10 @@ class MessageHandler(Handler):
updates.
Args:
filters (list[function]): A list of filter functions. Standard filters
can be found in the Filters class above.
| Each `function` takes ``Update`` as arg and returns ``bool``.
| All messages that match at least one of those filters will be
accepted. If ``bool(filters)`` evaluates to ``False``, messages are
not filtered.
filters (telegram.ext.BaseFilter): A filter inheriting from
:class:`telegram.filters.BaseFilter`. Standard filters can be found in
:class:`telegram.filters.Filters`. Filters can be combined using bitwise
operators (& for and, | for or).
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
@ -141,6 +56,13 @@ class MessageHandler(Handler):
self.filters = filters
self.allow_edited = allow_edited
# We put this up here instead of with the rest of checking code
# in check_update since we don't wanna spam a ton
if isinstance(self.filters, list):
warnings.warn('Using a list of filters in MessageHandler is getting '
'deprecated, please use bitwise operators (& and |) '
'instead. More info: https://git.io/vPTbc.')
def check_update(self, update):
if (isinstance(update, Update)
and (update.message or update.edited_message and self.allow_edited)):
@ -150,7 +72,10 @@ class MessageHandler(Handler):
else:
message = update.message or update.edited_message
if isinstance(self.filters, list):
res = any(func(message) for func in self.filters)
else:
res = self.filters(message)
else:
res = False
@ -162,8 +87,7 @@ class MessageHandler(Handler):
return self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
# old non-PEP8 Handler methods
m = "telegram.MessageHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -34,21 +34,19 @@ class File(TelegramObject):
Args:
file_id (str):
bot (telegram.Bot):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
file_size (Optional[int]):
file_path (Optional[str]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self, file_id, bot, **kwargs):
def __init__(self, file_id, bot, file_size=0, file_path='', **kwargs):
# Required
self.file_id = str(file_id)
# Optionals
self.file_size = int(kwargs.get('file_size', 0))
self.file_path = str(kwargs.get('file_path', ''))
self.file_size = int(file_size)
self.file_path = str(file_path)
self.bot = bot

View file

@ -30,17 +30,16 @@ class ForceReply(ReplyMarkup):
Args:
force_reply (bool):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
selective (Optional[bool]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self, force_reply=True, **kwargs):
def __init__(self, force_reply=True, selective=False, **kwargs):
# Required
self.force_reply = bool(force_reply)
# Optionals
self.selective = bool(kwargs.get('selective', False))
self.selective = bool(selective)
@staticmethod
def de_json(data, bot):

View file

@ -35,8 +35,6 @@ class InlineKeyboardButton(TelegramObject):
Args:
text (str): Label text on the button.
Keyword Args:
url (Optional[str]): HTTP url to be opened when button is pressed.
callback_data (Optional[str]): Data to be sent in a callback query to the bot when button
is pressed, 1-64 bytes.
@ -49,17 +47,18 @@ class InlineKeyboardButton(TelegramObject):
Can be empty, in which case only the bot's username will be inserted.
callback_game (Optional[:class:`telegram.CallbackGame`]): Description of the game that will
be launched when the user presses the button.
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self, text, **kwargs):
def __init__(self, text, url=None, callback_data=None, switch_inline_query=None, **kwargs):
# Required
self.text = text
# Optionals
self.url = kwargs.get('url')
self.callback_data = kwargs.get('callback_data')
self.switch_inline_query = kwargs.get('switch_inline_query')
self.url = url
self.callback_data = callback_data
self.switch_inline_query = switch_inline_query
self.switch_inline_query_current_chat = kwargs.get('switch_inline_query_current_chat')
self.callback_game = kwargs.get('callback_game')

View file

@ -30,6 +30,7 @@ class InlineKeyboardMarkup(ReplyMarkup):
Args:
inline_keyboard (List[List[:class:`telegram.InlineKeyboardButton`]]):
**kwargs (dict): Arbitrary keyword arguments.
"""
@ -46,6 +47,7 @@ class InlineKeyboardMarkup(ReplyMarkup):
Returns:
telegram.InlineKeyboardMarkup:
"""
data = super(InlineKeyboardMarkup, InlineKeyboardMarkup).de_json(data, bot)

View file

@ -38,14 +38,13 @@ class InlineQuery(TelegramObject):
from_user (:class:`telegram.User`):
query (str):
offset (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
location (optional[:class:`telegram.Location`]):
bot (Optional[Bot]): The Bot to use for instance methods
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self, id, from_user, query, offset, bot=None, **kwargs):
def __init__(self, id, from_user, query, offset, location=None, bot=None, **kwargs):
# Required
self.id = id
self.from_user = from_user
@ -53,7 +52,7 @@ class InlineQuery(TelegramObject):
self.offset = offset
# Optional
self.location = kwargs.get('location')
self.location = location
self.bot = bot

View file

@ -26,12 +26,13 @@ class InlineQueryResult(TelegramObject):
"""This object represents a Telegram InlineQueryResult.
Attributes:
type (str):
id (str):
type (str): Type of the result.
id (str): Unique identifier for this result, 1-64 Bytes
Args:
type (str):
type (str): Type of the result.
id (str): Unique identifier for this result, 1-64 Bytes
**kwargs (dict): Arbitrary keyword arguments.
"""

View file

@ -42,21 +42,20 @@ class InlineQueryResultArticle(InlineQueryResult):
parse_mode (str): Use :class:`InputTextMessageContent` instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent`
instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent` instead.
Args:
id (str): Unique identifier for this result, 1-64 Bytes
title (str):
reply_markup (:class:`telegram.ReplyMarkup`):
Keyword Args:
url (Optional[str]):
hide_url (Optional[bool]):
description (Optional[str]):
thumb_url (Optional[str]):
thumb_width (Optional[int]):
thumb_height (Optional[int]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,

View file

@ -35,29 +35,25 @@ class InlineQueryResultAudio(InlineQueryResult):
audio_duration (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
input_message_content (Optional[:class:`telegram.input_message_content`]):
Deprecated: 4.0
message_text (str): Use :class:`InputTextMessageContent` instead.
parse_mode (str): Use :class:`InputTextMessageContent` instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent`
instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent` instead.
Args:
audio_url (str):
title (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
performer (Optional[str]):
audio_duration (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
input_message_content (Optional[:class:`telegram.input_message_content`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,

View file

@ -23,36 +23,31 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageConten
class InlineQueryResultCachedAudio(InlineQueryResult):
"""Represents a link to an mp3 audio file stored on the Telegram
servers. 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.
"""Represents a link to an mp3 audio file stored on the Telegram servers. 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_file_id (str):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
input_message_content (Optional[:class:`telegram.input_message_content`]):
Deprecated: 4.0
message_text (str): Use :class:`InputTextMessageContent` instead.
parse_mode (str): Use :class:`InputTextMessageContent` instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent`
instead.
disable_web_page_preview (bool): Use :class:`InputTextMessageContent` instead.
Args:
audio_file_id (str):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[
:class:`telegram.input_message_content`]):
input_message_content (Optional[:class:`telegram.input_message_content`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,

View file

@ -16,13 +16,38 @@
#
# 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 the classes that represent Telegram
InlineQueryResultCachedDocument"""
"""This module contains the classes that represent Telegram InlineQueryResultCachedDocument"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultCachedDocument(InlineQueryResult):
"""Represents a link to a file stored on the Telegram servers. By default, this file will be
sent by the user with an optional caption. Alternatively, you can use input_message_content to
send a message with the specified content instead of the file. Currently, only pdf-files and
zip archives can be sent using this method.
Attributes:
title (str): Title for the result.
document_file_id (str): A valid file identifier for the file.
description (Optional[str]): Short description of the result.
caption (Optional[str]): Caption of the document to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the file.
Args:
id (str):
title (str):
document_file_id (str):
description (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -23,6 +23,29 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageConten
class InlineQueryResultCachedGif(InlineQueryResult):
"""Represents a link to an animated GIF file stored on the Telegram servers. By default, this
animated GIF file will be sent by the user with an optional caption. Alternatively, you can use
input_message_content to send a message with specified content instead of the animation.
Attributes:
gif_file_id (str): A valid file identifier for the GIF file.
title (Optional[str]): Title for the result.
caption (Optional[str]): Caption of the GIF file to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the GIF animation.
Args:
id (str):
gif_file_id (str):
title (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -23,6 +23,30 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageConten
class InlineQueryResultCachedMpeg4Gif(InlineQueryResult):
"""Represents a link to a video animation (H.264/MPEG-4 AVC video without sound) stored on the
Telegram servers. By default, this animated MPEG-4 file will be sent by the user with an
optional caption. Alternatively, you can use input_message_content to send a message with the
specified content instead of the animation.
Attributes:
mpeg4_file_id (str): A valid file identifier for the MP4 file.
title (Optional[str]): Title for the result.
caption (Optional[str]): Caption of the MPEG-4 file to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the video animation
Args:
id (str):
mpeg4_file_id (str):
title (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,37 @@
#
# 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 the classes that represent Telegram
InlineQueryResultPhoto"""
"""This module contains the classes that represent Telegram InlineQueryResultPhoto"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultCachedPhoto(InlineQueryResult):
"""Represents a link to a photo stored on the Telegram servers. By default, this photo will be
sent by the user with an optional caption. Alternatively, you can use input_message_content to
send a message with the specified content instead of the photo.
Attributes:
photo_file_id (str): A valid file identifier of the photo.
title (Optional[str]): Title for the result.
description (Optional[str]): Short description of the result.
caption (Optional[str]): Caption of the photo to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the photo
Args:
id (str):
photo_file_id (str):
title (Optional[str]):
description (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,31 @@
#
# 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 the classes that represent Telegram
InlineQueryResultCachedSticker"""
"""This module contains the classes that represent Telegram InlineQueryResultCachedSticker"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultCachedSticker(InlineQueryResult):
"""Represents a link to a sticker stored on the Telegram servers. By default, this sticker will
be sent by the user. Alternatively, you can use input_message_content to send a message with
the specified content instead of the sticker.
Attributes:
sticker_file_id (str): A valid file identifier of the sticker.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the sticker.
Args:
id (str):
sticker_file_id (str):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,37 @@
#
# 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 the classes that represent Telegram
InlineQueryResultCachedVideo"""
"""This module contains the classes that represent Telegram InlineQueryResultCachedVideo"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultCachedVideo(InlineQueryResult):
"""Represents a link to a video file stored on the Telegram servers. By default, this video
file will be sent by the user with an optional caption. Alternatively, you can use
input_message_content to send a message with the specified content instead of the video.
Attributes:
video_file_id (str): A valid file identifier for the video file.
title (str): Title for the result.
description (Optional[str]): Short description of the result.
caption (Optional[str]): Caption of the video to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the video
Args:
id (str):
video_file_id (str):
title (str):
description (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,35 @@
#
# 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 the classes that represent Telegram
InlineQueryResultCachedVoice"""
"""This module contains the classes that represent Telegram InlineQueryResultCachedVoice"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultCachedVoice(InlineQueryResult):
"""Represents a link to a voice message stored on the Telegram servers. By default, this voice
message will be sent by the user. Alternatively, you can use input_message_content to send a
message with the specified content instead of the voice message.
Attributes:
voice_file_id (str): A valid file identifier for the voice message.
title (str): Voice message title.
caption (Optional[str]): Caption, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the voice message.
Args:
id (str):
voice_file_id (str):
title (str):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,41 @@
#
# 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 the classes that represent Telegram
InlineQueryResultContact"""
"""This module contains the classes that represent Telegram InlineQueryResultContact"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultContact(InlineQueryResult):
"""Represents a contact with a phone number. By default, this contact will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the contact.
Attributes:
phone_number (str): Contact's phone number.
first_name (str): Contact's first name.
last_name (Optional[str]): Contact's last name.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the contact.
thumb_url (Optional[str]): Url of the thumbnail for the result.
thumb_width (Optional[int]): Thumbnail width.
thumb_height (Optional[int]): Thumbnail height.
Args:
id (str):
phone_number (str):
first_name (str):
last_name (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
thumb_url (Optional[str]): Url of the thumbnail for the result.
thumb_width (Optional[int]):
thumb_height (Optional[int]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,46 @@
#
# 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 the classes that represent Telegram
InlineQueryResultDocument"""
"""This module contains the classes that represent Telegram InlineQueryResultDocument"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultDocument(InlineQueryResult):
"""Represents a link to a file. By default, this file will be sent by the user with an optional
caption. Alternatively, you can use input_message_content to send a message with the specified
content instead of the file. Currently, only .PDF and .ZIP files can be sent using this method.
Attributes:
title (str): Title for the result.
caption (Optional[str]): Caption of the document to be sent, 0-200 characters.
document_url (Optional[str]): A valid URL for the file.
mime_type (Optional[str]): Mime type of the content of the file, either "application/pdf"
or "application/zip".
description (Optional[str]): Short description of the result.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the file.
thumb_url (Optional[str]): URL of the thumbnail (jpeg only) for the file.
thumb_width (Optional[int]): Thumbnail width.
thumb_height (Optional[int]): Thumbnail height.
Args:
id (str):
document_url (str):
title (str):
mime_type (str):
caption (Optional[str]):
description (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
thumb_url (Optional[str]):
thumb_width (Optional[int]):
thumb_height (Optional[int]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -23,6 +23,35 @@ from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageConten
class InlineQueryResultGif(InlineQueryResult):
"""Represents a link to an animated GIF file. By default, this animated GIF file will be sent
by the user with optional caption. Alternatively, you can use input_message_content to send a
message with the specified content instead of the animation.
Attributes:
gif_url (str): A valid URL for the GIF file. File size must not exceed 1MB.
thumb_url (str): URL of the static thumbnail for the result (jpeg or gif).
gif_width (Optional[int]): Width of the GIF.
gif_height (Optional[int]): Height of the GIF.
title (Optional[str]): Title for the result.
caption (Optional[str]): Caption of the GIF file to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the GIF animation.
Args:
id (str):
gif_url (str):
thumb_url (str):
gif_width (Optional[int]):
gif_height (Optional[int]):
title (Optional[str]):
caption (Optional[str]):
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
input_message_content (Optional[:class:`telegram.InputMessageContent`]):
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,42 @@
#
# 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 the classes that represent Telegram
InlineQueryResultLocation"""
"""This module contains the classes that represent Telegram InlineQueryResultLocation"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultLocation(InlineQueryResult):
"""Represents a location on a map. By default, the location will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the location.
Attributes:
latitude (float): Location latitude in degrees.
longitude (float): Location longitude in degrees.
title (str): Location title.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the location.
thumb_url (Optional[str]): Url of the thumbnail for the result.
thumb_width (Optional[int]): Thumbnail width.
thumb_height (Optional[int]): Thumbnail height.
Args:
latitude (float): Location latitude in degrees.
longitude (float): Location longitude in degrees.
title (str): Location title.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the location.
thumb_url (Optional[str]): Url of the thumbnail for the result.
thumb_width (Optional[int]): Thumbnail width.
thumb_height (Optional[int]): Thumbnail height.
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,43 @@
#
# 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 the classes that represent Telegram
InlineQueryResultMpeg4Gif"""
"""This module contains the classes that represent Telegram InlineQueryResultMpeg4Gif"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultMpeg4Gif(InlineQueryResult):
"""Represents a link to a video animation (H.264/MPEG-4 AVC video without sound). By default,
this animated MPEG-4 file will be sent by the user with optional caption. Alternatively, you
can use input_message_content to send a message with the specified content instead of the
animation.
Attributes:
mpeg4_url (str): A valid URL for the MP4 file. File size must not exceed 1MB.
thumb_url (str): URL of the static thumbnail (jpeg or gif) for the result.
mpeg4_width (Optional[int]): Video width.
mpeg4_height (Optional[int]): Video height.
title (Optional[str]): Title for the result.
caption (Optional[str]): Caption of the MPEG-4 file to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the video animation.
Args:
mpeg4_url (str): A valid URL for the MP4 file. File size must not exceed 1MB.
thumb_url (str): URL of the static thumbnail (jpeg or gif) for the result.
mpeg4_width (Optional[int]): Video width.
mpeg4_height (Optional[int]): Video height.
title (Optional[str]): Title for the result.
caption (Optional[str]): Caption of the MPEG-4 file to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the video animation.
**kwargs (dict): Arbitrary keyword arguments.
"""
def __init__(self,
id,

View file

@ -16,13 +16,31 @@
#
# 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 the classes that represent Telegram
InlineQueryResultPhoto"""
"""This module contains the classes that represent Telegram InlineQueryResultPhoto"""
from telegram import InlineQueryResult, InlineKeyboardMarkup, InputMessageContent
class InlineQueryResultPhoto(InlineQueryResult):
"""Represents a link to a photo. By default, this photo will be sent by the user with optional
caption. Alternatively, you can use input_message_content to send a message with the specified
content instead of the photo.
Attributes:
photo_url (str): A valid URL of the photo. Photo must be in jpeg format. Photo size must
not exceed 5MB.
thumb_url (str): URL of the thumbnail for the photo.
photo_width (Optional[int]): Width of the photo.
photo_height (Optional[int]): Height of the photo.
title (Optional[str]): Title for the result.
description (Optional[str]): Short description of the result.
caption (Optional[str]): Caption of the photo to be sent, 0-200 characters.
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]): Inline keyboard attached
to the message.
input_message_content (Optional[:class:`telegram.InputMessageContent`]): Content of the
message to be sent instead of the photo.
"""
def __init__(self,
id,

View file

@ -108,42 +108,76 @@ class Message(TelegramObject):
bot (Optional[Bot]): The Bot to use for instance methods
"""
def __init__(self, message_id, from_user, date, chat, bot=None, **kwargs):
def __init__(self,
message_id,
from_user,
date,
chat,
forward_from=None,
forward_from_chat=None,
forward_date=None,
reply_to_message=None,
edit_date=None,
text='',
entities=None,
audio=None,
document=None,
photo=None,
sticker=None,
video=None,
voice=None,
caption='',
contact=None,
location=None,
venue=None,
new_chat_member=None,
left_chat_member=None,
new_chat_title='',
new_chat_photo=None,
delete_chat_photo=False,
group_chat_created=False,
supergroup_chat_created=False,
migrate_to_chat_id=0,
migrate_from_chat_id=0,
channel_chat_created=False,
pinned_message=None,
bot=None,
**kwargs):
# Required
self.message_id = int(message_id)
self.from_user = from_user
self.date = date
self.chat = chat
# Optionals
self.forward_from = kwargs.get('forward_from')
self.forward_from_chat = kwargs.get('forward_from_chat')
self.forward_date = kwargs.get('forward_date')
self.reply_to_message = kwargs.get('reply_to_message')
self.edit_date = kwargs.get('edit_date')
self.text = kwargs.get('text', '')
self.entities = kwargs.get('entities', list())
self.audio = kwargs.get('audio')
self.forward_from = forward_from
self.forward_from_chat = forward_from_chat
self.forward_date = forward_date
self.reply_to_message = reply_to_message
self.edit_date = edit_date
self.text = text
self.entities = entities or list()
self.audio = audio
self.game = kwargs.get('game')
self.document = kwargs.get('document')
self.photo = kwargs.get('photo')
self.sticker = kwargs.get('sticker')
self.video = kwargs.get('video')
self.voice = kwargs.get('voice')
self.caption = kwargs.get('caption', '')
self.contact = kwargs.get('contact')
self.location = kwargs.get('location')
self.venue = kwargs.get('venue')
self.new_chat_member = kwargs.get('new_chat_member')
self.left_chat_member = kwargs.get('left_chat_member')
self.new_chat_title = kwargs.get('new_chat_title', '')
self.new_chat_photo = kwargs.get('new_chat_photo')
self.delete_chat_photo = bool(kwargs.get('delete_chat_photo', False))
self.group_chat_created = bool(kwargs.get('group_chat_created', False))
self.supergroup_chat_created = bool(kwargs.get('supergroup_chat_created', False))
self.migrate_to_chat_id = int(kwargs.get('migrate_to_chat_id', 0))
self.migrate_from_chat_id = int(kwargs.get('migrate_from_chat_id', 0))
self.channel_chat_created = bool(kwargs.get('channel_chat_created', False))
self.pinned_message = kwargs.get('pinned_message')
self.document = document
self.photo = photo
self.sticker = sticker
self.video = video
self.voice = voice
self.caption = caption
self.contact = contact
self.location = location
self.venue = venue
self.new_chat_member = new_chat_member
self.left_chat_member = left_chat_member
self.new_chat_title = new_chat_title
self.new_chat_photo = new_chat_photo
self.delete_chat_photo = bool(delete_chat_photo)
self.group_chat_created = bool(group_chat_created)
self.supergroup_chat_created = bool(supergroup_chat_created)
self.migrate_to_chat_id = int(migrate_to_chat_id)
self.migrate_from_chat_id = int(migrate_from_chat_id)
self.channel_chat_created = bool(channel_chat_created)
self.pinned_message = pinned_message
self.bot = bot
@ -416,6 +450,48 @@ class Message(TelegramObject):
disable_notification=disable_notification,
message_id=self.message_id)
def edit_text(self, *args, **kwargs):
"""
Shortcut for ``bot.editMessageText(chat_id=message.chat_id,
message_id=message.message_id,
*args, **kwargs)``
Note:
You can only edit messages that the bot sent itself,
therefore this method can only be used on the
return value of the bot.send_* family of methods.
"""
return self.bot.edit_message_text(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
def edit_caption(self, *args, **kwargs):
"""
Shortcut for ``bot.editMessageCaption(chat_id=message.chat_id,
message_id=message.message_id,
*args, **kwargs)``
Note:
You can only edit messages that the bot sent itself,
therefore this method can only be used on the
return value of the bot.send_* family of methods.
"""
return self.bot.edit_message_caption(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
def edit_reply_markup(self, *args, **kwargs):
"""
Shortcut for ``bot.editReplyMarkup(chat_id=message.chat_id,
message_id=message.message_id,
*args, **kwargs)``
Note:
You can only edit messages that the bot sent itself,
therefore this method can only be used on the
return value of the bot.send_* family of methods.
"""
return self.bot.edit_message_caption(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
def parse_entity(self, entity):
"""
Returns the text from a given :class:`telegram.MessageEntity`.

View file

@ -34,14 +34,14 @@ class MessageEntity(TelegramObject):
user (Optional[:class:`telegram.User`]):
"""
def __init__(self, type, offset, length, **kwargs):
def __init__(self, type, offset, length, url=None, user=None, **kwargs):
# Required
self.type = type
self.offset = offset
self.length = length
# Optionals
self.url = kwargs.get('url')
self.user = kwargs.get('user')
self.url = url
self.user = user
@staticmethod
def de_json(data, bot):

View file

@ -40,13 +40,13 @@ class PhotoSize(TelegramObject):
file_size (Optional[int]):
"""
def __init__(self, file_id, width, height, **kwargs):
def __init__(self, file_id, width, height, file_size=0, **kwargs):
# Required
self.file_id = str(file_id)
self.width = int(width)
self.height = int(height)
# Optionals
self.file_size = int(kwargs.get('file_size', 0))
self.file_size = int(file_size)
def __eq__(self, other):
if not isinstance(other, self.__class__):

View file

@ -37,11 +37,11 @@ class ReplyKeyboardHide(ReplyMarkup):
selective (Optional[bool]):
"""
def __init__(self, hide_keyboard=True, **kwargs):
def __init__(self, hide_keyboard=True, selective=False, **kwargs):
# Required
self.hide_keyboard = bool(hide_keyboard)
# Optionals
self.selective = bool(kwargs.get('selective', False))
self.selective = bool(selective)
@staticmethod
def de_json(data, bot):

View file

@ -41,13 +41,18 @@ class ReplyKeyboardMarkup(ReplyMarkup):
selective (Optional[bool]):
"""
def __init__(self, keyboard, **kwargs):
def __init__(self,
keyboard,
resize_keyboard=False,
one_time_keyboard=False,
selective=False,
**kwargs):
# Required
self.keyboard = keyboard
# Optionals
self.resize_keyboard = bool(kwargs.get('resize_keyboard', False))
self.one_time_keyboard = bool(kwargs.get('one_time_keyboard', False))
self.selective = bool(kwargs.get('selective', False))
self.resize_keyboard = bool(resize_keyboard)
self.one_time_keyboard = bool(one_time_keyboard)
self.selective = bool(selective)
@staticmethod
def de_json(data, bot):

View file

@ -44,15 +44,15 @@ class Sticker(TelegramObject):
file_size (Optional[int]):
"""
def __init__(self, file_id, width, height, **kwargs):
def __init__(self, file_id, width, height, thumb=None, emoji='', file_size=0, **kwargs):
# Required
self.file_id = str(file_id)
self.width = int(width)
self.height = int(height)
# Optionals
self.thumb = kwargs.get('thumb')
self.emoji = kwargs.get('emoji', '')
self.file_size = int(kwargs.get('file_size', 0))
self.thumb = thumb
self.emoji = emoji
self.file_size = int(file_size)
@staticmethod
def de_json(data, bot):

View file

@ -44,15 +44,22 @@ class Update(TelegramObject):
callback_query (Optional[:class:`telegram.CallbackQuery`]):
"""
def __init__(self, update_id, **kwargs):
def __init__(self,
update_id,
message=None,
edited_message=None,
inline_query=None,
chosen_inline_result=None,
callback_query=None,
**kwargs):
# Required
self.update_id = int(update_id)
# Optionals
self.message = kwargs.get('message')
self.edited_message = kwargs.get('edited_message')
self.inline_query = kwargs.get('inline_query')
self.chosen_inline_result = kwargs.get('chosen_inline_result')
self.callback_query = kwargs.get('callback_query')
self.message = message
self.edited_message = edited_message
self.inline_query = inline_query
self.chosen_inline_result = chosen_inline_result
self.callback_query = callback_query
@staticmethod
def de_json(data, bot):

View file

@ -44,14 +44,14 @@ class User(TelegramObject):
bot (Optional[Bot]): The Bot to use for instance methods
"""
def __init__(self, id, first_name, bot=None, **kwargs):
def __init__(self, id, first_name, type='', last_name='', username='', bot=None, **kwargs):
# Required
self.id = int(id)
self.first_name = first_name
# Optionals
self.type = kwargs.get('type', '')
self.last_name = kwargs.get('last_name', '')
self.username = kwargs.get('username', '')
self.type = type
self.last_name = last_name
self.username = username
self.bot = bot

View file

@ -46,16 +46,24 @@ class Video(TelegramObject):
file_size (Optional[int]):
"""
def __init__(self, file_id, width, height, duration, **kwargs):
def __init__(self,
file_id,
width,
height,
duration,
thumb=None,
mime_type='',
file_size=0,
**kwargs):
# Required
self.file_id = str(file_id)
self.width = int(width)
self.height = int(height)
self.duration = int(duration)
# Optionals
self.thumb = kwargs.get('thumb')
self.mime_type = str(kwargs.get('mime_type', ''))
self.file_size = int(kwargs.get('file_size', 0))
self.thumb = thumb
self.mime_type = str(mime_type)
self.file_size = int(file_size)
@staticmethod
def de_json(data, bot):

View file

@ -32,21 +32,21 @@ class Voice(TelegramObject):
Args:
file_id (str):
duration (Optional[int]):
**kwargs: Arbitrary keyword arguments.
Keyword Args:
duration (Optional[int]):
mime_type (Optional[str]):
file_size (Optional[int]):
"""
def __init__(self, file_id, **kwargs):
def __init__(self, file_id, duration, mime_type='', file_size=0, **kwargs):
# Required
self.file_id = str(file_id)
self.duration = int(duration)
# Optionals
self.duration = int(kwargs.get('duration', 0))
self.mime_type = str(kwargs.get('mime_type', ''))
self.file_size = int(kwargs.get('file_size', 0))
self.mime_type = str(mime_type)
self.file_size = int(file_size)
@staticmethod
def de_json(data, bot):

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""
This module contains an object that represents Tests for MessageHandler.Filters
This module contains an object that represents Tests for Filters for use with MessageHandler.
"""
import sys
@ -28,7 +28,7 @@ import functools
sys.path.append('.')
from telegram import Message, User, Chat, MessageEntity
from telegram.ext import Filters
from telegram.ext import Filters, BaseFilter
from tests.base import BaseTest
@ -37,6 +37,7 @@ class FiltersTest(BaseTest, unittest.TestCase):
def setUp(self):
self.message = Message(0, User(0, "Testuser"), datetime.now(), Chat(0, 'private'))
self.e = functools.partial(MessageEntity, offset=0, length=0)
def test_filters_text(self):
self.message.text = 'test'
@ -158,20 +159,76 @@ class FiltersTest(BaseTest, unittest.TestCase):
self.message.pinned_message = None
def test_entities_filter(self):
e = functools.partial(MessageEntity, offset=0, length=0)
self.message.entities = [e(MessageEntity.MENTION)]
self.message.entities = [self.e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = []
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [e(MessageEntity.BOLD)]
self.message.entities = [self.e(MessageEntity.BOLD)]
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [e(MessageEntity.BOLD), e(MessageEntity.MENTION)]
self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
def test_and_filters(self):
self.message.text = 'test'
self.message.forward_date = True
self.assertTrue((Filters.text & Filters.forwarded)(self.message))
self.message.text = '/test'
self.assertFalse((Filters.text & Filters.forwarded)(self.message))
self.message.text = 'test'
self.message.forward_date = None
self.assertFalse((Filters.text & Filters.forwarded)(self.message))
self.message.text = 'test'
self.message.forward_date = True
self.message.entities = [self.e(MessageEntity.MENTION)]
self.assertTrue((Filters.text & Filters.forwarded & Filters.entity(MessageEntity.MENTION))(
self.message))
self.message.entities = [self.e(MessageEntity.BOLD)]
self.assertFalse((Filters.text & Filters.forwarded & Filters.entity(MessageEntity.MENTION)
)(self.message))
def test_or_filters(self):
self.message.text = 'test'
self.assertTrue((Filters.text | Filters.status_update)(self.message))
self.message.group_chat_created = True
self.assertTrue((Filters.text | Filters.status_update)(self.message))
self.message.text = None
self.assertTrue((Filters.text | Filters.status_update)(self.message))
self.message.group_chat_created = False
self.assertFalse((Filters.text | Filters.status_update)(self.message))
def test_and_or_filters(self):
self.message.text = 'test'
self.message.forward_date = True
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
)(self.message))
self.message.forward_date = False
self.assertFalse((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)
))(self.message))
self.message.entities = [self.e(MessageEntity.MENTION)]
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
)(self.message))
self.assertRegexpMatches(
str((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)))),
r"<telegram.ext.filters.MergedFilter consisting of <telegram.ext.filters.(Filters.)?_"
r"Text object at .*?> and <telegram.ext.filters.MergedFilter consisting of "
r"<telegram.ext.filters.(Filters.)?_Forwarded object at .*?> or "
r"<telegram.ext.filters.(Filters.)?entity object at .*?>>>")
def test_faulty_custom_filter(self):
class _CustomFilter(BaseFilter):
pass
custom = _CustomFilter()
with self.assertRaises(NotImplementedError):
(custom & Filters.text)(self.message)
if __name__ == '__main__':
unittest.main()

View file

@ -77,6 +77,15 @@ class MessageTest(BaseTest, unittest.TestCase):
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text, 'Testing class method')
@flaky(3, 1)
def test_edit_text(self):
"""Test for Message.edit_text"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.edit_text('Testing class method')
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text, 'Testing class method')
if __name__ == '__main__':
unittest.main()

View file

@ -182,7 +182,7 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self._setup_updater('Test', edited=True)
d = self.updater.dispatcher
from telegram.ext import Filters
handler = MessageHandler([Filters.text], self.telegramHandlerEditedTest, allow_edited=True)
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
d.addHandler(handler)
self.updater.start_polling(0.01)
sleep(.1)
@ -190,8 +190,7 @@ class UpdaterTest(BaseTest, unittest.TestCase):
# Remove handler
d.removeHandler(handler)
handler = MessageHandler(
[Filters.text], self.telegramHandlerEditedTest, allow_edited=False)
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=False)
d.addHandler(handler)
self.reset()
@ -201,7 +200,7 @@ class UpdaterTest(BaseTest, unittest.TestCase):
def test_addTelegramMessageHandlerMultipleMessages(self):
self._setup_updater('Multiple', 100)
self.updater.dispatcher.add_handler(MessageHandler([], self.telegramHandlerTest))
self.updater.dispatcher.add_handler(MessageHandler(Filters.all, self.telegramHandlerTest))
self.updater.start_polling(0.0)
sleep(2)
self.assertEqual(self.received_message, 'Multiple')