Skip to content

Commit acf1541

Browse files
tsnoamjh0ker
authored andcommitted
* New fields channel_post and edited_channel_post for Update refs python-telegram-bot#468 * setGameScore() changes - Changed behaviour: messages with high scores will be update with new high scores by default. (documentation fix) - Use (new) disable_edit_message in setGameScore to disable the above new behaviour. - The edit_message parameter from setGameScore is no longer in use. For backward compatibility, it will be taken into account for a while, unless disable_edit_message is passed explicitly. refs python-telegram-bot#468 * New field forward_from_message_id for Message. refs python-telegram-bot#468 * New parameter cache_time for answerCallbackQuery refs python-telegram-bot#468 * replykeyboardhide renamed to replykeyboardremove refs python-telegram-bot#468 * Unitests for updated setGameScore semantics refs python-telegram-bot#468 * Backward compatibility for ReplyKeyboardHide refs python-telegram-bot#468 * Fix docstrings of wrapper methods in Message * Unitest new field forward_from_message_id of Message refs python-telegram-bot#468 * Fix testMaxCaptionLength Telegram servers changed their behaviour - now they truncate a long caption instead of returning an error. * MessageHandler: Added support for channel posts * Fix flake8 complaints in a manner which yapf will like it too. * fix rst markup
1 parent 906a1b8 commit acf1541

15 files changed

+314
-95
lines changed

docs/source/telegram.replykeyboardhide.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.replykeyboardremove module
2+
===================================
3+
4+
.. automodule:: telegram.replykeyboardremove
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/telegram.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Submodules
6262
telegram.messageentity
6363
telegram.parsemode
6464
telegram.photosize
65-
telegram.replykeyboardhide
65+
telegram.replykeyboardremove
6666
telegram.replykeyboardmarkup
6767
telegram.replymarkup
6868
telegram.sticker

