From ab2d6eb494e7313ae13f671741c36aeac8c1d2b4 Mon Sep 17 00:00:00 2001 From: Li-aung 'Lewis' Yip Date: Mon, 22 Aug 2016 05:49:37 +0800 Subject: [PATCH 01/19] Fix "key not found" exception if the very first message handler in a ConversationHandler returns the state ConversationHandler.END. --- telegram/ext/conversationhandler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index 36e978dce..8a950d402 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -213,7 +213,8 @@ class ConversationHandler(Handler): def update_state(self, new_state, key): if new_state == self.END: - del self.conversations[key] + if key in self.conversations: + del self.conversations[key] elif isinstance(new_state, Promise): self.conversations[key] = (self.conversations[key], new_state) From 1c36ff46ada1d0169dfea3f50781a93710544951 Mon Sep 17 00:00:00 2001 From: Li-aung 'Lewis' Yip Date: Wed, 24 Aug 2016 09:37:23 +0800 Subject: [PATCH 02/19] Add myself to AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 9da40ea78..30a887e38 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -18,6 +18,7 @@ The following wonderful people contributed directly or indirectly to this projec - `jh0ker `_ - `JRoot3D `_ - `jlmadurga `_ +- `Li-aung Yip `_ - `macrojames `_ - `naveenvhegde `_ - `njittam `_ From 4e60008086651fe0fb1e896c1cee9794a975472c Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Tue, 13 Sep 2016 20:09:46 +0200 Subject: [PATCH 03/19] Add entities filter Should ideally superseed #375. --- telegram/ext/messagehandler.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/telegram/ext/messagehandler.py b/telegram/ext/messagehandler.py index 03208fa10..c121d56eb 100644 --- a/telegram/ext/messagehandler.py +++ b/telegram/ext/messagehandler.py @@ -85,6 +85,33 @@ class Filters(object): def forwarded(message): return bool(message.forward_date) + @staticmethod + def entities(entity_types): + """Filters messages to only allow those which have a :class:`telegram.MessageEntity` + that match `entity_types`. + + Note: + This filter functions as an OR filter, meaning that just one of the entity_type. + If you want AND filtering instead, you have to specify multiple of this filter in the + `filters` attribute. Example: + + >>> MessageHandler([Filters.entities(TEXT_MENTION, MENTION), + ... Filters.entities(HASHTAG)], callback) + + Will require either a one type of mention AND a hashtag in the message. + + Args: + entity_types: List of entity types 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_types in entity.type for entity in message.entities]) + + return entities_filter + class MessageHandler(Handler): """ From f7b497c1b4e8f91ed881679fcc704350a5249fb3 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Tue, 13 Sep 2016 20:45:42 +0200 Subject: [PATCH 04/19] Fix in keyword ordering We're testing for a string in list, not the other way around :P --- telegram/ext/messagehandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/messagehandler.py b/telegram/ext/messagehandler.py index c121d56eb..b6837e2cc 100644 --- a/telegram/ext/messagehandler.py +++ b/telegram/ext/messagehandler.py @@ -108,7 +108,7 @@ class Filters(object): """ def entities_filter(message): - return any([entity_types in entity.type for entity in message.entities]) + return any([entity.type in entity_types for entity in message.entities]) return entities_filter From 7ab007d8d48f995cb1cf4a1372083ec81381b9b3 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Tue, 13 Sep 2016 20:47:43 +0200 Subject: [PATCH 05/19] Add Filters.entities test. --- tests/test_filters.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test_filters.py b/tests/test_filters.py index b023700af..9dadac440 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -23,10 +23,11 @@ This module contains a object that represents Tests for MessageHandler.Filters import sys import unittest from datetime import datetime +import functools sys.path.append('.') -from telegram import Message, User, Chat +from telegram import Message, User, Chat, MessageEntity from telegram.ext import Filters from tests.base import BaseTest @@ -150,6 +151,27 @@ class FiltersTest(BaseTest, unittest.TestCase): self.assertTrue(Filters.status_update(self.message)) 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.assertTrue(Filters.entities([MessageEntity.MENTION])(self.message)) + + self.message.entities = [] + self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message)) + + self.message.entities = [e(MessageEntity.BOLD)] + self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message)) + + self.message.entities = [e(MessageEntity.MENTION)] + self.assertTrue( + Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message)) + self.message.entities = [e(MessageEntity.BOLD)] + self.assertTrue( + Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message)) + self.assertFalse( + Filters.entities([MessageEntity.MENTION, MessageEntity.TEXT_MENTION])(self.message)) + if __name__ == '__main__': unittest.main() From 97bb04cd385271ff2b357475a78520f6e53933db Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Tue, 13 Sep 2016 20:50:25 +0200 Subject: [PATCH 06/19] Faulty example was faulty. --- telegram/ext/messagehandler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/ext/messagehandler.py b/telegram/ext/messagehandler.py index b6837e2cc..6289d86f8 100644 --- a/telegram/ext/messagehandler.py +++ b/telegram/ext/messagehandler.py @@ -95,8 +95,8 @@ class Filters(object): If you want AND filtering instead, you have to specify multiple of this filter in the `filters` attribute. Example: - >>> MessageHandler([Filters.entities(TEXT_MENTION, MENTION), - ... Filters.entities(HASHTAG)], callback) + >>> MessageHandler([Filters.entities([TEXT_MENTION, MENTION]), + ... Filters.entities([HASHTAG])], callback) Will require either a one type of mention AND a hashtag in the message. From 1efd330e59d6e178b603b48fc295cbb6def9eb3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 20 Sep 2016 05:00:39 +0200 Subject: [PATCH 07/19] ConversationHandler: Fix #373 --- telegram/ext/conversationhandler.py | 4 ++- tests/test_conversationhandler.py | 47 ++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index 8a950d402..6afa90575 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -215,9 +215,11 @@ class ConversationHandler(Handler): if new_state == self.END: if key in self.conversations: del self.conversations[key] + else: + pass elif isinstance(new_state, Promise): - self.conversations[key] = (self.conversations[key], new_state) + self.conversations[key] = (self.conversations.get(key), new_state) elif new_state is not None: self.conversations[key] = new_state diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index 6f6cd4ba3..f9ee4623b 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -36,7 +36,7 @@ except ImportError: sys.path.append('.') from telegram import Update, Message, TelegramError, User, Chat, Bot -from telegram.ext import * +from telegram.ext import Updater, ConversationHandler, CommandHandler from tests.base import BaseTest from tests.test_updater import MockBot @@ -109,6 +109,9 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase): def start(self, bot, update): return self._set_state(update, self.THIRSTY) + def start_end(self, bot, update): + return self._set_state(update, self.END) + def brew(self, bot, update): return self._set_state(update, self.BREWING) @@ -161,6 +164,48 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase): sleep(.1) self.assertRaises(KeyError, self._get_state, user_id=second_user.id) + def test_endOnFirstMessage(self): + self._setup_updater('', messages=0) + d = self.updater.dispatcher + user = User(first_name="Misses Test", id=123) + + handler = ConversationHandler( + entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) + d.add_handler(handler) + queue = self.updater.start_polling(0.01) + + # User starts the state machine and immediately ends it. + message = Message(0, user, None, None, text="/start") + queue.put(Update(update_id=0, message=message)) + sleep(.1) + self.assertEquals(len(handler.conversations), 0) + + def test_endOnFirstMessageAsync(self): + self._setup_updater('', messages=0) + d = self.updater.dispatcher + user = User(first_name="Misses Test", id=123) + + start_end_async = (lambda bot, update: d.run_async(self.start_end, bot, update)) + + handler = ConversationHandler( + entry_points=[CommandHandler('start', start_end_async)], states={}, fallbacks=[]) + d.add_handler(handler) + queue = self.updater.start_polling(0.01) + + # User starts the state machine with an async function that immediately ends the + # conversation. Async results are resolved when the users state is queried next time. + message = Message(0, user, None, None, text="/start") + queue.put(Update(update_id=0, message=message)) + sleep(.1) + # Assert that the Promise has been accepted as the new state + self.assertEquals(len(handler.conversations), 1) + + message = Message(0, user, None, None, text="resolve promise pls") + queue.put(Update(update_id=0, message=message)) + sleep(.1) + # Assert that the Promise has been resolved and the conversation ended. + self.assertEquals(len(handler.conversations), 0) + if __name__ == '__main__': unittest.main() From 5116a77221ea44d20d1fd9b0d29c711237454a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 20 Sep 2016 06:36:55 +0200 Subject: [PATCH 08/19] Class methods (#362) * bot.py: add create_references method * create bot reference in webhook handler, use create_references on new updates * message.py: implement reply_text * echobot2.py: use Message.reply_text * fix create_references in webhook handler * add some more instance methods * Chat.kick_member and unban_member * bot.py: Create bot references in outgoing messages * add tests for everything testable * test_updater.py: add create_references method to MockBot * remove Bot.create_references and refactor TelegramObject.de_json to take the additional parameter bot * List bot as named kwarg where used * file.py: Use Bot.request property instead of Bot._request attr --- examples/echobot2.py | 6 +- telegram/audio.py | 5 +- telegram/base.py | 10 +- telegram/bot.py | 20 ++-- telegram/callbackquery.py | 25 ++++- telegram/chat.py | 38 ++++++- telegram/chatmember.py | 5 +- telegram/choseninlineresult.py | 7 +- telegram/contact.py | 5 +- telegram/document.py | 7 +- telegram/ext/updater.py | 3 +- telegram/file.py | 17 +-- telegram/forcereply.py | 5 +- telegram/inlinekeyboardbutton.py | 16 ++- telegram/inlinekeyboardmarkup.py | 14 ++- telegram/inlinequery.py | 20 +++- telegram/inlinequeryresult.py | 6 +- telegram/inlinequeryresultarticle.py | 8 +- telegram/inlinequeryresultaudio.py | 8 +- telegram/inlinequeryresultcachedaudio.py | 8 +- telegram/inlinequeryresultcacheddocument.py | 8 +- telegram/inlinequeryresultcachedgif.py | 8 +- telegram/inlinequeryresultcachedmpeg4gif.py | 8 +- telegram/inlinequeryresultcachedphoto.py | 8 +- telegram/inlinequeryresultcachedsticker.py | 9 +- telegram/inlinequeryresultcachedvideo.py | 8 +- telegram/inlinequeryresultcachedvoice.py | 8 +- telegram/inlinequeryresultcontact.py | 8 +- telegram/inlinequeryresultdocument.py | 8 +- telegram/inlinequeryresultgif.py | 8 +- telegram/inlinequeryresultlocation.py | 8 +- telegram/inlinequeryresultmpeg4gif.py | 8 +- telegram/inlinequeryresultphoto.py | 8 +- telegram/inlinequeryresultvenue.py | 8 +- telegram/inlinequeryresultvideo.py | 8 +- telegram/inlinequeryresultvoice.py | 8 +- telegram/inputcontactmessagecontent.py | 2 +- telegram/inputlocationmessagecontent.py | 2 +- telegram/inputmessagecontent.py | 12 +- telegram/inputtextmessagecontent.py | 2 +- telegram/inputvenuemessagecontent.py | 2 +- telegram/keyboardbutton.py | 6 +- telegram/location.py | 5 +- telegram/message.py | 103 ++++++++++++++---- telegram/messageentity.py | 10 +- telegram/photosize.py | 10 +- telegram/replykeyboardhide.py | 5 +- telegram/replykeyboardmarkup.py | 7 +- telegram/replymarkup.py | 4 +- telegram/sticker.py | 7 +- telegram/update.py | 14 ++- telegram/user.py | 18 ++- telegram/userprofilephotos.py | 7 +- telegram/utils/webhookhandler.py | 6 +- telegram/venue.py | 6 +- telegram/video.py | 7 +- telegram/voice.py | 5 +- tests/test_audio.py | 15 ++- tests/test_chat.py | 22 +++- tests/test_choseninlineresult.py | 6 +- tests/test_contact.py | 18 ++- tests/test_document.py | 15 ++- tests/test_file.py | 6 +- tests/test_forcereply.py | 10 +- tests/test_inlinekeyboardbutton.py | 11 +- tests/test_inlinekeyboardmarkup.py | 8 +- tests/test_inlinequery.py | 6 +- tests/test_inlinequeryresultarticle.py | 6 +- tests/test_inlinequeryresultaudio.py | 6 +- tests/test_inlinequeryresultcachedaudio.py | 6 +- tests/test_inlinequeryresultcacheddocument.py | 7 +- tests/test_inlinequeryresultcachedgif.py | 6 +- tests/test_inlinequeryresultcachedmpeg4gif.py | 7 +- tests/test_inlinequeryresultcachedphoto.py | 6 +- tests/test_inlinequeryresultcachedsticker.py | 7 +- tests/test_inlinequeryresultcachedvideo.py | 6 +- tests/test_inlinequeryresultcachedvoice.py | 6 +- tests/test_inlinequeryresultcontact.py | 6 +- tests/test_inlinequeryresultdocument.py | 6 +- tests/test_inlinequeryresultgif.py | 6 +- tests/test_inlinequeryresultlocation.py | 6 +- tests/test_inlinequeryresultmpeg4gif.py | 6 +- tests/test_inlinequeryresultphoto.py | 6 +- tests/test_inlinequeryresultvenue.py | 6 +- tests/test_inlinequeryresultvideo.py | 6 +- tests/test_inlinequeryresultvoice.py | 6 +- tests/test_inputcontactmessagecontent.py | 10 +- tests/test_inputlocationmessagecontent.py | 10 +- tests/test_inputmessagecontent.py | 2 +- tests/test_inputtextmessagecontent.py | 10 +- tests/test_inputvenuemessagecontent.py | 10 +- tests/test_keyboardbutton.py | 10 +- tests/test_location.py | 18 ++- tests/test_message.py | 20 ++++ tests/test_messageentity.py | 6 +- tests/test_photo.py | 15 ++- tests/test_replykeyboardhide.py | 10 +- tests/test_replykeyboardmarkup.py | 10 +- tests/test_replymarkup.py | 2 +- tests/test_sticker.py | 15 ++- tests/test_update.py | 8 +- tests/test_updater.py | 3 + tests/test_user.py | 24 +++- tests/test_venue.py | 18 ++- tests/test_video.py | 17 ++- tests/test_voice.py | 15 ++- 106 files changed, 690 insertions(+), 379 deletions(-) diff --git a/examples/echobot2.py b/examples/echobot2.py index ea1666954..d0c81fa91 100644 --- a/examples/echobot2.py +++ b/examples/echobot2.py @@ -29,15 +29,15 @@ logger = logging.getLogger(__name__) # Define a few command handlers. These usually take the two arguments bot and # update. Error handlers also receive the raised TelegramError object in error. def start(bot, update): - bot.sendMessage(update.message.chat_id, text='Hi!') + update.message.reply_text('Hi!') def help(bot, update): - bot.sendMessage(update.message.chat_id, text='Help!') + update.message.reply_text('Help!') def echo(bot, update): - bot.sendMessage(update.message.chat_id, text=update.message.text) + update.message.reply_text(update.message.text) def error(bot, update, error): diff --git a/telegram/audio.py b/telegram/audio.py index 3a35e7112..89ce7c5ba 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -55,10 +55,11 @@ class Audio(TelegramObject): self.file_size = int(kwargs.get('file_size', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Audio: diff --git a/telegram/base.py b/telegram/base.py index 6d0fe7ff1..abe635603 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -38,13 +38,14 @@ class TelegramObject(object): return self.__dict__[item] @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: - telegram.TelegramObject: + dict: """ if not data: return None @@ -68,6 +69,9 @@ class TelegramObject(object): data = dict() for key in iter(self.__dict__): + if key == 'bot': + continue + value = self.__dict__[key] if value is not None: if hasattr(value, 'to_dict'): diff --git a/telegram/bot.py b/telegram/bot.py index a73ab1467..3125e1ccf 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -154,7 +154,7 @@ class Bot(TelegramObject): if result is True: return result - return Message.de_json(result) + return Message.de_json(result, self) return decorator @@ -176,7 +176,7 @@ class Bot(TelegramObject): result = self._request.get(url) - self.bot = User.de_json(result) + self.bot = User.de_json(result, self) return self.bot @@ -860,7 +860,7 @@ class Bot(TelegramObject): result = self._request.post(url, data, timeout=kwargs.get('timeout')) - return UserProfilePhotos.de_json(result) + return UserProfilePhotos.de_json(result, self) @log def getFile(self, file_id, **kwargs): @@ -894,7 +894,7 @@ class Bot(TelegramObject): if result.get('file_path'): result['file_path'] = '%s/%s' % (self.base_file_url, result['file_path']) - return File.de_json(result, self._request) + return File.de_json(result, self) @log def kickChatMember(self, chat_id, user_id, **kwargs): @@ -1225,7 +1225,7 @@ class Bot(TelegramObject): else: self.logger.debug('No new updates found.') - return [Update.de_json(x) for x in result] + return [Update.de_json(u, self) for u in result] @log def setWebhook(self, webhook_url=None, certificate=None, **kwargs): @@ -1325,7 +1325,7 @@ class Bot(TelegramObject): result = self._request.post(url, data, timeout=kwargs.get('timeout')) - return Chat.de_json(result) + return Chat.de_json(result, self) @log def getChatAdministrators(self, chat_id, **kwargs): @@ -1360,7 +1360,7 @@ class Bot(TelegramObject): result = self._request.post(url, data, timeout=kwargs.get('timeout')) - return [ChatMember.de_json(x) for x in result] + return [ChatMember.de_json(x, self) for x in result] @log def getChatMembersCount(self, chat_id, **kwargs): @@ -1423,11 +1423,11 @@ class Bot(TelegramObject): result = self._request.post(url, data, timeout=kwargs.get('timeout')) - return ChatMember.de_json(result) + return ChatMember.de_json(result, self) @staticmethod - def de_json(data): - data = super(Bot, Bot).de_json(data) + def de_json(data, bot): + data = super(Bot, Bot).de_json(data, bot) return Bot(**data) diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index 97ab956e2..77332c50f 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -25,7 +25,7 @@ from telegram import TelegramObject, Message, User class CallbackQuery(TelegramObject): """This object represents a Telegram CallbackQuery.""" - def __init__(self, id, from_user, data, **kwargs): + def __init__(self, id, from_user, data, bot=None, **kwargs): # Required self.id = id self.from_user = from_user @@ -34,15 +34,26 @@ class CallbackQuery(TelegramObject): self.message = kwargs.get('message') self.inline_message_id = kwargs.get('inline_message_id', '') + self.bot = bot + @staticmethod - def de_json(data): + def de_json(data, bot): + """ + Args: + data (dict): + bot (telegram.Bot): + + Returns: + telegram.CallbackQuery: + """ + if not data: return None - data['from_user'] = User.de_json(data.get('from')) - data['message'] = Message.de_json(data.get('message')) + data['from_user'] = User.de_json(data.get('from'), bot) + data['message'] = Message.de_json(data.get('message'), bot) - return CallbackQuery(**data) + return CallbackQuery(bot=bot, **data) def to_dict(self): """ @@ -54,3 +65,7 @@ class CallbackQuery(TelegramObject): # Required data['from'] = data.pop('from_user', None) return data + + def answer(self, *args, **kwargs): + """Shortcut for ``bot.answerCallbackQuery(update.callback_query.id, *args, **kwargs)``""" + return self.bot.answerCallbackQuery(self.id, *args, **kwargs) diff --git a/telegram/chat.py b/telegram/chat.py index 962d60041..94ade4994 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -40,6 +40,7 @@ class Chat(TelegramObject): Keyword Args: type (Optional[str]): + bot (Optional[Bot]): The Bot to use for instance methods """ PRIVATE = 'private' @@ -47,7 +48,7 @@ class Chat(TelegramObject): SUPERGROUP = 'supergroup' CHANNEL = 'channel' - def __init__(self, id, type, **kwargs): + def __init__(self, id, type, bot=None, **kwargs): # Required self.id = int(id) self.type = type @@ -57,11 +58,14 @@ class Chat(TelegramObject): self.first_name = kwargs.get('first_name', '') self.last_name = kwargs.get('last_name', '') + self.bot = bot + @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.Chat: @@ -69,4 +73,32 @@ class Chat(TelegramObject): if not data: return None - return Chat(**data) + return Chat(bot=bot, **data) + + def send_action(self, *args, **kwargs): + """Shortcut for ``bot.sendChatAction(update.message.chat.id, *args, **kwargs)``""" + return self.bot.sendChatAction(self.id, *args, **kwargs) + + def leave(self, *args, **kwargs): + """Shortcut for ``bot.leaveChat(update.message.chat.id, *args, **kwargs)``""" + return self.bot.leaveChat(self.id, *args, **kwargs) + + def get_administrators(self, *args, **kwargs): + """Shortcut for ``bot.getChatAdministrators(update.message.chat.id, *args, **kwargs)``""" + return self.bot.getChatAdministrators(self.id, *args, **kwargs) + + def get_members_count(self, *args, **kwargs): + """Shortcut for ``bot.getChatMembersCount(update.message.chat.id, *args, **kwargs)``""" + return self.bot.getChatMembersCount(self.id, *args, **kwargs) + + def get_member(self, *args, **kwargs): + """Shortcut for ``bot.getChatMember(update.message.chat.id, *args, **kwargs)``""" + return self.bot.getChatMember(self.id, *args, **kwargs) + + def kick_member(self, *args, **kwargs): + """Shortcut for ``bot.kickChatMember(update.message.chat.id, *args, **kwargs)``""" + return self.bot.kickChatMember(self.id, *args, **kwargs) + + def unban_member(self, *args, **kwargs): + """Shortcut for ``bot.unbanChatMember(update.message.chat.id, *args, **kwargs)``""" + return self.bot.unbanChatMember(self.id, *args, **kwargs) diff --git a/telegram/chatmember.py b/telegram/chatmember.py index 22b7d306a..dfcfce055 100644 --- a/telegram/chatmember.py +++ b/telegram/chatmember.py @@ -46,10 +46,11 @@ class ChatMember(TelegramObject): self.status = status @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.ChatMember: @@ -57,6 +58,6 @@ class ChatMember(TelegramObject): if not data: return None - data['user'] = User.de_json(data.get('user')) + data['user'] = User.de_json(data.get('user'), bot) return ChatMember(**data) diff --git a/telegram/choseninlineresult.py b/telegram/choseninlineresult.py index 7eed66519..edc9c3515 100644 --- a/telegram/choseninlineresult.py +++ b/telegram/choseninlineresult.py @@ -57,10 +57,11 @@ class ChosenInlineResult(TelegramObject): self.inline_message_id = inline_message_id @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.ChosenInlineResult: @@ -69,9 +70,9 @@ class ChosenInlineResult(TelegramObject): return None # Required - data['from_user'] = User.de_json(data.pop('from')) + data['from_user'] = User.de_json(data.pop('from'), bot) # Optionals - data['location'] = Location.de_json(data.get('location')) + data['location'] = Location.de_json(data.get('location'), bot) return ChosenInlineResult(**data) diff --git a/telegram/contact.py b/telegram/contact.py index a755e3eeb..48f7e3065 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -49,10 +49,11 @@ class Contact(TelegramObject): self.user_id = int(kwargs.get('user_id', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Contact: diff --git a/telegram/document.py b/telegram/document.py index d9ea79d13..4e16b02b5 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -52,10 +52,11 @@ class Document(TelegramObject): self.file_size = int(kwargs.get('file_size', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Document: @@ -63,6 +64,6 @@ class Document(TelegramObject): if not data: return None - data['thumb'] = PhotoSize.de_json(data.get('thumb')) + data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) return Document(**data) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 969e23a13..1bb3788f7 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -274,7 +274,8 @@ class Updater(object): url_path = '/{0}'.format(url_path) # Create and start server - self.httpd = WebhookServer((listen, port), WebhookHandler, self.update_queue, url_path) + self.httpd = WebhookServer((listen, port), WebhookHandler, self.update_queue, url_path, + self.bot) if use_ssl: self._check_ssl_cert(cert, key) diff --git a/telegram/file.py b/telegram/file.py index 5c3ca8062..b04e85cff 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -33,7 +33,7 @@ class File(TelegramObject): Args: file_id (str): - request (telegram.utils.request.Request): + bot (telegram.Bot): **kwargs: Arbitrary keyword arguments. Keyword Args: @@ -42,29 +42,30 @@ class File(TelegramObject): """ - def __init__(self, file_id, request, **kwargs): + def __init__(self, file_id, bot, **kwargs): # Required self.file_id = str(file_id) - self._request = request + # Optionals self.file_size = int(kwargs.get('file_size', 0)) self.file_path = str(kwargs.get('file_path', '')) + self.bot = bot + @staticmethod - def de_json(data, request): + def de_json(data, bot): """ Args: data (dict): - request (telegram.utils.request.Request): + bot (telegram.Bot): Returns: telegram.File: - """ if not data: return None - return File(request=request, **data) + return File(bot=bot, **data) def download(self, custom_path=None): """ @@ -79,4 +80,4 @@ class File(TelegramObject): else: filename = basename(url) - self._request.download(url, filename) + self.bot.request.download(url, filename) diff --git a/telegram/forcereply.py b/telegram/forcereply.py index 4388b1cd5..3993e2390 100644 --- a/telegram/forcereply.py +++ b/telegram/forcereply.py @@ -43,10 +43,11 @@ class ForceReply(ReplyMarkup): self.selective = bool(kwargs.get('selective', False)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.ForceReply: diff --git a/telegram/inlinekeyboardbutton.py b/telegram/inlinekeyboardbutton.py index 0ef8b2954..d2fbcc588 100644 --- a/telegram/inlinekeyboardbutton.py +++ b/telegram/inlinekeyboardbutton.py @@ -52,8 +52,16 @@ class InlineKeyboardButton(TelegramObject): self.switch_inline_query = kwargs.get('switch_inline_query') @staticmethod - def de_json(data): - data = super(InlineKeyboardButton, InlineKeyboardButton).de_json(data) + def de_json(data, bot): + """ + Args: + data (dict): + bot (telegram.Bot): + + Returns: + telegram.InlineKeyboardButton: + """ + data = super(InlineKeyboardButton, InlineKeyboardButton).de_json(data, bot) if not data: return None @@ -61,12 +69,12 @@ class InlineKeyboardButton(TelegramObject): return InlineKeyboardButton(**data) @staticmethod - def de_list(data): + def de_list(data, bot): if not data: return [] inline_keyboards = list() for inline_keyboard in data: - inline_keyboards.append(InlineKeyboardButton.de_json(inline_keyboard)) + inline_keyboards.append(InlineKeyboardButton.de_json(inline_keyboard, bot)) return inline_keyboards diff --git a/telegram/inlinekeyboardmarkup.py b/telegram/inlinekeyboardmarkup.py index 139a3e2fc..5a82d1b30 100644 --- a/telegram/inlinekeyboardmarkup.py +++ b/telegram/inlinekeyboardmarkup.py @@ -38,13 +38,21 @@ class InlineKeyboardMarkup(ReplyMarkup): self.inline_keyboard = inline_keyboard @staticmethod - def de_json(data): - data = super(InlineKeyboardMarkup, InlineKeyboardMarkup).de_json(data) + def de_json(data, bot): + """ + Args: + data (dict): + bot (telegram.Bot): + + Returns: + telegram.InlineKeyboardMarkup: + """ + data = super(InlineKeyboardMarkup, InlineKeyboardMarkup).de_json(data, bot) if not data: return None - data['inline_keyboard'] = [InlineKeyboardButton.de_list(inline_keyboard) + data['inline_keyboard'] = [InlineKeyboardButton.de_list(inline_keyboard, bot) for inline_keyboard in data['inline_keyboard']] return InlineKeyboardMarkup(**data) diff --git a/telegram/inlinequery.py b/telegram/inlinequery.py index 83426c2ef..58e4f265b 100644 --- a/telegram/inlinequery.py +++ b/telegram/inlinequery.py @@ -42,9 +42,10 @@ class InlineQuery(TelegramObject): Keyword Args: location (optional[:class:`telegram.Location`]): + bot (Optional[Bot]): The Bot to use for instance methods """ - def __init__(self, id, from_user, query, offset, **kwargs): + def __init__(self, id, from_user, query, offset, bot=None, **kwargs): # Required self.id = id self.from_user = from_user @@ -54,24 +55,27 @@ class InlineQuery(TelegramObject): # Optional self.location = kwargs.get('location') + self.bot = bot + @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.InlineQuery: """ - data = super(InlineQuery, InlineQuery).de_json(data) + data = super(InlineQuery, InlineQuery).de_json(data, bot) if not data: return None - data['from_user'] = User.de_json(data.get('from')) - data['location'] = Location.de_json(data.get('location')) + data['from_user'] = User.de_json(data.get('from'), bot) + data['location'] = Location.de_json(data.get('location'), bot) - return InlineQuery(**data) + return InlineQuery(bot=bot, **data) def to_dict(self): """ @@ -84,3 +88,7 @@ class InlineQuery(TelegramObject): data['from'] = data.pop('from_user', None) return data + + def answer(self, *args, **kwargs): + """Shortcut for ``bot.answerInlineQuery(update.inline_query.id, *args, **kwargs)``""" + return self.bot.answerInlineQuery(self.id, *args, **kwargs) diff --git a/telegram/inlinequeryresult.py b/telegram/inlinequeryresult.py index 6ecb7de16..b161d7253 100644 --- a/telegram/inlinequeryresult.py +++ b/telegram/inlinequeryresult.py @@ -35,11 +35,11 @@ class InlineQueryResult(TelegramObject): """ - def __init__(self, type, id): + def __init__(self, type, id, **kwargs): # Required self.type = str(type) self.id = str(id) @staticmethod - def de_json(data): - return super(InlineQueryResult, InlineQueryResult).de_json(data) + def de_json(data, bot): + return super(InlineQueryResult, InlineQueryResult).de_json(data, bot) diff --git a/telegram/inlinequeryresultarticle.py b/telegram/inlinequeryresultarticle.py index ecc87a11e..bbf528520 100644 --- a/telegram/inlinequeryresultarticle.py +++ b/telegram/inlinequeryresultarticle.py @@ -94,11 +94,11 @@ class InlineQueryResultArticle(InlineQueryResult): self.thumb_height = thumb_height @staticmethod - def de_json(data): - data = super(InlineQueryResultArticle, InlineQueryResultArticle).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultArticle, InlineQueryResultArticle).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultArticle(**data) diff --git a/telegram/inlinequeryresultaudio.py b/telegram/inlinequeryresultaudio.py index 32c266c7f..eabf551cb 100644 --- a/telegram/inlinequeryresultaudio.py +++ b/telegram/inlinequeryresultaudio.py @@ -84,11 +84,11 @@ class InlineQueryResultAudio(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultAudio, InlineQueryResultAudio).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultAudio, InlineQueryResultAudio).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultAudio(**data) diff --git a/telegram/inlinequeryresultcachedaudio.py b/telegram/inlinequeryresultcachedaudio.py index f2ecc957a..44779ed71 100644 --- a/telegram/inlinequeryresultcachedaudio.py +++ b/telegram/inlinequeryresultcachedaudio.py @@ -65,11 +65,11 @@ class InlineQueryResultCachedAudio(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedAudio, InlineQueryResultCachedAudio).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedAudio, InlineQueryResultCachedAudio).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedAudio(**data) diff --git a/telegram/inlinequeryresultcacheddocument.py b/telegram/inlinequeryresultcacheddocument.py index c6866170e..8439e877d 100644 --- a/telegram/inlinequeryresultcacheddocument.py +++ b/telegram/inlinequeryresultcacheddocument.py @@ -49,12 +49,12 @@ class InlineQueryResultCachedDocument(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): + def de_json(data, bot): data = super(InlineQueryResultCachedDocument, - InlineQueryResultCachedDocument).de_json(data) + InlineQueryResultCachedDocument).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedDocument(**data) diff --git a/telegram/inlinequeryresultcachedgif.py b/telegram/inlinequeryresultcachedgif.py index 3a92d6ab1..2e9820cb3 100644 --- a/telegram/inlinequeryresultcachedgif.py +++ b/telegram/inlinequeryresultcachedgif.py @@ -47,11 +47,11 @@ class InlineQueryResultCachedGif(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedGif, InlineQueryResultCachedGif).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedGif, InlineQueryResultCachedGif).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedGif(**data) diff --git a/telegram/inlinequeryresultcachedmpeg4gif.py b/telegram/inlinequeryresultcachedmpeg4gif.py index 0c19f2ff6..ea1ed6ec6 100644 --- a/telegram/inlinequeryresultcachedmpeg4gif.py +++ b/telegram/inlinequeryresultcachedmpeg4gif.py @@ -47,12 +47,12 @@ class InlineQueryResultCachedMpeg4Gif(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): + def de_json(data, bot): data = super(InlineQueryResultCachedMpeg4Gif, - InlineQueryResultCachedMpeg4Gif).de_json(data) + InlineQueryResultCachedMpeg4Gif).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedMpeg4Gif(**data) diff --git a/telegram/inlinequeryresultcachedphoto.py b/telegram/inlinequeryresultcachedphoto.py index d7a9b6428..fa7bbe5d3 100644 --- a/telegram/inlinequeryresultcachedphoto.py +++ b/telegram/inlinequeryresultcachedphoto.py @@ -50,11 +50,11 @@ class InlineQueryResultCachedPhoto(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedPhoto, InlineQueryResultCachedPhoto).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedPhoto, InlineQueryResultCachedPhoto).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedPhoto(**data) diff --git a/telegram/inlinequeryresultcachedsticker.py b/telegram/inlinequeryresultcachedsticker.py index 1a93b5fd5..f27f2e406 100644 --- a/telegram/inlinequeryresultcachedsticker.py +++ b/telegram/inlinequeryresultcachedsticker.py @@ -41,11 +41,12 @@ class InlineQueryResultCachedSticker(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedSticker, InlineQueryResultCachedSticker).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedSticker, + InlineQueryResultCachedSticker).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedSticker(**data) diff --git a/telegram/inlinequeryresultcachedvideo.py b/telegram/inlinequeryresultcachedvideo.py index ed156387c..6890176aa 100644 --- a/telegram/inlinequeryresultcachedvideo.py +++ b/telegram/inlinequeryresultcachedvideo.py @@ -49,11 +49,11 @@ class InlineQueryResultCachedVideo(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedVideo, InlineQueryResultCachedVideo).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedVideo, InlineQueryResultCachedVideo).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedVideo(**data) diff --git a/telegram/inlinequeryresultcachedvoice.py b/telegram/inlinequeryresultcachedvoice.py index a81b5ecd4..927d51045 100644 --- a/telegram/inlinequeryresultcachedvoice.py +++ b/telegram/inlinequeryresultcachedvoice.py @@ -46,11 +46,11 @@ class InlineQueryResultCachedVoice(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultCachedVoice, InlineQueryResultCachedVoice).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultCachedVoice, InlineQueryResultCachedVoice).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultCachedVoice(**data) diff --git a/telegram/inlinequeryresultcontact.py b/telegram/inlinequeryresultcontact.py index 09446a13f..2755a456a 100644 --- a/telegram/inlinequeryresultcontact.py +++ b/telegram/inlinequeryresultcontact.py @@ -55,11 +55,11 @@ class InlineQueryResultContact(InlineQueryResult): self.thumb_height = thumb_height @staticmethod - def de_json(data): - data = super(InlineQueryResultContact, InlineQueryResultContact).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultContact, InlineQueryResultContact).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultContact(**data) diff --git a/telegram/inlinequeryresultdocument.py b/telegram/inlinequeryresultdocument.py index 722f56276..81345bb8a 100644 --- a/telegram/inlinequeryresultdocument.py +++ b/telegram/inlinequeryresultdocument.py @@ -60,11 +60,11 @@ class InlineQueryResultDocument(InlineQueryResult): self.thumb_height = thumb_height @staticmethod - def de_json(data): - data = super(InlineQueryResultDocument, InlineQueryResultDocument).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultDocument, InlineQueryResultDocument).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultDocument(**data) diff --git a/telegram/inlinequeryresultgif.py b/telegram/inlinequeryresultgif.py index dc6a45c0a..b96bfbb17 100644 --- a/telegram/inlinequeryresultgif.py +++ b/telegram/inlinequeryresultgif.py @@ -56,11 +56,11 @@ class InlineQueryResultGif(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultGif, InlineQueryResultGif).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultGif, InlineQueryResultGif).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultGif(**data) diff --git a/telegram/inlinequeryresultlocation.py b/telegram/inlinequeryresultlocation.py index ec75c98b0..8d2f53f31 100644 --- a/telegram/inlinequeryresultlocation.py +++ b/telegram/inlinequeryresultlocation.py @@ -54,11 +54,11 @@ class InlineQueryResultLocation(InlineQueryResult): self.thumb_height = thumb_height @staticmethod - def de_json(data): - data = super(InlineQueryResultLocation, InlineQueryResultLocation).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultLocation, InlineQueryResultLocation).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultLocation(**data) diff --git a/telegram/inlinequeryresultmpeg4gif.py b/telegram/inlinequeryresultmpeg4gif.py index 42dc3671e..bf9a9822a 100644 --- a/telegram/inlinequeryresultmpeg4gif.py +++ b/telegram/inlinequeryresultmpeg4gif.py @@ -56,11 +56,11 @@ class InlineQueryResultMpeg4Gif(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultMpeg4Gif, InlineQueryResultMpeg4Gif).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultMpeg4Gif, InlineQueryResultMpeg4Gif).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultMpeg4Gif(**data) diff --git a/telegram/inlinequeryresultphoto.py b/telegram/inlinequeryresultphoto.py index d81592d96..c79f4389f 100644 --- a/telegram/inlinequeryresultphoto.py +++ b/telegram/inlinequeryresultphoto.py @@ -58,11 +58,11 @@ class InlineQueryResultPhoto(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultPhoto, InlineQueryResultPhoto).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultPhoto, InlineQueryResultPhoto).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultPhoto(**data) diff --git a/telegram/inlinequeryresultvenue.py b/telegram/inlinequeryresultvenue.py index 7fafc45e0..2d19469a1 100644 --- a/telegram/inlinequeryresultvenue.py +++ b/telegram/inlinequeryresultvenue.py @@ -60,11 +60,11 @@ class InlineQueryResultVenue(InlineQueryResult): self.thumb_height = thumb_height @staticmethod - def de_json(data): - data = super(InlineQueryResultVenue, InlineQueryResultVenue).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultVenue, InlineQueryResultVenue).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultVenue(**data) diff --git a/telegram/inlinequeryresultvideo.py b/telegram/inlinequeryresultvideo.py index 498854e47..e4463d438 100644 --- a/telegram/inlinequeryresultvideo.py +++ b/telegram/inlinequeryresultvideo.py @@ -63,11 +63,11 @@ class InlineQueryResultVideo(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultVideo, InlineQueryResultVideo).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultVideo, InlineQueryResultVideo).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultVideo(**data) diff --git a/telegram/inlinequeryresultvoice.py b/telegram/inlinequeryresultvoice.py index e323e67ae..04051ba98 100644 --- a/telegram/inlinequeryresultvoice.py +++ b/telegram/inlinequeryresultvoice.py @@ -47,11 +47,11 @@ class InlineQueryResultVoice(InlineQueryResult): self.input_message_content = input_message_content @staticmethod - def de_json(data): - data = super(InlineQueryResultVoice, InlineQueryResultVoice).de_json(data) + def de_json(data, bot): + data = super(InlineQueryResultVoice, InlineQueryResultVoice).de_json(data, bot) - data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup')) + data['reply_markup'] = InlineKeyboardMarkup.de_json(data.get('reply_markup'), bot) data['input_message_content'] = InputMessageContent.de_json( - data.get('input_message_content')) + data.get('input_message_content'), bot) return InlineQueryResultVoice(**data) diff --git a/telegram/inputcontactmessagecontent.py b/telegram/inputcontactmessagecontent.py index 4a0964813..d907357ac 100644 --- a/telegram/inputcontactmessagecontent.py +++ b/telegram/inputcontactmessagecontent.py @@ -33,5 +33,5 @@ class InputContactMessageContent(InputMessageContent): self.last_name = last_name @staticmethod - def de_json(data): + def de_json(data, bot): return InputContactMessageContent(**data) diff --git a/telegram/inputlocationmessagecontent.py b/telegram/inputlocationmessagecontent.py index 05a7e35f6..1c7fb4a8f 100644 --- a/telegram/inputlocationmessagecontent.py +++ b/telegram/inputlocationmessagecontent.py @@ -31,5 +31,5 @@ class InputLocationMessageContent(InputMessageContent): self.longitude = longitude @staticmethod - def de_json(data): + def de_json(data, bot): return InputLocationMessageContent(**data) diff --git a/telegram/inputmessagecontent.py b/telegram/inputmessagecontent.py index 0663ecbcd..5f6fcc164 100644 --- a/telegram/inputmessagecontent.py +++ b/telegram/inputmessagecontent.py @@ -26,33 +26,33 @@ class InputMessageContent(TelegramObject): """Base class for Telegram InputMessageContent Objects""" @staticmethod - def de_json(data): - data = super(InputMessageContent, InputMessageContent).de_json(data) + def de_json(data, bot): + data = super(InputMessageContent, InputMessageContent).de_json(data, bot) if not data: return None try: from telegram import InputTextMessageContent - return InputTextMessageContent.de_json(data) + return InputTextMessageContent.de_json(data, bot) except TypeError: pass try: from telegram import InputVenueMessageContent - return InputVenueMessageContent.de_json(data) + return InputVenueMessageContent.de_json(data, bot) except TypeError: pass try: from telegram import InputLocationMessageContent - return InputLocationMessageContent.de_json(data) + return InputLocationMessageContent.de_json(data, bot) except TypeError: pass try: from telegram import InputContactMessageContent - return InputContactMessageContent.de_json(data) + return InputContactMessageContent.de_json(data, bot) except TypeError: pass diff --git a/telegram/inputtextmessagecontent.py b/telegram/inputtextmessagecontent.py index 999d47b0a..029d95e3b 100644 --- a/telegram/inputtextmessagecontent.py +++ b/telegram/inputtextmessagecontent.py @@ -33,5 +33,5 @@ class InputTextMessageContent(InputMessageContent): self.disable_web_page_preview = disable_web_page_preview @staticmethod - def de_json(data): + def de_json(data, bot): return InputTextMessageContent(**data) diff --git a/telegram/inputvenuemessagecontent.py b/telegram/inputvenuemessagecontent.py index 23b342f9c..34f4ea3d7 100644 --- a/telegram/inputvenuemessagecontent.py +++ b/telegram/inputvenuemessagecontent.py @@ -35,5 +35,5 @@ class InputVenueMessageContent(InputMessageContent): self.foursquare_id = foursquare_id @staticmethod - def de_json(data): + def de_json(data, bot): return InputVenueMessageContent(**data) diff --git a/telegram/keyboardbutton.py b/telegram/keyboardbutton.py index c6c0731d0..1c19bbbd6 100644 --- a/telegram/keyboardbutton.py +++ b/telegram/keyboardbutton.py @@ -43,19 +43,19 @@ class KeyboardButton(TelegramObject): self.request_location = request_location @staticmethod - def de_json(data): + def de_json(data, bot): if not data: return None return KeyboardButton(**data) @staticmethod - def de_list(data): + def de_list(data, bot): if not data: return [] keyboards = list() for keyboard in data: - keyboards.append(KeyboardButton.de_json(keyboard)) + keyboards.append(KeyboardButton.de_json(keyboard, bot)) return keyboards diff --git a/telegram/location.py b/telegram/location.py index cc28dde10..b7f0df38f 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -39,10 +39,11 @@ class Location(TelegramObject): self.latitude = float(latitude) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Location: diff --git a/telegram/message.py b/telegram/message.py index 84079c2ec..479334e3e 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -103,9 +103,10 @@ class Message(TelegramObject): migrate_to_chat_id (Optional[int]): migrate_from_chat_id (Optional[int]): channel_chat_created (Optional[bool]): + bot (Optional[Bot]): The Bot to use for instance methods """ - def __init__(self, message_id, from_user, date, chat, **kwargs): + def __init__(self, message_id, from_user, date, chat, bot=None, **kwargs): # Required self.message_id = int(message_id) self.from_user = from_user @@ -141,16 +142,19 @@ class Message(TelegramObject): self.channel_chat_created = bool(kwargs.get('channel_chat_created', False)) self.pinned_message = kwargs.get('pinned_message') + self.bot = bot + @property def chat_id(self): """int: Short for :attr:`Message.chat.id`""" return self.chat.id @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.Message: @@ -158,30 +162,30 @@ class Message(TelegramObject): if not data: return None - data['from_user'] = User.de_json(data.get('from')) + data['from_user'] = User.de_json(data.get('from'), bot) data['date'] = datetime.fromtimestamp(data['date']) - data['chat'] = Chat.de_json(data.get('chat')) - data['entities'] = MessageEntity.de_list(data.get('entities')) - data['forward_from'] = User.de_json(data.get('forward_from')) - data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat')) + data['chat'] = Chat.de_json(data.get('chat'), bot) + data['entities'] = MessageEntity.de_list(data.get('entities'), bot) + data['forward_from'] = User.de_json(data.get('forward_from'), bot) + data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'), bot) data['forward_date'] = Message._fromtimestamp(data.get('forward_date')) - data['reply_to_message'] = Message.de_json(data.get('reply_to_message')) + data['reply_to_message'] = Message.de_json(data.get('reply_to_message'), bot) data['edit_date'] = Message._fromtimestamp(data.get('edit_date')) - data['audio'] = Audio.de_json(data.get('audio')) - data['document'] = Document.de_json(data.get('document')) - data['photo'] = PhotoSize.de_list(data.get('photo')) - data['sticker'] = Sticker.de_json(data.get('sticker')) - data['video'] = Video.de_json(data.get('video')) - data['voice'] = Voice.de_json(data.get('voice')) - data['contact'] = Contact.de_json(data.get('contact')) - data['location'] = Location.de_json(data.get('location')) - data['venue'] = Venue.de_json(data.get('venue')) - data['new_chat_member'] = User.de_json(data.get('new_chat_member')) - data['left_chat_member'] = User.de_json(data.get('left_chat_member')) - data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo')) - data['pinned_message'] = Message.de_json(data.get('pinned_message')) + data['audio'] = Audio.de_json(data.get('audio'), bot) + data['document'] = Document.de_json(data.get('document'), bot) + data['photo'] = PhotoSize.de_list(data.get('photo'), bot) + data['sticker'] = Sticker.de_json(data.get('sticker'), bot) + data['video'] = Video.de_json(data.get('video'), bot) + data['voice'] = Voice.de_json(data.get('voice'), bot) + data['contact'] = Contact.de_json(data.get('contact'), bot) + data['location'] = Location.de_json(data.get('location'), bot) + data['venue'] = Venue.de_json(data.get('venue'), bot) + data['new_chat_member'] = User.de_json(data.get('new_chat_member'), bot) + data['left_chat_member'] = User.de_json(data.get('left_chat_member'), bot) + data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'), bot) + data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot) - return Message(**data) + return Message(bot=bot, **data) def __getitem__(self, item): if item in self.__dict__.keys(): @@ -246,6 +250,61 @@ class Message(TelegramObject): # Python 3 (< 3.3) and Python 2 return int(mktime(dt_obj.timetuple())) + def reply_text(self, *args, **kwargs): + """Shortcut for ``bot.sendMessage(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendMessage(self.chat_id, *args, **kwargs) + + def reply_photo(self, *args, **kwargs): + """Shortcut for ``bot.sendPhoto(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendPhoto(self.chat_id, *args, **kwargs) + + def reply_audio(self, *args, **kwargs): + """Shortcut for ``bot.sendAudio(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendAudio(self.chat_id, *args, **kwargs) + + def reply_document(self, *args, **kwargs): + """Shortcut for ``bot.sendDocument(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendDocument(self.chat_id, *args, **kwargs) + + def reply_sticker(self, *args, **kwargs): + """Shortcut for ``bot.sendSticker(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendSticker(self.chat_id, *args, **kwargs) + + def reply_video(self, *args, **kwargs): + """Shortcut for ``bot.sendVideo(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendVideo(self.chat_id, *args, **kwargs) + + def reply_voice(self, *args, **kwargs): + """Shortcut for ``bot.sendVoice(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendVoice(self.chat_id, *args, **kwargs) + + def reply_location(self, *args, **kwargs): + """Shortcut for ``bot.sendLocation(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendLocation(self.chat_id, *args, **kwargs) + + def reply_venue(self, *args, **kwargs): + """Shortcut for ``bot.sendVenue(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendVenue(self.chat_id, *args, **kwargs) + + def reply_contact(self, *args, **kwargs): + """Shortcut for ``bot.sendContact(update.message.chat_id, *args, **kwargs)``""" + return self.bot.sendContact(self.chat_id, *args, **kwargs) + + def forward(self, chat_id, disable_notification=False): + """Shortcut for + + bot.forwardMessage(chat_id=chat_id, + from_chat_id=update.message.chat_id, + disable_notification=disable_notification, + message_id=update.message.message_id) + + """ + return self.bot.forwardMessage( + chat_id=chat_id, + from_chat_id=self.chat_id, + disable_notification=disable_notification, + message_id=self.message_id) + def parse_entity(self, entity): """ Returns the text from a given :class:`telegram.MessageEntity`. diff --git a/telegram/messageentity.py b/telegram/messageentity.py index 7700b774d..eb6a41248 100644 --- a/telegram/messageentity.py +++ b/telegram/messageentity.py @@ -44,15 +44,15 @@ class MessageEntity(TelegramObject): self.user = kwargs.get('user') @staticmethod - def de_json(data): - data = super(MessageEntity, MessageEntity).de_json(data) + def de_json(data, bot): + data = super(MessageEntity, MessageEntity).de_json(data, bot) - data['user'] = User.de_json(data.get('user')) + data['user'] = User.de_json(data.get('user'), bot) return MessageEntity(**data) @staticmethod - def de_list(data): + def de_list(data, bot): """ Args: data (list): @@ -65,7 +65,7 @@ class MessageEntity(TelegramObject): entities = list() for entity in data: - entities.append(MessageEntity.de_json(entity)) + entities.append(MessageEntity.de_json(entity, bot)) return entities diff --git a/telegram/photosize.py b/telegram/photosize.py index 7c57748b5..e708ab11d 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -55,10 +55,11 @@ class PhotoSize(TelegramObject): and self.height == other.height and self.file_size == other.file_size) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.PhotoSize: @@ -69,10 +70,11 @@ class PhotoSize(TelegramObject): return PhotoSize(**data) @staticmethod - def de_list(data): + def de_list(data, bot): """ Args: data (list): + bot (telegram.Bot): Returns: List: @@ -82,6 +84,6 @@ class PhotoSize(TelegramObject): photos = list() for photo in data: - photos.append(PhotoSize.de_json(photo)) + photos.append(PhotoSize.de_json(photo, bot)) return photos diff --git a/telegram/replykeyboardhide.py b/telegram/replykeyboardhide.py index 6c1f37c72..ddc5ff414 100644 --- a/telegram/replykeyboardhide.py +++ b/telegram/replykeyboardhide.py @@ -44,10 +44,11 @@ class ReplyKeyboardHide(ReplyMarkup): self.selective = bool(kwargs.get('selective', False)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot(telegram.Bot): Returns: telegram.ReplyKeyboardHide: diff --git a/telegram/replykeyboardmarkup.py b/telegram/replykeyboardmarkup.py index 9ff66cb6d..17f00ff57 100644 --- a/telegram/replykeyboardmarkup.py +++ b/telegram/replykeyboardmarkup.py @@ -50,10 +50,11 @@ class ReplyKeyboardMarkup(ReplyMarkup): self.selective = bool(kwargs.get('selective', False)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.ReplyKeyboardMarkup: @@ -61,7 +62,7 @@ class ReplyKeyboardMarkup(ReplyMarkup): if not data: return None - data['keyboard'] = [KeyboardButton.de_list(keyboard) for keyboard in data['keyboard']] + data['keyboard'] = [KeyboardButton.de_list(keyboard, bot) for keyboard in data['keyboard']] return ReplyKeyboardMarkup(**data) diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py index c0a0bc55d..a5a7bf84a 100644 --- a/telegram/replymarkup.py +++ b/telegram/replymarkup.py @@ -25,8 +25,8 @@ class ReplyMarkup(TelegramObject): """Base class for Telegram ReplyMarkup Objects""" @staticmethod - def de_json(data): - data = super(ReplyMarkup, ReplyMarkup).de_json(data) + def de_json(data, bot): + data = super(ReplyMarkup, ReplyMarkup).de_json(data, bot) if not data: return None diff --git a/telegram/sticker.py b/telegram/sticker.py index ad2b6f080..b2255fb18 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -55,10 +55,11 @@ class Sticker(TelegramObject): self.file_size = int(kwargs.get('file_size', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Sticker: @@ -66,6 +67,6 @@ class Sticker(TelegramObject): if not data: return None - data['thumb'] = PhotoSize.de_json(data.get('thumb')) + data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) return Sticker(**data) diff --git a/telegram/update.py b/telegram/update.py index c20c61d3c..08a164b99 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -55,10 +55,11 @@ class Update(TelegramObject): self.callback_query = kwargs.get('callback_query') @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: data (dict): + bot (telegram.Bot): Returns: telegram.Update: @@ -66,10 +67,11 @@ class Update(TelegramObject): if not data: return None - data['message'] = Message.de_json(data.get('message')) - data['edited_message'] = Message.de_json(data.get('edited_message')) - data['inline_query'] = InlineQuery.de_json(data.get('inline_query')) - data['chosen_inline_result'] = ChosenInlineResult.de_json(data.get('chosen_inline_result')) - data['callback_query'] = CallbackQuery.de_json(data.get('callback_query')) + data['message'] = Message.de_json(data.get('message'), bot) + data['edited_message'] = Message.de_json(data.get('edited_message'), bot) + data['inline_query'] = InlineQuery.de_json(data.get('inline_query'), bot) + data['chosen_inline_result'] = ChosenInlineResult.de_json( + data.get('chosen_inline_result'), bot) + data['callback_query'] = CallbackQuery.de_json(data.get('callback_query'), bot) return Update(**data) diff --git a/telegram/user.py b/telegram/user.py index c1b340015..676d276ee 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -41,9 +41,10 @@ class User(TelegramObject): type (Optional[str]): last_name (Optional[str]): username (Optional[str]): + bot (Optional[Bot]): The Bot to use for instance methods """ - def __init__(self, id, first_name, **kwargs): + def __init__(self, id, first_name, bot=None, **kwargs): # Required self.id = int(id) self.first_name = first_name @@ -52,6 +53,8 @@ class User(TelegramObject): self.last_name = kwargs.get('last_name', '') self.username = kwargs.get('username', '') + self.bot = bot + @property def name(self): """str: """ @@ -62,10 +65,11 @@ class User(TelegramObject): return self.first_name @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.User: @@ -73,4 +77,10 @@ class User(TelegramObject): if not data: return None - return User(**data) + return User(bot=bot, **data) + + def get_profile_photos(self, *args, **kwargs): + """ + Shortcut for ``bot.getUserProfilePhotos(update.message.from_user.id, *args, **kwargs)`` + """ + return self.bot.getUserProfilePhotos(self.id, *args, **kwargs) diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index 91ded979a..7b80b2a07 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -40,10 +40,11 @@ class UserProfilePhotos(TelegramObject): self.photos = photos @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.UserProfilePhotos: @@ -51,7 +52,7 @@ class UserProfilePhotos(TelegramObject): if not data: return None - data['photos'] = [PhotoSize.de_list(photo) for photo in data['photos']] + data['photos'] = [PhotoSize.de_list(photo, bot) for photo in data['photos']] return UserProfilePhotos(**data) diff --git a/telegram/utils/webhookhandler.py b/telegram/utils/webhookhandler.py index 14c60d3a8..ec3a94f15 100644 --- a/telegram/utils/webhookhandler.py +++ b/telegram/utils/webhookhandler.py @@ -24,11 +24,12 @@ class _InvalidPost(Exception): class WebhookServer(BaseHTTPServer.HTTPServer, object): - def __init__(self, server_address, RequestHandlerClass, update_queue, webhook_path): + def __init__(self, server_address, RequestHandlerClass, update_queue, webhook_path, bot): super(WebhookServer, self).__init__(server_address, RequestHandlerClass) self.logger = logging.getLogger(__name__) self.update_queue = update_queue self.webhook_path = webhook_path + self.bot = bot self.is_running = False self.server_lock = Lock() self.shutdown_lock = Lock() @@ -85,7 +86,8 @@ class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler, object): self.logger.debug('Webhook received data: ' + json_string) - update = Update.de_json(json.loads(json_string)) + update = Update.de_json(json.loads(json_string), self.server.bot) + self.logger.debug('Received Update with ID %d on Webhook' % update.update_id) self.server.update_queue.put(update) diff --git a/telegram/venue.py b/telegram/venue.py index d059cb8f0..833875263 100644 --- a/telegram/venue.py +++ b/telegram/venue.py @@ -41,12 +41,12 @@ class Venue(TelegramObject): self.foursquare_id = foursquare_id @staticmethod - def de_json(data): - data = super(Venue, Venue).de_json(data) + def de_json(data, bot): + data = super(Venue, Venue).de_json(data, bot) if not data: return None - data['location'] = Location.de_json(data.get('location')) + data['location'] = Location.de_json(data.get('location'), bot) return Venue(**data) diff --git a/telegram/video.py b/telegram/video.py index d486c19a4..9adceb553 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -58,10 +58,11 @@ class Video(TelegramObject): self.file_size = int(kwargs.get('file_size', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot): Returns: telegram.Video: @@ -69,6 +70,6 @@ class Video(TelegramObject): if not data: return None - data['thumb'] = PhotoSize.de_json(data.get('thumb')) + data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) return Video(**data) diff --git a/telegram/voice.py b/telegram/voice.py index 8b6c11387..50e2f9300 100644 --- a/telegram/voice.py +++ b/telegram/voice.py @@ -49,10 +49,11 @@ class Voice(TelegramObject): self.file_size = int(kwargs.get('file_size', 0)) @staticmethod - def de_json(data): + def de_json(data, bot): """ Args: - data (str): + data (dict): + bot (telegram.Bot) Returns: telegram.Voice: diff --git a/tests/test_audio.py b/tests/test_audio.py index 0f2de88c9..051db49f1 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -169,7 +169,7 @@ class AudioTest(BaseTest, unittest.TestCase): self.assertEqual(audio.mime_type, self.mime_type) def test_audio_de_json(self): - audio = telegram.Audio.de_json(self.json_dict) + audio = telegram.Audio.de_json(self.json_dict, self._bot) self.assertEqual(audio.file_id, self.audio_file_id) self.assertEqual(audio.duration, self.duration) @@ -179,12 +179,12 @@ class AudioTest(BaseTest, unittest.TestCase): self.assertEqual(audio.file_size, self.file_size) def test_audio_to_json(self): - audio = telegram.Audio.de_json(self.json_dict) + audio = telegram.Audio.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(audio.to_json())) def test_audio_to_dict(self): - audio = telegram.Audio.de_json(self.json_dict) + audio = telegram.Audio.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(audio.to_dict())) self.assertEqual(audio['file_id'], self.audio_file_id) @@ -230,6 +230,15 @@ class AudioTest(BaseTest, unittest.TestCase): TypeError, lambda: self._bot.sendAudio(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_audio(self): + """Test for Message.reply_audio""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_audio(self.audio_file) + + self.assertNotEqual(message.audio.file_id, '') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_chat.py b/tests/test_chat.py index 3b904dc04..25a85fbcf 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -20,6 +20,9 @@ import unittest import sys + +from flaky import flaky + sys.path.append('.') import telegram @@ -37,30 +40,41 @@ class ChatTest(BaseTest, unittest.TestCase): self.json_dict = {'id': self.id, 'title': self.title, 'type': self.type} def test_group_chat_de_json_empty_json(self): - group_chat = telegram.Chat.de_json({}) + group_chat = telegram.Chat.de_json({}, self._bot) self.assertEqual(group_chat, None) def test_group_chat_de_json(self): - group_chat = telegram.Chat.de_json(self.json_dict) + group_chat = telegram.Chat.de_json(self.json_dict, self._bot) self.assertEqual(group_chat.id, self.id) self.assertEqual(group_chat.title, self.title) self.assertEqual(group_chat.type, self.type) def test_group_chat_to_json(self): - group_chat = telegram.Chat.de_json(self.json_dict) + group_chat = telegram.Chat.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(group_chat.to_json())) def test_group_chat_to_dict(self): - group_chat = telegram.Chat.de_json(self.json_dict) + group_chat = telegram.Chat.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(group_chat.to_dict())) self.assertEqual(group_chat['id'], self.id) self.assertEqual(group_chat['title'], self.title) self.assertEqual(group_chat['type'], self.type) + @flaky(3, 1) + def test_send_action(self): + """Test for Chat.send_action""" + self.json_dict['id'] = self._chat_id + group_chat = telegram.Chat.de_json(self.json_dict, self._bot) + group_chat.bot = self._bot + + result = group_chat.send_action(telegram.ChatAction.TYPING) + + self.assertTrue(result) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_choseninlineresult.py b/tests/test_choseninlineresult.py index ed757f26a..2cbf853d4 100644 --- a/tests/test_choseninlineresult.py +++ b/tests/test_choseninlineresult.py @@ -45,19 +45,19 @@ class ChosenInlineResultTest(BaseTest, unittest.TestCase): } def test_choseninlineresult_de_json(self): - result = telegram.ChosenInlineResult.de_json(self.json_dict) + result = telegram.ChosenInlineResult.de_json(self.json_dict, self._bot) self.assertEqual(result.result_id, self.result_id) self.assertDictEqual(result.from_user.to_dict(), self.from_user.to_dict()) self.assertEqual(result.query, self.query) def test_choseninlineresult_to_json(self): - result = telegram.ChosenInlineResult.de_json(self.json_dict) + result = telegram.ChosenInlineResult.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(result.to_json())) def test_choseninlineresult_to_dict(self): - result = telegram.ChosenInlineResult.de_json(self.json_dict).to_dict() + result = telegram.ChosenInlineResult.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(result)) self.assertEqual(result['result_id'], self.result_id) diff --git a/tests/test_contact.py b/tests/test_contact.py index 1df539fe6..21292078e 100644 --- a/tests/test_contact.py +++ b/tests/test_contact.py @@ -20,6 +20,9 @@ import unittest import sys + +from flaky import flaky + sys.path.append('.') import telegram @@ -43,7 +46,7 @@ class ContactTest(BaseTest, unittest.TestCase): } def test_contact_de_json(self): - contact = telegram.Contact.de_json(self.json_dict) + contact = telegram.Contact.de_json(self.json_dict, self._bot) self.assertEqual(contact.phone_number, self.phone_number) self.assertEqual(contact.first_name, self.first_name) @@ -51,12 +54,12 @@ class ContactTest(BaseTest, unittest.TestCase): self.assertEqual(contact.user_id, self.user_id) def test_contact_to_json(self): - contact = telegram.Contact.de_json(self.json_dict) + contact = telegram.Contact.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(contact.to_json())) def test_contact_to_dict(self): - contact = telegram.Contact.de_json(self.json_dict) + contact = telegram.Contact.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(contact.to_dict())) self.assertEqual(contact['phone_number'], self.phone_number) @@ -64,6 +67,15 @@ class ContactTest(BaseTest, unittest.TestCase): self.assertEqual(contact['last_name'], self.last_name) self.assertEqual(contact['user_id'], self.user_id) + @flaky(3, 1) + def test_reply_contact(self): + """Test for Message.reply_contact""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_contact(self.phone_number, self.first_name) + + self.assertEqual(message.contact.phone_number, self.phone_number) + self.assertEqual(message.contact.first_name, self.first_name) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_document.py b/tests/test_document.py index 90d80a5dd..99139cdd5 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -109,7 +109,7 @@ class DocumentTest(BaseTest, unittest.TestCase): self.assertEqual(document.mime_type, self.mime_type) def test_document_de_json(self): - document = telegram.Document.de_json(self.json_dict) + document = telegram.Document.de_json(self.json_dict, self._bot) self.assertEqual(document.file_id, self.document_file_id) self.assertTrue(isinstance(document.thumb, telegram.PhotoSize)) @@ -118,12 +118,12 @@ class DocumentTest(BaseTest, unittest.TestCase): self.assertEqual(document.file_size, self.file_size) def test_document_to_json(self): - document = telegram.Document.de_json(self.json_dict) + document = telegram.Document.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(document.to_json())) def test_document_to_dict(self): - document = telegram.Document.de_json(self.json_dict) + document = telegram.Document.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(document.to_dict())) self.assertEqual(document['file_id'], self.document_file_id) @@ -167,6 +167,15 @@ class DocumentTest(BaseTest, unittest.TestCase): lambda: self._bot.sendDocument(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_document(self): + """Test for Message.reply_document""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_document(self.document_file) + + self.assertNotEqual(message.document.file_id, '') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_file.py b/tests/test_file.py index a58718ae7..0197cfb98 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -101,19 +101,19 @@ class FileTest(BaseTest, unittest.TestCase): self.assertTrue(os.path.isfile('telegram.ogg')) def test_file_de_json(self): - newFile = telegram.File.de_json(self.json_dict, None) + newFile = telegram.File.de_json(self.json_dict, self._bot) self.assertEqual(newFile.file_id, self.json_dict['file_id']) self.assertEqual(newFile.file_path, self.json_dict['file_path']) self.assertEqual(newFile.file_size, self.json_dict['file_size']) def test_file_to_json(self): - newFile = telegram.File.de_json(self.json_dict, None) + newFile = telegram.File.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(newFile.to_json())) def test_file_to_dict(self): - newFile = telegram.File.de_json(self.json_dict, None) + newFile = telegram.File.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(newFile.to_dict())) self.assertEqual(newFile['file_id'], self.json_dict['file_id']) diff --git a/tests/test_forcereply.py b/tests/test_forcereply.py index 7ab6caf13..4d0cabb86 100644 --- a/tests/test_forcereply.py +++ b/tests/test_forcereply.py @@ -42,29 +42,29 @@ class ForceReplyTest(BaseTest, unittest.TestCase): message = self._bot.sendMessage( self._chat_id, 'Моё судно на воздушной подушке полно угрей', - reply_markup=telegram.ForceReply.de_json(self.json_dict)) + reply_markup=telegram.ForceReply.de_json(self.json_dict, self._bot)) self.assertTrue(self.is_json(message.to_json())) self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей') def test_force_reply_de_json(self): - force_reply = telegram.ForceReply.de_json(self.json_dict) + force_reply = telegram.ForceReply.de_json(self.json_dict, self._bot) self.assertEqual(force_reply.force_reply, self.force_reply) self.assertEqual(force_reply.selective, self.selective) def test_force_reply_de_json_empty(self): - force_reply = telegram.ForceReply.de_json(None) + force_reply = telegram.ForceReply.de_json(None, self._bot) self.assertFalse(force_reply) def test_force_reply_to_json(self): - force_reply = telegram.ForceReply.de_json(self.json_dict) + force_reply = telegram.ForceReply.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(force_reply.to_json())) def test_force_reply_to_dict(self): - force_reply = telegram.ForceReply.de_json(self.json_dict) + force_reply = telegram.ForceReply.de_json(self.json_dict, self._bot) self.assertEqual(force_reply['force_reply'], self.force_reply) self.assertEqual(force_reply['selective'], self.selective) diff --git a/tests/test_inlinekeyboardbutton.py b/tests/test_inlinekeyboardbutton.py index 2b6b8c185..282797a22 100644 --- a/tests/test_inlinekeyboardbutton.py +++ b/tests/test_inlinekeyboardbutton.py @@ -45,7 +45,7 @@ class InlineKeyboardButtonTest(BaseTest, unittest.TestCase): } def test_inline_keyboard_button_de_json(self): - inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict) + inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict, self._bot) self.assertEqual(inline_keyboard_button.text, self.text) self.assertEqual(inline_keyboard_button.url, self.url) @@ -53,22 +53,23 @@ class InlineKeyboardButtonTest(BaseTest, unittest.TestCase): self.assertEqual(inline_keyboard_button.switch_inline_query, self.switch_inline_query) def test_inline_keyboard_button_de_json_empty(self): - inline_keyboard_button = telegram.InlineKeyboardButton.de_json(None) + inline_keyboard_button = telegram.InlineKeyboardButton.de_json(None, self._bot) self.assertFalse(inline_keyboard_button) def test_inline_keyboard_button_de_list_empty(self): - inline_keyboard_button = telegram.InlineKeyboardButton.de_list(None) + inline_keyboard_button = telegram.InlineKeyboardButton.de_list(None, self._bot) self.assertFalse(inline_keyboard_button) def test_inline_keyboard_button_to_json(self): - inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict) + inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(inline_keyboard_button.to_json())) def test_inline_keyboard_button_to_dict(self): - inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict).to_dict() + inline_keyboard_button = telegram.InlineKeyboardButton.de_json(self.json_dict, + self._bot).to_dict() self.assertTrue(self.is_dict(inline_keyboard_button)) self.assertDictEqual(self.json_dict, inline_keyboard_button) diff --git a/tests/test_inlinekeyboardmarkup.py b/tests/test_inlinekeyboardmarkup.py index bf869a254..564f9c816 100644 --- a/tests/test_inlinekeyboardmarkup.py +++ b/tests/test_inlinekeyboardmarkup.py @@ -51,12 +51,12 @@ class InlineKeyboardMarkupTest(BaseTest, unittest.TestCase): self.assertEqual(message.text, 'Testing InlineKeyboardMarkup') def test_inline_keyboard_markup_de_json_empty(self): - inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(None) + inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(None, self._bot) self.assertFalse(inline_keyboard_markup) def test_inline_keyboard_markup_de_json(self): - inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict) + inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(inline_keyboard_markup.inline_keyboard, list)) self.assertTrue( @@ -64,12 +64,12 @@ class InlineKeyboardMarkupTest(BaseTest, unittest.TestCase): telegram.InlineKeyboardButton)) def test_inline_keyboard_markup_to_json(self): - inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict) + inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(inline_keyboard_markup.to_json())) def test_inline_keyboard_markup_to_dict(self): - inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict) + inline_keyboard_markup = telegram.InlineKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(inline_keyboard_markup.inline_keyboard, list)) self.assertTrue( diff --git a/tests/test_inlinequery.py b/tests/test_inlinequery.py index 47560d03b..ca8077ce1 100644 --- a/tests/test_inlinequery.py +++ b/tests/test_inlinequery.py @@ -50,7 +50,7 @@ class InlineQueryTest(BaseTest, unittest.TestCase): } def test_inlinequery_de_json(self): - inlinequery = telegram.InlineQuery.de_json(self.json_dict) + inlinequery = telegram.InlineQuery.de_json(self.json_dict, self._bot) self.assertEqual(inlinequery.id, self.id) self.assertDictEqual(inlinequery.from_user.to_dict(), self.from_user.to_dict()) @@ -59,12 +59,12 @@ class InlineQueryTest(BaseTest, unittest.TestCase): self.assertEqual(inlinequery.offset, self.offset) def test_inlinequery_to_json(self): - inlinequery = telegram.InlineQuery.de_json(self.json_dict) + inlinequery = telegram.InlineQuery.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(inlinequery.to_json())) def test_inlinequery_to_dict(self): - inlinequery = telegram.InlineQuery.de_json(self.json_dict).to_dict() + inlinequery = telegram.InlineQuery.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(inlinequery)) self.assertDictEqual(inlinequery, self.json_dict) diff --git a/tests/test_inlinequeryresultarticle.py b/tests/test_inlinequeryresultarticle.py index 9e0fa3972..44373c032 100644 --- a/tests/test_inlinequeryresultarticle.py +++ b/tests/test_inlinequeryresultarticle.py @@ -61,7 +61,7 @@ class InlineQueryResultArticleTest(BaseTest, unittest.TestCase): } def test_article_de_json(self): - article = telegram.InlineQueryResultArticle.de_json(self.json_dict) + article = telegram.InlineQueryResultArticle.de_json(self.json_dict, self._bot) self.assertEqual(article.type, self.type) self.assertEqual(article.id, self.id) @@ -77,12 +77,12 @@ class InlineQueryResultArticleTest(BaseTest, unittest.TestCase): self.assertEqual(article.thumb_width, self.thumb_width) def test_article_to_json(self): - article = telegram.InlineQueryResultArticle.de_json(self.json_dict) + article = telegram.InlineQueryResultArticle.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(article.to_json())) def test_article_to_dict(self): - article = telegram.InlineQueryResultArticle.de_json(self.json_dict).to_dict() + article = telegram.InlineQueryResultArticle.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(article)) self.assertDictEqual(self.json_dict, article) diff --git a/tests/test_inlinequeryresultaudio.py b/tests/test_inlinequeryresultaudio.py index 3d5ab8cf2..ec04b77f4 100644 --- a/tests/test_inlinequeryresultaudio.py +++ b/tests/test_inlinequeryresultaudio.py @@ -55,7 +55,7 @@ class InlineQueryResultAudioTest(BaseTest, unittest.TestCase): } def test_audio_de_json(self): - audio = telegram.InlineQueryResultAudio.de_json(self.json_dict) + audio = telegram.InlineQueryResultAudio.de_json(self.json_dict, self._bot) self.assertEqual(audio.type, self.type) self.assertEqual(audio.id, self.id) @@ -68,12 +68,12 @@ class InlineQueryResultAudioTest(BaseTest, unittest.TestCase): self.assertDictEqual(audio.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_audio_to_json(self): - audio = telegram.InlineQueryResultAudio.de_json(self.json_dict) + audio = telegram.InlineQueryResultAudio.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(audio.to_json())) def test_audio_to_dict(self): - audio = telegram.InlineQueryResultAudio.de_json(self.json_dict).to_dict() + audio = telegram.InlineQueryResultAudio.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(audio)) self.assertDictEqual(self.json_dict, audio) diff --git a/tests/test_inlinequeryresultcachedaudio.py b/tests/test_inlinequeryresultcachedaudio.py index cb3787e49..13216ae35 100644 --- a/tests/test_inlinequeryresultcachedaudio.py +++ b/tests/test_inlinequeryresultcachedaudio.py @@ -50,7 +50,7 @@ class InlineQueryResultCachedAudioTest(BaseTest, unittest.TestCase): } def test_audio_de_json(self): - audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict) + audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict, self._bot) self.assertEqual(audio.type, self.type) self.assertEqual(audio.id, self.id) @@ -60,12 +60,12 @@ class InlineQueryResultCachedAudioTest(BaseTest, unittest.TestCase): self.assertDictEqual(audio.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_audio_to_json(self): - audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict) + audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(audio.to_json())) def test_audio_to_dict(self): - audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict).to_dict() + audio = telegram.InlineQueryResultCachedAudio.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(audio)) self.assertDictEqual(self.json_dict, audio) diff --git a/tests/test_inlinequeryresultcacheddocument.py b/tests/test_inlinequeryresultcacheddocument.py index 144f4d846..cde4bcc31 100644 --- a/tests/test_inlinequeryresultcacheddocument.py +++ b/tests/test_inlinequeryresultcacheddocument.py @@ -55,7 +55,7 @@ class InlineQueryResultCachedDocumentTest(BaseTest, unittest.TestCase): } def test_document_de_json(self): - document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict) + document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict, self._bot) self.assertEqual(document.id, self.id) self.assertEqual(document.type, self.type) @@ -68,12 +68,13 @@ class InlineQueryResultCachedDocumentTest(BaseTest, unittest.TestCase): self.assertDictEqual(document.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_document_to_json(self): - document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict) + document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(document.to_json())) def test_document_to_dict(self): - document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict).to_dict() + document = telegram.InlineQueryResultCachedDocument.de_json(self.json_dict, + self._bot).to_dict() self.assertTrue(self.is_dict(document)) self.assertDictEqual(self.json_dict, document) diff --git a/tests/test_inlinequeryresultcachedgif.py b/tests/test_inlinequeryresultcachedgif.py index df3556b19..a04e77e31 100644 --- a/tests/test_inlinequeryresultcachedgif.py +++ b/tests/test_inlinequeryresultcachedgif.py @@ -53,7 +53,7 @@ class InlineQueryResultCachedGifTest(BaseTest, unittest.TestCase): } def test_gif_de_json(self): - gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict) + gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict, self._bot) self.assertEqual(gif.type, self.type) self.assertEqual(gif.id, self.id) @@ -65,12 +65,12 @@ class InlineQueryResultCachedGifTest(BaseTest, unittest.TestCase): self.assertDictEqual(gif.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_gif_to_json(self): - gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict) + gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(gif.to_json())) def test_gif_to_dict(self): - gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict).to_dict() + gif = telegram.InlineQueryResultCachedGif.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(gif)) self.assertDictEqual(self.json_dict, gif) diff --git a/tests/test_inlinequeryresultcachedmpeg4gif.py b/tests/test_inlinequeryresultcachedmpeg4gif.py index 609795b4c..c8cb8fc32 100644 --- a/tests/test_inlinequeryresultcachedmpeg4gif.py +++ b/tests/test_inlinequeryresultcachedmpeg4gif.py @@ -54,7 +54,7 @@ class InlineQueryResultCachedMpeg4GifTest(BaseTest, unittest.TestCase): } def test_mpeg4_de_json(self): - mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict) + mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict, self._bot) self.assertEqual(mpeg4.type, self.type) self.assertEqual(mpeg4.id, self.id) @@ -66,12 +66,13 @@ class InlineQueryResultCachedMpeg4GifTest(BaseTest, unittest.TestCase): self.assertDictEqual(mpeg4.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_mpeg4_to_json(self): - mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict) + mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(mpeg4.to_json())) def test_mpeg4_to_dict(self): - mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict).to_dict() + mpeg4 = telegram.InlineQueryResultCachedMpeg4Gif.de_json(self.json_dict, + self._bot).to_dict() self.assertTrue(self.is_dict(mpeg4)) self.assertDictEqual(self.json_dict, mpeg4) diff --git a/tests/test_inlinequeryresultcachedphoto.py b/tests/test_inlinequeryresultcachedphoto.py index 67beb03c9..cee484c02 100644 --- a/tests/test_inlinequeryresultcachedphoto.py +++ b/tests/test_inlinequeryresultcachedphoto.py @@ -56,7 +56,7 @@ class InlineQueryResultCachedPhotoTest(BaseTest, unittest.TestCase): } def test_photo_de_json(self): - photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict) + photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict, self._bot) self.assertEqual(photo.type, self.type) self.assertEqual(photo.id, self.id) @@ -69,12 +69,12 @@ class InlineQueryResultCachedPhotoTest(BaseTest, unittest.TestCase): self.assertDictEqual(photo.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_photo_to_json(self): - photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict) + photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(photo.to_json())) def test_photo_to_dict(self): - photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict).to_dict() + photo = telegram.InlineQueryResultCachedPhoto.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(photo)) self.assertDictEqual(self.json_dict, photo) diff --git a/tests/test_inlinequeryresultcachedsticker.py b/tests/test_inlinequeryresultcachedsticker.py index b3d1a99f7..bccec6b5f 100644 --- a/tests/test_inlinequeryresultcachedsticker.py +++ b/tests/test_inlinequeryresultcachedsticker.py @@ -50,7 +50,7 @@ class InlineQueryResultCachedStickerTest(BaseTest, unittest.TestCase): } def test_sticker_de_json(self): - sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict) + sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict, self._bot) self.assertEqual(sticker.type, self.type) self.assertEqual(sticker.id, self.id) @@ -60,12 +60,13 @@ class InlineQueryResultCachedStickerTest(BaseTest, unittest.TestCase): self.assertDictEqual(sticker.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_sticker_to_json(self): - sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict) + sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(sticker.to_json())) def test_sticker_to_dict(self): - sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict).to_dict() + sticker = telegram.InlineQueryResultCachedSticker.de_json(self.json_dict, + self._bot).to_dict() self.assertTrue(self.is_dict(sticker)) self.assertDictEqual(self.json_dict, sticker) diff --git a/tests/test_inlinequeryresultcachedvideo.py b/tests/test_inlinequeryresultcachedvideo.py index 84e4d9687..b78180056 100644 --- a/tests/test_inlinequeryresultcachedvideo.py +++ b/tests/test_inlinequeryresultcachedvideo.py @@ -56,7 +56,7 @@ class InlineQueryResultCachedVideoTest(BaseTest, unittest.TestCase): } def test_video_de_json(self): - video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict) + video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict, self._bot) self.assertEqual(video.type, self.type) self.assertEqual(video.id, self.id) @@ -69,12 +69,12 @@ class InlineQueryResultCachedVideoTest(BaseTest, unittest.TestCase): self.assertDictEqual(video.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_video_to_json(self): - video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict) + video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(video.to_json())) def test_video_to_dict(self): - video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict).to_dict() + video = telegram.InlineQueryResultCachedVideo.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(video)) self.assertDictEqual(self.json_dict, video) diff --git a/tests/test_inlinequeryresultcachedvoice.py b/tests/test_inlinequeryresultcachedvoice.py index 5297f1d5e..9bddb2fe6 100644 --- a/tests/test_inlinequeryresultcachedvoice.py +++ b/tests/test_inlinequeryresultcachedvoice.py @@ -54,7 +54,7 @@ class InlineQueryResultCachedVoiceTest(BaseTest, unittest.TestCase): } def test_voice_de_json(self): - voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict) + voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict, self._bot) self.assertEqual(voice.type, self.type) self.assertEqual(voice.id, self.id) @@ -66,12 +66,12 @@ class InlineQueryResultCachedVoiceTest(BaseTest, unittest.TestCase): self.assertDictEqual(voice.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_voice_to_json(self): - voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict) + voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(voice.to_json())) def test_voice_to_dict(self): - voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict).to_dict() + voice = telegram.InlineQueryResultCachedVoice.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(voice)) self.assertDictEqual(self.json_dict, voice) diff --git a/tests/test_inlinequeryresultcontact.py b/tests/test_inlinequeryresultcontact.py index f36fc5cbe..5187e8e85 100644 --- a/tests/test_inlinequeryresultcontact.py +++ b/tests/test_inlinequeryresultcontact.py @@ -58,7 +58,7 @@ class InlineQueryResultContactTest(BaseTest, unittest.TestCase): } def test_contact_de_json(self): - contact = telegram.InlineQueryResultContact.de_json(self.json_dict) + contact = telegram.InlineQueryResultContact.de_json(self.json_dict, self._bot) self.assertEqual(contact.id, self.id) self.assertEqual(contact.type, self.type) @@ -73,12 +73,12 @@ class InlineQueryResultContactTest(BaseTest, unittest.TestCase): self.assertDictEqual(contact.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_contact_to_json(self): - contact = telegram.InlineQueryResultContact.de_json(self.json_dict) + contact = telegram.InlineQueryResultContact.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(contact.to_json())) def test_contact_to_dict(self): - contact = telegram.InlineQueryResultContact.de_json(self.json_dict).to_dict() + contact = telegram.InlineQueryResultContact.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(contact)) self.assertDictEqual(self.json_dict, contact) diff --git a/tests/test_inlinequeryresultdocument.py b/tests/test_inlinequeryresultdocument.py index 91b7caf42..fee73e8ff 100644 --- a/tests/test_inlinequeryresultdocument.py +++ b/tests/test_inlinequeryresultdocument.py @@ -62,7 +62,7 @@ class InlineQueryResultDocumentTest(BaseTest, unittest.TestCase): } def test_document_de_json(self): - document = telegram.InlineQueryResultDocument.de_json(self.json_dict) + document = telegram.InlineQueryResultDocument.de_json(self.json_dict, self._bot) self.assertEqual(document.id, self.id) self.assertEqual(document.type, self.type) @@ -79,12 +79,12 @@ class InlineQueryResultDocumentTest(BaseTest, unittest.TestCase): self.assertDictEqual(document.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_document_to_json(self): - document = telegram.InlineQueryResultDocument.de_json(self.json_dict) + document = telegram.InlineQueryResultDocument.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(document.to_json())) def test_document_to_dict(self): - document = telegram.InlineQueryResultDocument.de_json(self.json_dict).to_dict() + document = telegram.InlineQueryResultDocument.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(document)) self.assertDictEqual(self.json_dict, document) diff --git a/tests/test_inlinequeryresultgif.py b/tests/test_inlinequeryresultgif.py index d4f8d8861..8e52e4680 100644 --- a/tests/test_inlinequeryresultgif.py +++ b/tests/test_inlinequeryresultgif.py @@ -59,7 +59,7 @@ class InlineQueryResultGifTest(BaseTest, unittest.TestCase): } def test_gif_de_json(self): - gif = telegram.InlineQueryResultGif.de_json(self.json_dict) + gif = telegram.InlineQueryResultGif.de_json(self.json_dict, self._bot) self.assertEqual(gif.type, self.type) self.assertEqual(gif.id, self.id) @@ -74,12 +74,12 @@ class InlineQueryResultGifTest(BaseTest, unittest.TestCase): self.assertDictEqual(gif.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_gif_to_json(self): - gif = telegram.InlineQueryResultGif.de_json(self.json_dict) + gif = telegram.InlineQueryResultGif.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(gif.to_json())) def test_gif_to_dict(self): - gif = telegram.InlineQueryResultGif.de_json(self.json_dict).to_dict() + gif = telegram.InlineQueryResultGif.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(gif)) self.assertDictEqual(self.json_dict, gif) diff --git a/tests/test_inlinequeryresultlocation.py b/tests/test_inlinequeryresultlocation.py index 903bb4545..f47981a91 100644 --- a/tests/test_inlinequeryresultlocation.py +++ b/tests/test_inlinequeryresultlocation.py @@ -58,7 +58,7 @@ class InlineQueryResultLocationTest(BaseTest, unittest.TestCase): } def test_location_de_json(self): - location = telegram.InlineQueryResultLocation.de_json(self.json_dict) + location = telegram.InlineQueryResultLocation.de_json(self.json_dict, self._bot) self.assertEqual(location.id, self.id) self.assertEqual(location.type, self.type) @@ -73,12 +73,12 @@ class InlineQueryResultLocationTest(BaseTest, unittest.TestCase): self.assertDictEqual(location.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_location_to_json(self): - location = telegram.InlineQueryResultLocation.de_json(self.json_dict) + location = telegram.InlineQueryResultLocation.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(location.to_json())) def test_location_to_dict(self): - location = telegram.InlineQueryResultLocation.de_json(self.json_dict).to_dict() + location = telegram.InlineQueryResultLocation.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(location)) self.assertDictEqual(self.json_dict, location) diff --git a/tests/test_inlinequeryresultmpeg4gif.py b/tests/test_inlinequeryresultmpeg4gif.py index 751017e9c..1a55db69d 100644 --- a/tests/test_inlinequeryresultmpeg4gif.py +++ b/tests/test_inlinequeryresultmpeg4gif.py @@ -59,7 +59,7 @@ class InlineQueryResultMpeg4GifTest(BaseTest, unittest.TestCase): } def test_mpeg4_de_json(self): - mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict) + mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict, self._bot) self.assertEqual(mpeg4.type, self.type) self.assertEqual(mpeg4.id, self.id) @@ -74,12 +74,12 @@ class InlineQueryResultMpeg4GifTest(BaseTest, unittest.TestCase): self.assertDictEqual(mpeg4.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_mpeg4_to_json(self): - mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict) + mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(mpeg4.to_json())) def test_mpeg4_to_dict(self): - mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict).to_dict() + mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(mpeg4)) self.assertDictEqual(self.json_dict, mpeg4) diff --git a/tests/test_inlinequeryresultphoto.py b/tests/test_inlinequeryresultphoto.py index 76cf22ad4..fd178ed5b 100644 --- a/tests/test_inlinequeryresultphoto.py +++ b/tests/test_inlinequeryresultphoto.py @@ -61,7 +61,7 @@ class InlineQueryResultPhotoTest(BaseTest, unittest.TestCase): } def test_photo_de_json(self): - photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict) + photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict, self._bot) self.assertEqual(photo.type, self.type) self.assertEqual(photo.id, self.id) @@ -77,12 +77,12 @@ class InlineQueryResultPhotoTest(BaseTest, unittest.TestCase): self.assertDictEqual(photo.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_photo_to_json(self): - photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict) + photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(photo.to_json())) def test_photo_to_dict(self): - photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict).to_dict() + photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(photo)) self.assertDictEqual(self.json_dict, photo) diff --git a/tests/test_inlinequeryresultvenue.py b/tests/test_inlinequeryresultvenue.py index 73af4c4bb..a2620736a 100644 --- a/tests/test_inlinequeryresultvenue.py +++ b/tests/test_inlinequeryresultvenue.py @@ -62,7 +62,7 @@ class InlineQueryResultVenueTest(BaseTest, unittest.TestCase): } def test_venue_de_json(self): - venue = telegram.InlineQueryResultVenue.de_json(self.json_dict) + venue = telegram.InlineQueryResultVenue.de_json(self.json_dict, self._bot) self.assertEqual(venue.id, self.id) self.assertEqual(venue.type, self.type) @@ -79,12 +79,12 @@ class InlineQueryResultVenueTest(BaseTest, unittest.TestCase): self.assertDictEqual(venue.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_venue_to_json(self): - venue = telegram.InlineQueryResultVenue.de_json(self.json_dict) + venue = telegram.InlineQueryResultVenue.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(venue.to_json())) def test_venue_to_dict(self): - venue = telegram.InlineQueryResultVenue.de_json(self.json_dict).to_dict() + venue = telegram.InlineQueryResultVenue.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(venue)) self.assertDictEqual(self.json_dict, venue) diff --git a/tests/test_inlinequeryresultvideo.py b/tests/test_inlinequeryresultvideo.py index 5d33182a8..d14f0b0d2 100644 --- a/tests/test_inlinequeryresultvideo.py +++ b/tests/test_inlinequeryresultvideo.py @@ -65,7 +65,7 @@ class InlineQueryResultVideoTest(BaseTest, unittest.TestCase): } def test_video_de_json(self): - video = telegram.InlineQueryResultVideo.de_json(self.json_dict) + video = telegram.InlineQueryResultVideo.de_json(self.json_dict, self._bot) self.assertEqual(video.type, self.type) self.assertEqual(video.id, self.id) @@ -83,12 +83,12 @@ class InlineQueryResultVideoTest(BaseTest, unittest.TestCase): self.assertDictEqual(video.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_video_to_json(self): - video = telegram.InlineQueryResultVideo.de_json(self.json_dict) + video = telegram.InlineQueryResultVideo.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(video.to_json())) def test_video_to_dict(self): - video = telegram.InlineQueryResultVideo.de_json(self.json_dict).to_dict() + video = telegram.InlineQueryResultVideo.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(video)) self.assertDictEqual(self.json_dict, video) diff --git a/tests/test_inlinequeryresultvoice.py b/tests/test_inlinequeryresultvoice.py index 4a7eb80ef..5a8a932c8 100644 --- a/tests/test_inlinequeryresultvoice.py +++ b/tests/test_inlinequeryresultvoice.py @@ -53,7 +53,7 @@ class InlineQueryResultVoiceTest(BaseTest, unittest.TestCase): } def test_voice_de_json(self): - voice = telegram.InlineQueryResultVoice.de_json(self.json_dict) + voice = telegram.InlineQueryResultVoice.de_json(self.json_dict, self._bot) self.assertEqual(voice.type, self.type) self.assertEqual(voice.id, self.id) @@ -65,12 +65,12 @@ class InlineQueryResultVoiceTest(BaseTest, unittest.TestCase): self.assertDictEqual(voice.reply_markup.to_dict(), self.reply_markup.to_dict()) def test_voice_to_json(self): - voice = telegram.InlineQueryResultVoice.de_json(self.json_dict) + voice = telegram.InlineQueryResultVoice.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(voice.to_json())) def test_voice_to_dict(self): - voice = telegram.InlineQueryResultVoice.de_json(self.json_dict).to_dict() + voice = telegram.InlineQueryResultVoice.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(voice)) self.assertDictEqual(self.json_dict, voice) diff --git a/tests/test_inputcontactmessagecontent.py b/tests/test_inputcontactmessagecontent.py index 41ee007bf..c4c8c4d4c 100644 --- a/tests/test_inputcontactmessagecontent.py +++ b/tests/test_inputcontactmessagecontent.py @@ -43,14 +43,14 @@ class InputContactMessageContentTest(BaseTest, unittest.TestCase): } def test_icmc_de_json(self): - icmc = telegram.InputContactMessageContent.de_json(self.json_dict) + icmc = telegram.InputContactMessageContent.de_json(self.json_dict, self._bot) self.assertEqual(icmc.first_name, self.first_name) self.assertEqual(icmc.phone_number, self.phone_number) self.assertEqual(icmc.last_name, self.last_name) def test_icmc_de_json_factory(self): - icmc = telegram.InputMessageContent.de_json(self.json_dict) + icmc = telegram.InputMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(icmc, telegram.InputContactMessageContent)) @@ -60,17 +60,17 @@ class InputContactMessageContentTest(BaseTest, unittest.TestCase): del (json_dict['phone_number']) del (json_dict['first_name']) - icmc = telegram.InputMessageContent.de_json(json_dict) + icmc = telegram.InputMessageContent.de_json(json_dict, self._bot) self.assertFalse(icmc) def test_icmc_to_json(self): - icmc = telegram.InputContactMessageContent.de_json(self.json_dict) + icmc = telegram.InputContactMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(icmc.to_json())) def test_icmc_to_dict(self): - icmc = telegram.InputContactMessageContent.de_json(self.json_dict).to_dict() + icmc = telegram.InputContactMessageContent.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(icmc)) self.assertDictEqual(self.json_dict, icmc) diff --git a/tests/test_inputlocationmessagecontent.py b/tests/test_inputlocationmessagecontent.py index ca7c6b953..b45a630b9 100644 --- a/tests/test_inputlocationmessagecontent.py +++ b/tests/test_inputlocationmessagecontent.py @@ -39,13 +39,13 @@ class InputLocationMessageContentTest(BaseTest, unittest.TestCase): 'latitude': self.latitude,} def test_ilmc_de_json(self): - ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict) + ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict, self._bot) self.assertEqual(ilmc.longitude, self.longitude) self.assertEqual(ilmc.latitude, self.latitude) def test_ilmc_de_json_factory(self): - ilmc = telegram.InputMessageContent.de_json(self.json_dict) + ilmc = telegram.InputMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(ilmc, telegram.InputLocationMessageContent)) @@ -56,17 +56,17 @@ class InputLocationMessageContentTest(BaseTest, unittest.TestCase): # If none args are sent it will fall in a different condition # del (json_dict['latitude']) - ilmc = telegram.InputMessageContent.de_json(json_dict) + ilmc = telegram.InputMessageContent.de_json(json_dict, self._bot) self.assertFalse(ilmc) def test_ilmc_to_json(self): - ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict) + ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(ilmc.to_json())) def test_ilmc_to_dict(self): - ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict).to_dict() + ilmc = telegram.InputLocationMessageContent.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(ilmc)) self.assertDictEqual(self.json_dict, ilmc) diff --git a/tests/test_inputmessagecontent.py b/tests/test_inputmessagecontent.py index ad9c08718..a26b798c3 100644 --- a/tests/test_inputmessagecontent.py +++ b/tests/test_inputmessagecontent.py @@ -32,7 +32,7 @@ class InputMessageContentTest(BaseTest, unittest.TestCase): """This object represents Tests for Telegram InputMessageContent.""" def test_imc_de_json(self): - imc = telegram.InputMessageContent.de_json(None) + imc = telegram.InputMessageContent.de_json(None, self._bot) self.assertFalse(imc) diff --git a/tests/test_inputtextmessagecontent.py b/tests/test_inputtextmessagecontent.py index 7aa1fe873..c14266733 100644 --- a/tests/test_inputtextmessagecontent.py +++ b/tests/test_inputtextmessagecontent.py @@ -43,14 +43,14 @@ class InputTextMessageContentTest(BaseTest, unittest.TestCase): } def test_itmc_de_json(self): - itmc = telegram.InputTextMessageContent.de_json(self.json_dict) + itmc = telegram.InputTextMessageContent.de_json(self.json_dict, self._bot) self.assertEqual(itmc.parse_mode, self.parse_mode) self.assertEqual(itmc.message_text, self.message_text) self.assertEqual(itmc.disable_web_page_preview, self.disable_web_page_preview) def test_itmc_de_json_factory(self): - itmc = telegram.InputMessageContent.de_json(self.json_dict) + itmc = telegram.InputMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(itmc, telegram.InputTextMessageContent)) @@ -59,17 +59,17 @@ class InputTextMessageContentTest(BaseTest, unittest.TestCase): del (json_dict['message_text']) - itmc = telegram.InputMessageContent.de_json(json_dict) + itmc = telegram.InputMessageContent.de_json(json_dict, self._bot) self.assertFalse(itmc) def test_itmc_to_json(self): - itmc = telegram.InputTextMessageContent.de_json(self.json_dict) + itmc = telegram.InputTextMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(itmc.to_json())) def test_itmc_to_dict(self): - itmc = telegram.InputTextMessageContent.de_json(self.json_dict).to_dict() + itmc = telegram.InputTextMessageContent.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(itmc)) self.assertDictEqual(self.json_dict, itmc) diff --git a/tests/test_inputvenuemessagecontent.py b/tests/test_inputvenuemessagecontent.py index a1ce7311b..88b83dedb 100644 --- a/tests/test_inputvenuemessagecontent.py +++ b/tests/test_inputvenuemessagecontent.py @@ -47,7 +47,7 @@ class InputVenueMessageContentTest(BaseTest, unittest.TestCase): } def test_ivmc_de_json(self): - ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict) + ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict, self._bot) self.assertEqual(ivmc.longitude, self.longitude) self.assertEqual(ivmc.latitude, self.latitude) @@ -56,7 +56,7 @@ class InputVenueMessageContentTest(BaseTest, unittest.TestCase): self.assertEqual(ivmc.foursquare_id, self.foursquare_id) def test_ivmc_de_json_factory(self): - ivmc = telegram.InputMessageContent.de_json(self.json_dict) + ivmc = telegram.InputMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(ivmc, telegram.InputVenueMessageContent)) @@ -68,17 +68,17 @@ class InputVenueMessageContentTest(BaseTest, unittest.TestCase): del (json_dict['title']) del (json_dict['address']) - ivmc = telegram.InputMessageContent.de_json(json_dict) + ivmc = telegram.InputMessageContent.de_json(json_dict, self._bot) self.assertFalse(ivmc) def test_ivmc_to_json(self): - ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict) + ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(ivmc.to_json())) def test_ivmc_to_dict(self): - ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict).to_dict() + ivmc = telegram.InputVenueMessageContent.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(ivmc)) self.assertDictEqual(self.json_dict, ivmc) diff --git a/tests/test_keyboardbutton.py b/tests/test_keyboardbutton.py index 9dab40120..f4f1e6338 100644 --- a/tests/test_keyboardbutton.py +++ b/tests/test_keyboardbutton.py @@ -44,29 +44,29 @@ class KeyboardButtonTest(BaseTest, unittest.TestCase): } def test_keyboard_button_de_json(self): - keyboard_button = telegram.KeyboardButton.de_json(self.json_dict) + keyboard_button = telegram.KeyboardButton.de_json(self.json_dict, self._bot) self.assertEqual(keyboard_button.text, self.text) self.assertEqual(keyboard_button.request_location, self.request_location) self.assertEqual(keyboard_button.request_contact, self.request_contact) def test_keyboard_button_de_json_empty(self): - keyboard_button = telegram.KeyboardButton.de_json(None) + keyboard_button = telegram.KeyboardButton.de_json(None, self._bot) self.assertFalse(keyboard_button) def test_keyboard_button_de_list_empty(self): - keyboard_button = telegram.KeyboardButton.de_list(None) + keyboard_button = telegram.KeyboardButton.de_list(None, self._bot) self.assertFalse(keyboard_button) def test_keyboard_button_to_json(self): - keyboard_button = telegram.KeyboardButton.de_json(self.json_dict) + keyboard_button = telegram.KeyboardButton.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(keyboard_button.to_json())) def test_keyboard_button_to_dict(self): - keyboard_button = telegram.KeyboardButton.de_json(self.json_dict).to_dict() + keyboard_button = telegram.KeyboardButton.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(keyboard_button)) self.assertDictEqual(self.json_dict, keyboard_button) diff --git a/tests/test_location.py b/tests/test_location.py index 59b7fcdbb..ba801e606 100644 --- a/tests/test_location.py +++ b/tests/test_location.py @@ -20,6 +20,9 @@ import unittest import sys + +from flaky import flaky + sys.path.append('.') import telegram @@ -53,18 +56,18 @@ class LocationTest(BaseTest, unittest.TestCase): self.assertEqual(location.longitude, self.longitude) def test_location_de_json(self): - location = telegram.Location.de_json(self.json_dict) + location = telegram.Location.de_json(self.json_dict, self._bot) self.assertEqual(location.latitude, self.latitude) self.assertEqual(location.longitude, self.longitude) def test_location_to_json(self): - location = telegram.Location.de_json(self.json_dict) + location = telegram.Location.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(location.to_json())) def test_location_to_dict(self): - location = telegram.Location.de_json(self.json_dict) + location = telegram.Location.de_json(self.json_dict, self._bot) self.assertEqual(location['latitude'], self.latitude) self.assertEqual(location['longitude'], self.longitude) @@ -89,6 +92,15 @@ class LocationTest(BaseTest, unittest.TestCase): lambda: self._bot.sendLocation(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + def test_reply_location(self): + """Test for Message.reply_location""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_location(self.latitude, self.longitude) + + self.assertEqual(message.location.latitude, self.latitude) + self.assertEqual(message.location.longitude, self.longitude) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_message.py b/tests/test_message.py index 8b3a8d51a..a036c9f0c 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -22,6 +22,8 @@ import sys import unittest +from flaky import flaky + sys.path.append('.') import telegram @@ -56,6 +58,24 @@ class MessageTest(BaseTest, unittest.TestCase): self.assertDictEqual(message.parse_entities(), {entity: 'http://google.com', entity_2: 'h'}) + @flaky(3, 1) + def test_reply_text(self): + """Test for Message.reply_text""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_text('Testing class method') + + self.assertTrue(self.is_json(message.to_json())) + self.assertEqual(message.text, 'Testing class method') + + @flaky(3, 1) + def test_forward(self): + """Test for Message.forward""" + message = self._bot.sendMessage(self._chat_id, 'Testing class method') + message = message.forward(self._chat_id) + + self.assertTrue(self.is_json(message.to_json())) + self.assertEqual(message.text, 'Testing class method') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_messageentity.py b/tests/test_messageentity.py index 7f154c144..c600afd27 100644 --- a/tests/test_messageentity.py +++ b/tests/test_messageentity.py @@ -45,7 +45,7 @@ class MessageEntityTest(BaseTest, unittest.TestCase): } def test_sticker_de_json(self): - sticker = telegram.MessageEntity.de_json(self.json_dict) + sticker = telegram.MessageEntity.de_json(self.json_dict, self._bot) self.assertEqual(sticker.type, self.type) self.assertEqual(sticker.offset, self.offset) @@ -53,12 +53,12 @@ class MessageEntityTest(BaseTest, unittest.TestCase): self.assertEqual(sticker.url, self.url) def test_sticker_to_json(self): - sticker = telegram.MessageEntity.de_json(self.json_dict) + sticker = telegram.MessageEntity.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(sticker.to_json())) def test_sticker_to_dict(self): - sticker = telegram.MessageEntity.de_json(self.json_dict).to_dict() + sticker = telegram.MessageEntity.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(sticker)) self.assertDictEqual(self.json_dict, sticker) diff --git a/tests/test_photo.py b/tests/test_photo.py index 15db261e4..1c1da78ec 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -140,7 +140,7 @@ class PhotoTest(BaseTest, unittest.TestCase): self.assertEqual(photo.height, self.height) def test_photo_de_json(self): - photo = telegram.PhotoSize.de_json(self.json_dict) + photo = telegram.PhotoSize.de_json(self.json_dict, self._bot) self.assertEqual(photo.file_id, self.photo_file_id) self.assertTrue(isinstance(photo, telegram.PhotoSize)) @@ -149,12 +149,12 @@ class PhotoTest(BaseTest, unittest.TestCase): self.assertEqual(photo.file_size, self.file_size) def test_photo_to_json(self): - photo = telegram.PhotoSize.de_json(self.json_dict) + photo = telegram.PhotoSize.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(photo.to_json())) def test_photo_to_dict(self): - photo = telegram.PhotoSize.de_json(self.json_dict) + photo = telegram.PhotoSize.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(photo.to_dict())) self.assertEqual(photo['file_id'], self.photo_file_id) @@ -200,6 +200,15 @@ class PhotoTest(BaseTest, unittest.TestCase): TypeError, lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_photo(self): + """Test for Message.reply_photo""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_photo(self.photo_file) + + self.assertNotEqual(message.photo[0].file_id, '') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_replykeyboardhide.py b/tests/test_replykeyboardhide.py index 8145e730a..02a221fed 100644 --- a/tests/test_replykeyboardhide.py +++ b/tests/test_replykeyboardhide.py @@ -42,29 +42,29 @@ class ReplyKeyboardHideTest(BaseTest, unittest.TestCase): message = self._bot.sendMessage( self._chat_id, 'Моё судно на воздушной подушке полно угрей', - reply_markup=telegram.ReplyKeyboardHide.de_json(self.json_dict)) + reply_markup=telegram.ReplyKeyboardHide.de_json(self.json_dict, self._bot)) self.assertTrue(self.is_json(message.to_json())) self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей') def test_reply_keyboard_hide_de_json(self): - reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict) + reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict, self._bot) self.assertEqual(reply_keyboard_hide.hide_keyboard, self.hide_keyboard) self.assertEqual(reply_keyboard_hide.selective, self.selective) def test_reply_keyboard_hide_de_json_empty(self): - reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(None) + reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(None, self._bot) self.assertFalse(reply_keyboard_hide) def test_reply_keyboard_hide_to_json(self): - reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict) + reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(reply_keyboard_hide.to_json())) def test_reply_keyboard_hide_to_dict(self): - reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict) + reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict, self._bot) self.assertEqual(reply_keyboard_hide['hide_keyboard'], self.hide_keyboard) self.assertEqual(reply_keyboard_hide['selective'], self.selective) diff --git a/tests/test_replykeyboardmarkup.py b/tests/test_replykeyboardmarkup.py index 9ccf94345..38752780e 100644 --- a/tests/test_replykeyboardmarkup.py +++ b/tests/test_replykeyboardmarkup.py @@ -48,18 +48,18 @@ class ReplyKeyboardMarkupTest(BaseTest, unittest.TestCase): message = self._bot.sendMessage( self._chat_id, 'Моё судно на воздушной подушке полно угрей', - reply_markup=telegram.ReplyKeyboardMarkup.de_json(self.json_dict)) + reply_markup=telegram.ReplyKeyboardMarkup.de_json(self.json_dict, self._bot)) self.assertTrue(self.is_json(message.to_json())) self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей') def test_reply_markup_empty_de_json_empty(self): - reply_markup_empty = telegram.ReplyKeyboardMarkup.de_json(None) + reply_markup_empty = telegram.ReplyKeyboardMarkup.de_json(None, self._bot) self.assertFalse(reply_markup_empty) def test_reply_keyboard_markup_de_json(self): - reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict) + reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(reply_keyboard_markup.keyboard, list)) self.assertTrue(isinstance(reply_keyboard_markup.keyboard[0][0], telegram.KeyboardButton)) @@ -68,12 +68,12 @@ class ReplyKeyboardMarkupTest(BaseTest, unittest.TestCase): self.assertEqual(reply_keyboard_markup.selective, self.selective) def test_reply_keyboard_markup_to_json(self): - reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict) + reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(reply_keyboard_markup.to_json())) def test_reply_keyboard_markup_to_dict(self): - reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict) + reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(reply_keyboard_markup.keyboard, list)) self.assertTrue(isinstance(reply_keyboard_markup.keyboard[0][0], telegram.KeyboardButton)) diff --git a/tests/test_replymarkup.py b/tests/test_replymarkup.py index 4e4e3715a..1843d548e 100644 --- a/tests/test_replymarkup.py +++ b/tests/test_replymarkup.py @@ -32,7 +32,7 @@ class ReplyMarkupTest(BaseTest, unittest.TestCase): """This object represents Tests for Telegram ReplyMarkup.""" def test_reply_markup_de_json_empty(self): - reply_markup = telegram.ReplyMarkup.de_json(None) + reply_markup = telegram.ReplyMarkup.de_json(None, self._bot) self.assertFalse(reply_markup) diff --git a/tests/test_sticker.py b/tests/test_sticker.py index f09c53f3d..9048511cc 100644 --- a/tests/test_sticker.py +++ b/tests/test_sticker.py @@ -77,7 +77,7 @@ class StickerTest(BaseTest, unittest.TestCase): self.assertEqual(sticker.file_size, self.file_size) def test_sticker_de_json(self): - sticker = telegram.Sticker.de_json(self.json_dict) + sticker = telegram.Sticker.de_json(self.json_dict, self._bot) self.assertEqual(sticker.file_id, self.sticker_file_id) self.assertEqual(sticker.width, self.width) @@ -87,12 +87,12 @@ class StickerTest(BaseTest, unittest.TestCase): self.assertEqual(sticker.file_size, self.file_size) def test_sticker_to_json(self): - sticker = telegram.Sticker.de_json(self.json_dict) + sticker = telegram.Sticker.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(sticker.to_json())) def test_sticker_to_dict(self): - sticker = telegram.Sticker.de_json(self.json_dict) + sticker = telegram.Sticker.de_json(self.json_dict, self._bot) self.assertEqual(sticker['file_id'], self.sticker_file_id) self.assertEqual(sticker['width'], self.width) @@ -136,6 +136,15 @@ class StickerTest(BaseTest, unittest.TestCase): TypeError, lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_sticker(self): + """Test for Message.reply_sticker""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_sticker(self.sticker_file_id) + + self.assertNotEqual(message.sticker.file_id, '') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_update.py b/tests/test_update.py index fdc8f0d11..f0ecd262e 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -48,23 +48,23 @@ class UpdateTest(BaseTest, unittest.TestCase): self.json_dict = {'update_id': self.update_id, 'message': self.message} def test_update_de_json(self): - update = telegram.Update.de_json(self.json_dict) + update = telegram.Update.de_json(self.json_dict, self._bot) self.assertEqual(update.update_id, self.update_id) self.assertTrue(isinstance(update.message, telegram.Message)) def test_update_de_json_empty(self): - update = telegram.Update.de_json(None) + update = telegram.Update.de_json(None, self._bot) self.assertFalse(update) def test_update_to_json(self): - update = telegram.Update.de_json(self.json_dict) + update = telegram.Update.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(update.to_json())) def test_update_to_dict(self): - update = telegram.Update.de_json(self.json_dict) + update = telegram.Update.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(update.to_dict())) self.assertEqual(update['update_id'], self.update_id) diff --git a/tests/test_updater.py b/tests/test_updater.py index 5b0e08995..a269378eb 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -799,6 +799,9 @@ class MockBot(object): else: return [] + def create_references(self, d): + pass + if __name__ == '__main__': unittest.main() diff --git a/tests/test_user.py b/tests/test_user.py index 4a0dd5033..0f65209a5 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -20,6 +20,9 @@ import unittest import sys + +from flaky import flaky + sys.path.append('.') import telegram @@ -45,7 +48,7 @@ class UserTest(BaseTest, unittest.TestCase): } def test_user_de_json(self): - user = telegram.User.de_json(self.json_dict) + user = telegram.User.de_json(self.json_dict, self._bot) self.assertEqual(user.id, self.id) self.assertEqual(user.first_name, self.first_name) @@ -60,7 +63,7 @@ class UserTest(BaseTest, unittest.TestCase): del (json_dict['username']) - user = telegram.User.de_json(self.json_dict) + user = telegram.User.de_json(self.json_dict, self._bot) self.assertEqual(user.id, self.id) self.assertEqual(user.first_name, self.first_name) @@ -75,7 +78,7 @@ class UserTest(BaseTest, unittest.TestCase): del (json_dict['username']) del (json_dict['last_name']) - user = telegram.User.de_json(self.json_dict) + user = telegram.User.de_json(self.json_dict, self._bot) self.assertEqual(user.id, self.id) self.assertEqual(user.first_name, self.first_name) @@ -83,12 +86,12 @@ class UserTest(BaseTest, unittest.TestCase): self.assertEqual(user.name, self.first_name) def test_user_to_json(self): - user = telegram.User.de_json(self.json_dict) + user = telegram.User.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(user.to_json())) def test_user_to_dict(self): - user = telegram.User.de_json(self.json_dict) + user = telegram.User.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(user.to_dict())) self.assertEqual(user['id'], self.id) @@ -97,6 +100,17 @@ class UserTest(BaseTest, unittest.TestCase): self.assertEqual(user['username'], self.username) self.assertEqual(user['type'], self.type) + @flaky(3, 1) + def test_get_profile_photos(self): + """Test for User.get_profile_photos""" + self.json_dict['id'] = self._chat_id + user = telegram.User.de_json(self.json_dict, self._bot) + user.bot = self._bot + + result = user.get_profile_photos() + + self.assertNotEquals(result, None) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_venue.py b/tests/test_venue.py index 853ad286f..d71adfc92 100644 --- a/tests/test_venue.py +++ b/tests/test_venue.py @@ -21,6 +21,8 @@ import sys import unittest +from flaky import flaky + sys.path.append('.') import telegram @@ -44,7 +46,7 @@ class VenueTest(BaseTest, unittest.TestCase): } def test_sticker_de_json(self): - sticker = telegram.Venue.de_json(self.json_dict) + sticker = telegram.Venue.de_json(self.json_dict, self._bot) self.assertTrue(isinstance(sticker.location, telegram.Location)) self.assertEqual(sticker.title, self.title) @@ -52,16 +54,26 @@ class VenueTest(BaseTest, unittest.TestCase): self.assertEqual(sticker.foursquare_id, self.foursquare_id) def test_sticker_to_json(self): - sticker = telegram.Venue.de_json(self.json_dict) + sticker = telegram.Venue.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(sticker.to_json())) def test_sticker_to_dict(self): - sticker = telegram.Venue.de_json(self.json_dict).to_dict() + sticker = telegram.Venue.de_json(self.json_dict, self._bot).to_dict() self.assertTrue(self.is_dict(sticker)) self.assertDictEqual(self.json_dict, sticker) + @flaky(3, 1) + def test_reply_venue(self): + """Test for Message.reply_venue""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_venue(self.location.latitude, self.location.longitude, self.title, + self._address) + + self.assertAlmostEqual(message.venue.location.latitude, self.location.latitude, 2) + self.assertAlmostEqual(message.venue.location.longitude, self.location.longitude, 2) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_video.py b/tests/test_video.py index 644180adc..4a8687980 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -43,7 +43,7 @@ class VideoTest(BaseTest, unittest.TestCase): self.thumb = telegram.PhotoSize.de_json({'file_id': 'AAQBABOMsecvAAQqqoY1Pee_MqcyAAIC', 'file_size': 645, 'height': 90, - 'width': 51}) + 'width': 51}, self._bot) self.mime_type = 'video/mp4' self.file_size = 326534 @@ -188,7 +188,7 @@ class VideoTest(BaseTest, unittest.TestCase): self.assertEqual(message.caption, self.caption) def test_video_de_json(self): - video = telegram.Video.de_json(self.json_dict) + video = telegram.Video.de_json(self.json_dict, self._bot) self.assertEqual(video.file_id, self.video_file_id) self.assertEqual(video.width, self.width) @@ -199,12 +199,12 @@ class VideoTest(BaseTest, unittest.TestCase): self.assertEqual(video.file_size, self.file_size) def test_video_to_json(self): - video = telegram.Video.de_json(self.json_dict) + video = telegram.Video.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(video.to_json())) def test_video_to_dict(self): - video = telegram.Video.de_json(self.json_dict) + video = telegram.Video.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(video.to_dict())) self.assertEqual(video['file_id'], self.video_file_id) @@ -253,6 +253,15 @@ class VideoTest(BaseTest, unittest.TestCase): timeout=10, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_video(self): + """Test for Message.reply_video""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_video(self.video_file) + + self.assertNotEqual(message.video.file_id, '') + if __name__ == '__main__': unittest.main() diff --git a/tests/test_voice.py b/tests/test_voice.py index f2e7f624a..db1318f13 100644 --- a/tests/test_voice.py +++ b/tests/test_voice.py @@ -137,7 +137,7 @@ class VoiceTest(BaseTest, unittest.TestCase): self.assertEqual(voice.mime_type, self.mime_type) def test_voice_de_json(self): - voice = telegram.Voice.de_json(self.json_dict) + voice = telegram.Voice.de_json(self.json_dict, self._bot) self.assertEqual(voice.file_id, self.voice_file_id) self.assertEqual(voice.duration, self.duration) @@ -145,12 +145,12 @@ class VoiceTest(BaseTest, unittest.TestCase): self.assertEqual(voice.file_size, self.file_size) def test_voice_to_json(self): - voice = telegram.Voice.de_json(self.json_dict) + voice = telegram.Voice.de_json(self.json_dict, self._bot) self.assertTrue(self.is_json(voice.to_json())) def test_voice_to_dict(self): - voice = telegram.Voice.de_json(self.json_dict) + voice = telegram.Voice.de_json(self.json_dict, self._bot) self.assertTrue(self.is_dict(voice.to_dict())) self.assertEqual(voice['file_id'], self.voice_file_id) @@ -194,6 +194,15 @@ class VoiceTest(BaseTest, unittest.TestCase): TypeError, lambda: self._bot.sendVoice(chat_id=self._chat_id, **json_dict)) + @flaky(3, 1) + @timeout(10) + def test_reply_voice(self): + """Test for Message.reply_voice""" + message = self._bot.sendMessage(self._chat_id, '.') + message = message.reply_voice(self.voice_file) + + self.assertNotEqual(message.voice.file_id, '') + if __name__ == '__main__': unittest.main() From a91fe5f8f6058abdb12eb8ebaf47ad860313bd17 Mon Sep 17 00:00:00 2001 From: Eli Gao Date: Tue, 20 Sep 2016 12:38:49 +0800 Subject: [PATCH 09/19] Properly split and handle arguments in CommandHandler (#414) * Properly split and handle arguments in CommandHandler * Update the docstring for pass_args in CommandHandler * Properly split and handle arguments in StringCommandHandler --- AUTHORS.rst | 1 + telegram/ext/commandhandler.py | 5 +++-- telegram/ext/stringcommandhandler.py | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 30a887e38..9a28e6f4b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -11,6 +11,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Avanatiker `_ - `Balduro `_ - `bimmlerd `_ +- `Eli Gao `_ - `ErgoZ Riftbit Vaper `_ - `franciscod `_ - `Jacob Bom `_ diff --git a/telegram/ext/commandhandler.py b/telegram/ext/commandhandler.py index 345f1a050..1b1e1e74e 100644 --- a/telegram/ext/commandhandler.py +++ b/telegram/ext/commandhandler.py @@ -39,7 +39,8 @@ class CommandHandler(Handler): pass_args (optional[bool]): If the handler should be passed the arguments passed to the command as a keyword argument called ` ``args``. It will contain a list of strings, which is the text - following the command split on spaces. Default is ``False`` + following the command split on single or consecutive whitespace characters. + Default is ``False`` pass_update_queue (optional[bool]): If set to ``True``, a keyword argument called ``update_queue`` will be passed to the callback function. It will be the ``Queue`` instance used by the ``Updater`` and ``Dispatcher`` that contains new updates which can @@ -80,7 +81,7 @@ class CommandHandler(Handler): message = update.message or update.edited_message if self.pass_args: - optional_args['args'] = message.text.split(' ')[1:] + optional_args['args'] = message.text.split()[1:] return self.callback(dispatcher.bot, update, **optional_args) diff --git a/telegram/ext/stringcommandhandler.py b/telegram/ext/stringcommandhandler.py index 9c39cdd6d..eaf37055f 100644 --- a/telegram/ext/stringcommandhandler.py +++ b/telegram/ext/stringcommandhandler.py @@ -35,7 +35,8 @@ class StringCommandHandler(Handler): pass_args (optional[bool]): If the handler should be passed the arguments passed to the command as a keyword argument called ` ``args``. It will contain a list of strings, which is the text - following the command split on spaces. Default is ``False`` + following the command split on single or consecutive whitespace characters. + Default is ``False`` pass_update_queue (optional[bool]): If set to ``True``, a keyword argument called ``update_queue`` will be passed to the callback function. It will be the ``Queue`` instance used by the ``Updater`` and ``Dispatcher`` that contains new updates which can @@ -65,7 +66,7 @@ class StringCommandHandler(Handler): optional_args = self.collect_optional_args(dispatcher) if self.pass_args: - optional_args['args'] = update.split(' ')[1:] + optional_args['args'] = update.split()[1:] return self.callback(dispatcher.bot, update, **optional_args) From 05fb9d161ad0d86914cc783ece6076b13d19fe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 23 Sep 2016 17:13:06 +0200 Subject: [PATCH 10/19] Link echobot2 example from tag v5.0 --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index 13788f0d2..c489eab9f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,7 +4,7 @@ The examples in this folder are small bots meant to show you how a bot that is w All examples are licensed under the [CC0 License](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/LICENSE.txt) and are therefore fully dedicated to the public domain. You can use them as the base for your own bots without worrying about copyrights. -### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py) +### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/v5.0/examples/echobot2.py) This is probably the base for most of the bots made with `python-telegram-bot`. It simply replies to each text message with a message that contains the same text. ### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py) From 93dde1ac1d31289877bd1ba8012d3a9b9972b131 Mon Sep 17 00:00:00 2001 From: Gareth Dwyer Date: Fri, 23 Sep 2016 17:13:32 +0200 Subject: [PATCH 11/19] Add install from source instructions to readme (#419) --- README.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 4fdc17fd7..f5bfb95ca 100644 --- a/README.rst +++ b/README.rst @@ -96,6 +96,13 @@ You can install or upgrade python-telegram-bot with: $ pip install python-telegram-bot --upgrade +Or you can install from source with: + +.. code:: shell + + $ git clone https://github.com/python-telegram-bot/python-telegram-bot + $ cd python-telegram-bot + $ python setup.py install =============== Getting started =============== From e1242b3b4a57cca830ac8574d6c6b3dba44a7186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 23 Sep 2016 17:44:09 +0200 Subject: [PATCH 12/19] message.py: add quote keyword argument to reply_x methods (#420) --- telegram/message.py | 127 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 10 deletions(-) diff --git a/telegram/message.py b/telegram/message.py index 479334e3e..32f8fe45b 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -250,44 +250,151 @@ class Message(TelegramObject): # Python 3 (< 3.3) and Python 2 return int(mktime(dt_obj.timetuple())) + def _quote(self, kwargs): + """Modify kwargs for replying with or without quoting""" + + if 'reply_to_message_id' in kwargs: + if 'quote' in kwargs: + del kwargs['quote'] + + elif 'quote' in kwargs: + if kwargs['quote']: + kwargs['reply_to_message_id'] = self.message_id + + del kwargs['quote'] + + else: + if self.chat.type != Chat.PRIVATE: + kwargs['reply_to_message_id'] = self.message_id + def reply_text(self, *args, **kwargs): - """Shortcut for ``bot.sendMessage(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendMessage(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendMessage(self.chat_id, *args, **kwargs) def reply_photo(self, *args, **kwargs): - """Shortcut for ``bot.sendPhoto(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendPhoto(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the photo is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendPhoto(self.chat_id, *args, **kwargs) def reply_audio(self, *args, **kwargs): - """Shortcut for ``bot.sendAudio(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendAudio(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the audio is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendAudio(self.chat_id, *args, **kwargs) def reply_document(self, *args, **kwargs): - """Shortcut for ``bot.sendDocument(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendDocument(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the document is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendDocument(self.chat_id, *args, **kwargs) def reply_sticker(self, *args, **kwargs): - """Shortcut for ``bot.sendSticker(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendSticker(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the sticker is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendSticker(self.chat_id, *args, **kwargs) def reply_video(self, *args, **kwargs): - """Shortcut for ``bot.sendVideo(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendVideo(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the video is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendVideo(self.chat_id, *args, **kwargs) def reply_voice(self, *args, **kwargs): - """Shortcut for ``bot.sendVoice(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendVoice(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the voice is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendVoice(self.chat_id, *args, **kwargs) def reply_location(self, *args, **kwargs): - """Shortcut for ``bot.sendLocation(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendLocation(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the location is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendLocation(self.chat_id, *args, **kwargs) def reply_venue(self, *args, **kwargs): - """Shortcut for ``bot.sendVenue(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendVenue(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the venue is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendVenue(self.chat_id, *args, **kwargs) def reply_contact(self, *args, **kwargs): - """Shortcut for ``bot.sendContact(update.message.chat_id, *args, **kwargs)``""" + """ + Shortcut for ``bot.sendContact(update.message.chat_id, *args, **kwargs)`` + + Keyword Args: + quote (Optional[bool]): If set to ``True``, the contact is sent as an actual reply to + this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter + will be ignored. Default: ``True`` in group chats and ``False`` in private chats. + """ + + self._quote(kwargs) return self.bot.sendContact(self.chat_id, *args, **kwargs) def forward(self, chat_id, disable_notification=False): From e16c1da6b10944070b4a3ae0ec6a37a3f62a732a Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 24 Sep 2016 13:38:56 +0200 Subject: [PATCH 13/19] Change entities filter to be singular. Also remove the faulty example completely since it should be no longer needed. --- telegram/ext/messagehandler.py | 21 ++++++--------------- tests/test_filters.py | 16 +++++----------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/telegram/ext/messagehandler.py b/telegram/ext/messagehandler.py index 6289d86f8..01483b7a8 100644 --- a/telegram/ext/messagehandler.py +++ b/telegram/ext/messagehandler.py @@ -86,29 +86,19 @@ class Filters(object): return bool(message.forward_date) @staticmethod - def entities(entity_types): + def entity(entity_type): """Filters messages to only allow those which have a :class:`telegram.MessageEntity` - that match `entity_types`. - - Note: - This filter functions as an OR filter, meaning that just one of the entity_type. - If you want AND filtering instead, you have to specify multiple of this filter in the - `filters` attribute. Example: - - >>> MessageHandler([Filters.entities([TEXT_MENTION, MENTION]), - ... Filters.entities([HASHTAG])], callback) - - Will require either a one type of mention AND a hashtag in the message. + where their `type` matches `entity_type`. Args: - entity_types: List of entity types to check for. All types can be found as constants + 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 in entity_types for entity in message.entities]) + return any([entity.type == entity_type for entity in message.entities]) return entities_filter @@ -168,7 +158,8 @@ 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") diff --git a/tests/test_filters.py b/tests/test_filters.py index 9dadac440..47bb95fae 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -155,22 +155,16 @@ class FiltersTest(BaseTest, unittest.TestCase): e = functools.partial(MessageEntity, offset=0, length=0) self.message.entities = [e(MessageEntity.MENTION)] - self.assertTrue(Filters.entities([MessageEntity.MENTION])(self.message)) + self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message)) self.message.entities = [] - self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message)) + self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message)) self.message.entities = [e(MessageEntity.BOLD)] - self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message)) + self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message)) - self.message.entities = [e(MessageEntity.MENTION)] - self.assertTrue( - Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message)) - self.message.entities = [e(MessageEntity.BOLD)] - self.assertTrue( - Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message)) - self.assertFalse( - Filters.entities([MessageEntity.MENTION, MessageEntity.TEXT_MENTION])(self.message)) + self.message.entities = [e(MessageEntity.BOLD), e(MessageEntity.MENTION)] + self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message)) if __name__ == '__main__': From cbe057083f58d4a29d115aceb7b36523290cc807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 24 Sep 2016 14:16:04 +0200 Subject: [PATCH 14/19] fix test_send_photo_resend --- tests/test_photo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_photo.py b/tests/test_photo.py index 1c1da78ec..13ae17d03 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -42,7 +42,7 @@ class PhotoTest(BaseTest, unittest.TestCase): self.thumb = { 'width': 90, 'height': 90, - 'file_id': 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABD64nkFkjujeNXYBAAEC', + 'file_id': 'AgADAQADgEsyGx8j9QeYW9oDz2mKRsKRzy8ABD64nkFkjujeNXYBAAEC', 'file_size': 1478 } self.file_size = 10209 From 8e80a8d273cea1fdef12942905ba889aaa961589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 24 Sep 2016 14:25:31 +0200 Subject: [PATCH 15/19] comment out test_reply_contact --- tests/test_contact.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_contact.py b/tests/test_contact.py index 21292078e..f9f25cfcd 100644 --- a/tests/test_contact.py +++ b/tests/test_contact.py @@ -67,6 +67,8 @@ class ContactTest(BaseTest, unittest.TestCase): self.assertEqual(contact['last_name'], self.last_name) self.assertEqual(contact['user_id'], self.user_id) + +''' Commented out, because it would cause "Too Many Requests (429)" errors. @flaky(3, 1) def test_reply_contact(self): """Test for Message.reply_contact""" @@ -75,7 +77,7 @@ class ContactTest(BaseTest, unittest.TestCase): self.assertEqual(message.contact.phone_number, self.phone_number) self.assertEqual(message.contact.first_name, self.first_name) - +''' if __name__ == '__main__': unittest.main() From c49058dbb48386cf8af3493ccd3a778a9957d682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 24 Sep 2016 15:29:23 +0200 Subject: [PATCH 16/19] Bump version to v5.1 --- CHANGES.rst | 17 +++++++++++++++++ setup.py | 2 +- telegram/version.py | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6d4495dc2..2116b225a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,23 @@ Changes ======= +**2016-09-24** + +*Released 5.1* + +- Drop Python 2.6 support +- Deprecate ``telegram.Emoji`` + +- Use ``ujson`` if available +- Add instance methods to ``Message``, ``Chat``, ``User``, ``InlineQuery`` and ``CallbackQuery`` +- RegEx filtering for ``CallbackQueryHandler`` and ``InlineQueryHandler`` +- New ``MessageHandler`` filters: ``forwarded`` and ``entity`` +- Add ``Message.get_entity`` to correctly handle UTF-16 codepoints and ``MessageEntity`` offsets +- Fix bug in ``ConversationHandler`` when first handler ends the conversation +- Allow multiple ``Dispatcher`` instances +- Add ``ChatMigrated`` Exception +- Properly split and handle arguments in ``CommandHandler`` + **2016-07-15** *Released 5.0* diff --git a/setup.py b/setup.py index 4ace4b9dc..b6c0da28e 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ with codecs.open('README.rst', 'r', 'utf-8') as fd: author='Leandro Toledo', author_email='devs@python-telegram-bot.org', license='LGPLv3', - url='https://github.com/python-telegram-bot/python-telegram-bot', + url='https://python-telegram-bot.org/', keywords='python telegram bot api wrapper', description='Not just a Python wrapper around the Telegram Bot API', long_description=fd.read(), diff --git a/telegram/version.py b/telegram/version.py index 1f7798ad1..7c54e624d 100644 --- a/telegram/version.py +++ b/telegram/version.py @@ -17,4 +17,4 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. -__version__ = '5.0.0' +__version__ = '5.1.0' From be675f0118ce3183b522eb701ffb31cfd58522f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 24 Sep 2016 15:32:22 +0200 Subject: [PATCH 17/19] update all examples to use instance methods (#421) --- examples/conversationbot.py | 37 +++++++++++++++++-------------------- examples/echobot.py | 2 +- examples/inlinebot.py | 6 +++--- examples/inlinekeyboard.py | 4 ++-- examples/timerbot.py | 16 ++++++++-------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/examples/conversationbot.py b/examples/conversationbot.py index 325c9810e..efda7304c 100644 --- a/examples/conversationbot.py +++ b/examples/conversationbot.py @@ -35,11 +35,11 @@ GENDER, PHOTO, LOCATION, BIO = range(4) def start(bot, update): reply_keyboard = [['Boy', 'Girl', 'Other']] - bot.sendMessage(update.message.chat_id, - text='Hi! My name is Professor Bot. I will hold a conversation with you. ' - 'Send /cancel to stop talking to me.\n\n' - 'Are you a boy or a girl?', - reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) + update.message.reply_text( + 'Hi! My name is Professor Bot. I will hold a conversation with you. ' + 'Send /cancel to stop talking to me.\n\n' + 'Are you a boy or a girl?', + reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) return GENDER @@ -47,9 +47,8 @@ def start(bot, update): def gender(bot, update): user = update.message.from_user logger.info("Gender of %s: %s" % (user.first_name, update.message.text)) - bot.sendMessage(update.message.chat_id, - text='I see! Please send me a photo of yourself, ' - 'so I know what you look like, or send /skip if you don\'t want to.') + update.message.reply_text('I see! Please send me a photo of yourself, ' + 'so I know what you look like, or send /skip if you don\'t want to.') return PHOTO @@ -59,8 +58,8 @@ def photo(bot, update): photo_file = bot.getFile(update.message.photo[-1].file_id) photo_file.download('user_photo.jpg') logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg')) - bot.sendMessage(update.message.chat_id, text='Gorgeous! Now, send me your location please, ' - 'or send /skip if you don\'t want to.') + update.message.reply_text('Gorgeous! Now, send me your location please, ' + 'or send /skip if you don\'t want to.') return LOCATION @@ -68,8 +67,8 @@ def photo(bot, update): def skip_photo(bot, update): user = update.message.from_user logger.info("User %s did not send a photo." % user.first_name) - bot.sendMessage(update.message.chat_id, text='I bet you look great! Now, send me your ' - 'location please, or send /skip.') + update.message.reply_text('I bet you look great! Now, send me your location please, ' + 'or send /skip.') return LOCATION @@ -79,8 +78,8 @@ def location(bot, update): user_location = update.message.location logger.info("Location of %s: %f / %f" % (user.first_name, user_location.latitude, user_location.longitude)) - bot.sendMessage(update.message.chat_id, text='Maybe I can visit you sometime! ' - 'At last, tell me something about yourself.') + update.message.reply_text('Maybe I can visit you sometime! ' + 'At last, tell me something about yourself.') return BIO @@ -88,8 +87,8 @@ def location(bot, update): def skip_location(bot, update): user = update.message.from_user logger.info("User %s did not send a location." % user.first_name) - bot.sendMessage(update.message.chat_id, text='You seem a bit paranoid! ' - 'At last, tell me something about yourself.') + update.message.reply_text('You seem a bit paranoid! ' + 'At last, tell me something about yourself.') return BIO @@ -97,8 +96,7 @@ def skip_location(bot, update): def bio(bot, update): user = update.message.from_user logger.info("Bio of %s: %s" % (user.first_name, update.message.text)) - bot.sendMessage(update.message.chat_id, - text='Thank you! I hope we can talk again some day.') + update.message.reply_text('Thank you! I hope we can talk again some day.') return ConversationHandler.END @@ -106,8 +104,7 @@ def bio(bot, update): def cancel(bot, update): user = update.message.from_user logger.info("User %s canceled the conversation." % user.first_name) - bot.sendMessage(update.message.chat_id, - text='Bye! I hope we can talk again some day.') + update.message.reply_text('Bye! I hope we can talk again some day.') return ConversationHandler.END diff --git a/examples/echobot.py b/examples/echobot.py index 06ae496e6..afe532060 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -46,7 +46,7 @@ def echo(bot): if update.message: # your bot can receive updates without messages # Reply to the message - bot.sendMessage(chat_id=chat_id, text=update.message.text) + update.message.reply_text(update.message.text) if __name__ == '__main__': diff --git a/examples/inlinebot.py b/examples/inlinebot.py index 44462b9a8..2db044d10 100644 --- a/examples/inlinebot.py +++ b/examples/inlinebot.py @@ -34,11 +34,11 @@ logger = logging.getLogger(__name__) # Define a few command handlers. These usually take the two arguments bot and # update. Error handlers also receive the raised TelegramError object in error. def start(bot, update): - bot.sendMessage(update.message.chat_id, text='Hi!') + update.message.reply_text('Hi!') def help(bot, update): - bot.sendMessage(update.message.chat_id, text='Help!') + update.message.reply_text('Help!') def escape_markdown(text): @@ -68,7 +68,7 @@ def inlinequery(bot, update): "_%s_" % escape_markdown(query), parse_mode=ParseMode.MARKDOWN))) - bot.answerInlineQuery(update.inline_query.id, results=results) + update.inline_query.answer(results) def error(bot, update, error): diff --git a/examples/inlinekeyboard.py b/examples/inlinekeyboard.py index 2c96819ac..326581a9f 100644 --- a/examples/inlinekeyboard.py +++ b/examples/inlinekeyboard.py @@ -20,7 +20,7 @@ def start(bot, update): reply_markup = InlineKeyboardMarkup(keyboard) - bot.sendMessage(update.message.chat_id, text="Please choose:", reply_markup=reply_markup) + update.message.reply_text('Please choose:', reply_markup=reply_markup) def button(bot, update): @@ -32,7 +32,7 @@ def button(bot, update): def help(bot, update): - bot.sendMessage(update.message.chat_id, text="Use /start to test this bot.") + update.message.reply_text("Use /start to test this bot.") def error(bot, update, error): diff --git a/examples/timerbot.py b/examples/timerbot.py index 7bd4d3023..d712b449b 100644 --- a/examples/timerbot.py +++ b/examples/timerbot.py @@ -31,7 +31,7 @@ timers = dict() # Define a few command handlers. These usually take the two arguments bot and # update. Error handlers also receive the raised TelegramError object in error. def start(bot, update): - bot.sendMessage(update.message.chat_id, text='Hi! Use /set to ' 'set a timer') + update.message.reply_text('Hi! Use /set to set a timer') def alarm(bot, job): @@ -46,7 +46,7 @@ def set(bot, update, args, job_queue): # args[0] should contain the time for the timer in seconds due = int(args[0]) if due < 0: - bot.sendMessage(chat_id, text='Sorry we can not go back to future!') + update.message.reply_text('Sorry we can not go back to future!') return # Add job to queue @@ -54,25 +54,25 @@ def set(bot, update, args, job_queue): timers[chat_id] = job job_queue.put(job) - bot.sendMessage(chat_id, text='Timer successfully set!') + update.message.reply_text('Timer successfully set!') except (IndexError, ValueError): - bot.sendMessage(chat_id, text='Usage: /set ') + update.message.reply_text('Usage: /set ') -def unset(bot, update, job_queue): +def unset(bot, update): """Removes the job if the user changed their mind""" chat_id = update.message.chat_id if chat_id not in timers: - bot.sendMessage(chat_id, text='You have no active timer') + update.message.reply_text('You have no active timer') return job = timers[chat_id] job.schedule_removal() del timers[chat_id] - bot.sendMessage(chat_id, text='Timer successfully unset!') + update.message.reply_text('Timer successfully unset!') def error(bot, update, error): @@ -89,7 +89,7 @@ def main(): dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", start)) dp.add_handler(CommandHandler("set", set, pass_args=True, pass_job_queue=True)) - dp.add_handler(CommandHandler("unset", unset, pass_job_queue=True)) + dp.add_handler(CommandHandler("unset", unset)) # log all errors dp.add_error_handler(error) From dc7a45951137ee5b8bc27b82a1e219562b95505c Mon Sep 17 00:00:00 2001 From: Rahiel Kasim Date: Sat, 24 Sep 2016 15:46:02 +0200 Subject: [PATCH 18/19] README: formatting (adding a newline) --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index f5bfb95ca..5b5aa93d4 100644 --- a/README.rst +++ b/README.rst @@ -103,6 +103,7 @@ Or you can install from source with: $ git clone https://github.com/python-telegram-bot/python-telegram-bot $ cd python-telegram-bot $ python setup.py install + =============== Getting started =============== From 9d0e0386d9d3ccfdeabc108032815c41f797016e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 24 Sep 2016 16:30:39 +0200 Subject: [PATCH 19/19] Link echobot2 example from master --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index c489eab9f..13788f0d2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,7 +4,7 @@ The examples in this folder are small bots meant to show you how a bot that is w All examples are licensed under the [CC0 License](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/LICENSE.txt) and are therefore fully dedicated to the public domain. You can use them as the base for your own bots without worrying about copyrights. -### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/v5.0/examples/echobot2.py) +### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py) This is probably the base for most of the bots made with `python-telegram-bot`. It simply replies to each text message with a message that contains the same text. ### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py)