examples/conversationbot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
bot.
1818
"""
1919

20-
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardHide)
20+
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
2121
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
2222
ConversationHandler)
2323

@@ -49,7 +49,7 @@ def gender(bot, update):
4949
logger.info("Gender of %s: %s" % (user.first_name, update.message.text))
5050
update.message.reply_text('I see! Please send me a photo of yourself, '
5151
'so I know what you look like, or send /skip if you don\'t want to.',
52-
reply_markup=ReplyKeyboardHide())
52+
reply_markup=ReplyKeyboardRemove())
5353

5454
return PHOTO
5555

@@ -106,7 +106,7 @@ def cancel(bot, update):
106106
user = update.message.from_user
107107
logger.info("User %s canceled the conversation." % user.first_name)
108108
update.message.reply_text('Bye! I hope we can talk again some day.',
109-
reply_markup=ReplyKeyboardHide())
109+
reply_markup=ReplyKeyboardRemove())
110110

111111
return ConversationHandler.END
112112

telegram/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from .keyboardbutton import KeyboardButton
3939
from .replymarkup import ReplyMarkup
4040
from .replykeyboardmarkup import ReplyKeyboardMarkup
41-
from .replykeyboardhide import ReplyKeyboardHide
41+
from .replykeyboardremove import ReplyKeyboardRemove, ReplyKeyboardHide
4242
from .forcereply import ForceReply
4343
from .error import TelegramError
4444
from .inputfile import InputFile
@@ -106,9 +106,9 @@
106106
'InlineQueryResultVoice', 'InlineQueryResultGame', 'InputContactMessageContent', 'InputFile',
107107
'InputLocationMessageContent', 'InputMessageContent', 'InputTextMessageContent',
108108
'InputVenueMessageContent', 'KeyboardButton', 'Location', 'Message', 'MessageEntity',
109-
'ParseMode', 'PhotoSize', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'ReplyMarkup', 'Sticker',
110-
'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue', 'Video',
111-
'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
109+
'ParseMode', 'PhotoSize', 'ReplyKeyboardRemove', 'ReplyKeyboardMarkup', 'ReplyMarkup',
110+
'Sticker', 'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue',
111+
'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
112112
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
113113
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo', 'Animation',
114114
'Game', 'GameHighScore'

telegram/__main__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ def print_ver_info():
1414
print('future {0}'.format(future.__version__))
1515
print('Python {0}'.format(sys.version.replace('\n', ' ')))
1616

17-
# main
18-
print_ver_info()
17+
18+
def main():
19+
print_ver_info()
20+
21+
22+
if __name__ == '__main__':
23+
main()

telegram/bot.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import functools
2323
import logging
24+
import warnings
2425

2526
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
2627
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore)
@@ -208,7 +209,7 @@ def sendMessage(self,
208209
ID of the original message.
209210
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
210211
interface options. A JSON-serialized object for an inline
211-
keyboard, custom reply keyboard, instructions to hide reply
212+
keyboard, custom reply keyboard, instructions to remove reply
212213
keyboard or to force a reply from the user.
213214
timeout (Optional[float]): If this value is specified, use it as
214215
the definitive timeout (in seconds) for urlopen() operations.
@@ -299,7 +300,7 @@ def sendPhoto(self,
299300
message.
300301
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
301302
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
302-
to hide reply keyboard or to force a reply from the user.
303+
to remove reply keyboard or to force a reply from the user.
303304
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
304305
(in seconds) for urlopen() operations.
305306
**kwargs (dict): Arbitrary keyword arguments.
@@ -360,7 +361,7 @@ def sendAudio(self,
360361
message.
361362
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
362363
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
363-
to hide reply keyboard or to force a reply from the user.
364+
to remove reply keyboard or to force a reply from the user.
364365
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
365366
(in seconds) for urlopen() operations.
366367
**kwargs (dict): Arbitrary keyword arguments.
@@ -415,7 +416,7 @@ def sendDocument(self,
415416
message.
416417
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
417418
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
418-
to hide reply keyboard or to force a reply from the user.
419+
to remove reply keyboard or to force a reply from the user.
419420
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
420421
(in seconds) for urlopen() operations.
421422
**kwargs (dict): Arbitrary keyword arguments.
@@ -461,7 +462,7 @@ def sendSticker(self,
461462
message.
462463
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
463464
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
464-
to hide reply keyboard or to force a reply from the user.
465+
to remove reply keyboard or to force a reply from the user.
465466
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
466467
(in seconds) for urlopen() operations.
467468
**kwargs (dict): Arbitrary keyword arguments.
@@ -508,7 +509,7 @@ def sendVideo(self,
508509
message.
509510
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
510511
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
511-
to hide reply keyboard or to force a reply from the user.
512+
to remove reply keyboard or to force a reply from the user.
512513
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
513514
(in seconds) for urlopen() operations.
514515
@@ -561,7 +562,7 @@ def sendVoice(self,
561562
message.
562563
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
563564
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
564-
to hide reply keyboard or to force a reply from the user.
565+
to remove reply keyboard or to force a reply from the user.
565566
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
566567
(in seconds) for urlopen() operations.
567568
**kwargs (dict): Arbitrary keyword arguments.
@@ -607,7 +608,7 @@ def sendLocation(self,
607608
message.
608609
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
609610
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
610-
to hide reply keyboard or to force a reply from the user.
611+
to remove reply keyboard or to force a reply from the user.
611612
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
612613
(in seconds) for urlopen() operations.
613614
**kwargs (dict): Arbitrary keyword arguments.
@@ -656,7 +657,7 @@ def sendVenue(self,
656657
message.
657658
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
658659
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
659-
to hide reply keyboard or to force a reply from the user.
660+
to remove reply keyboard or to force a reply from the user.
660661
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
661662
(in seconds) for urlopen() operations.
662663
**kwargs (dict): Arbitrary keyword arguments.
@@ -710,7 +711,7 @@ def sendContact(self,
710711
message.
711712
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
712713
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
713-
to hide reply keyboard or to force a reply from the user.
714+
to remove reply keyboard or to force a reply from the user.
714715
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
715716
(in seconds) for urlopen() operations.
716717
**kwargs (dict): Arbitrary keyword arguments.
@@ -749,7 +750,7 @@ def sendGame(self, chat_id, game_short_name, **kwargs):
749750
ID of the original message.
750751
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options.
751752
A JSON-serialized object for an inline keyboard, custom reply keyboard,
752-
instructions to hide reply keyboard or to force a reply from the user.
753+
instructions to remove reply keyboard or to force a reply from the user.
753754
timeout (Optional[float]): If this value is specified, use it as
754755
the definitive timeout (in seconds) for urlopen() operations.
755756
@@ -987,6 +988,7 @@ def answerCallbackQuery(self,
987988
text=None,
988989
show_alert=False,
989990
url=None,
991+
cache_time=None,
990992
timeout=None,
991993
**kwargs):
992994
"""Use this method to send answers to callback queries sent from inline keyboards. The
@@ -999,9 +1001,12 @@ def answerCallbackQuery(self,
9991001
to the user.
10001002
show_alert (Optional[bool]): If `True`, an alert will be shown by the client instead of
10011003
a notification at the top of the chat screen. Defaults to `False`.
1004+
url (Optional[str]): URL that will be opened by the user's client.
1005+
cache_time (Optional[int]): The maximum amount of time in seconds that the result of
1006+
the callback query may be cached client-side. Telegram apps will support caching
1007+
starting in version 3.14. Defaults to 0.
10021008
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
10031009
(in seconds) for urlopen() operations.
1004-
url (Optional[str]): URL that will be opened by the user's client.
10051010
**kwargs (dict): Arbitrary keyword arguments.
10061011
10071012
Returns:
@@ -1021,6 +1026,8 @@ def answerCallbackQuery(self,
10211026
data['show_alert'] = show_alert
10221027
if url:
10231028
data['url'] = url
1029+
if cache_time is not None:
1030+
data['cache_time'] = cache_time
10241031

10251032
result = self._request.post(url_, data, timeout=timeout)
10261033

@@ -1054,7 +1061,7 @@ def editMessageText(self,
10541061
reply_markup: A JSON-serialized object for an inline keyboard.
10551062
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
10561063
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
1057-
to hide reply keyboard or to force a reply from the user.
1064+
to remove reply keyboard or to force a reply from the user.
10581065
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
10591066
(in seconds) for urlopen() operations.
10601067
**kwargs (dict): Arbitrary keyword arguments.
@@ -1434,9 +1441,32 @@ def setGameScore(self,
14341441
message_id=None,
14351442
inline_message_id=None,
14361443
edit_message=None,
1444+
force=None,
1445+
disable_edit_message=None,
14371446
**kwargs):
14381447
"""Use this method to set the score of the specified user in a game.
14391448
1449+
Args:
1450+
user_id (int): User identifier.
1451+
score (int): New score, must be non-negative.
1452+
chat_id (Optional[int|str]): Required if `inline_message_id` is not specified. Unique
1453+
identifier for the target chat (or username of the target channel in the format
1454+
`@channelusername`)
1455+
message_id (Optional[int]): Required if inline_message_id is not specified. Identifier
1456+
of the sent message.
1457+
inline_message_id (Optional[str]): Required if chat_id and message_id are not
1458+
specified. Identifier of the inline message.
1459+
force (Optional[bool]): Pass True, if the high score is allowed to decrease. This can
1460+
be useful when fixing mistakes or banning cheaters.
1461+
disable_edit_message (Optional[bool]): Pass True, if the game message should not be
1462+
automatically edited to include the current scoreboard.
1463+
edit_message (Optional[bool]): Deprecated. Has the opposite logic for
1464+
`disable_edit_message`.
1465+
1466+
Keyword Args:
1467+
timeout (Optional[float]): If this value is specified, use it as the definitive timeout
1468+
(in seconds) for urlopen() operations.
1469+
14401470
Returns:
14411471
:class:`telegram.Message` or True: The edited message, or if the
14421472
message wasn't sent by the bot, True.
@@ -1452,8 +1482,16 @@ def setGameScore(self,
14521482
data['message_id'] = message_id
14531483
if inline_message_id:
14541484
data['inline_message_id'] = inline_message_id
1455-
if edit_message:
1456-
data['edit_message'] = edit_message
1485+
if force is not None:
1486+
data['force'] = force
1487+
if disable_edit_message is not None:
1488+
data['disable_edit_message'] = disable_edit_message
1489+
if edit_message is not None:
1490+
warnings.warn('edit_message is deprecated, use disable_edit_message instead')
1491+
if disable_edit_message is None:
1492+
data['edit_message'] = edit_message
1493+
else:
1494+
warnings.warn('edit_message is ignored when disable_edit_message is used')
14571495

14581496
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
14591497
if result is True:

telegram/ext/messagehandler.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class MessageHandler(Handler):
3232
3333
Args:
3434
filters (telegram.ext.BaseFilter): A filter inheriting from
35-
:class:`telegram.filters.BaseFilter`. Standard filters can be found in
36-
:class:`telegram.filters.Filters`. Filters can be combined using bitwise
35+
:class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
36+
:class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise
3737
operators (& for and, | for or).
3838
callback (function): A function that takes ``bot, update`` as
3939
positional arguments. It will be called when the ``check_update``
@@ -51,6 +51,11 @@ class MessageHandler(Handler):
5151
``chat_data`` will be passed to the callback function. It will be a ``dict`` you
5252
can use to keep any data related to the chat that the update was sent in.
5353
For each update in the same chat, it will be the same ``dict``. Default is ``False``.
54+
message_updates (Optional[bool]): Should "normal" message updates be handled? Default is
55+
``True``.
56+
channel_posts_updates (Optional[bool]): Should channel posts updates be handled? Default is
57+
``True``.
58+
5459
"""
5560

5661
def __init__(self,
@@ -60,7 +65,12 @@ def __init__(self,
6065
pass_update_queue=False,
6166
pass_job_queue=False,
6267
pass_user_data=False,
63-
pass_chat_data=False):
68+
pass_chat_data=False,
69+
message_updates=True,
70+
channel_posts_updates=True):
71+
if not message_updates and not channel_posts_updates:
72+
raise ValueError('Both message_updates & channel_post_updates are False')
73+
6474
super(MessageHandler, self).__init__(
6575
callback,
6676
pass_update_queue=pass_update_queue,
@@ -69,6 +79,8 @@ def __init__(self,
6979
pass_chat_data=pass_chat_data)
7080
self.filters = filters
7181
self.allow_edited = allow_edited
82+
self.message_updates = message_updates
83+
self.channel_posts_updates = channel_posts_updates
7284

7385
# We put this up here instead of with the rest of checking code
7486
# in check_update since we don't wanna spam a ton
@@ -77,15 +89,24 @@ def __init__(self,
7789
'deprecated, please use bitwise operators (& and |) '
7890
'instead. More info: https://git.io/vPTbc.')
7991

92+
def _is_allowed_message(self, update):
93+
return (self.message_updates
94+
and (update.message or (update.edited_message and self.allow_edited)))
95+
96+
def _is_allowed_channel_post(self, update):
97+
return (self.channel_posts_updates
98+
and (update.channel_post or (update.edited_channel_post and self.allow_edited)))
99+
80100
def check_update(self, update):
81101
if (isinstance(update, Update)
82-
and (update.message or update.edited_message and self.allow_edited)):
102+
and (self._is_allowed_message(update) or self._is_allowed_channel_post(update))):
83103

84104
if not self.filters:
85105
res = True
86106

87107
else:
88-
message = update.message or update.edited_message
108+
message = (update.message or update.edited_message or update.channel_post
109+
or update.edited_channel_post)
89110
if isinstance(self.filters, list):
90111
res = any(func(message) for func in self.filters)
91112
else:

0 commit comments

Comments
 (0)