diff --git a/CHANGES.rst b/CHANGES.rst index e49114f0e..a29964a96 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +2015-09-05 + Released 2.8.2 + Fix regression on Telegram ReplyMarkup + Add certificate to is_inputfile method + + 2015-09-05 Released 2.8.1 Fix regression on Telegram objects with thumb properties diff --git a/README.rst b/README.rst index 75af2a814..8321d93b8 100644 --- a/README.rst +++ b/README.rst @@ -268,9 +268,9 @@ You may copy, distribute and modify the software provided that modifications are _`Contact` ========== -Feel free to join to our `Telegram group `_. +Feel free to join to our `Telegram group `_. -*If you face trouble joining in the group please ping me on Telegram (@leandrotoledo), I'll be glad to add you.* +If you face trouble joining in the group please ping me `via Telegram `_, I'll be glad to add you. ======= _`TODO` diff --git a/docs/source/conf.py b/docs/source/conf.py index 5653bd482..c95c511b5 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -60,7 +60,7 @@ author = u'Leandro Toledo' # The short X.Y version. version = '2.8' # The full version, including alpha/beta/rc tags. -release = '2.8.1' +release = '2.8.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/examples/command_handler_example.py b/examples/command_handler_example.py deleted file mode 100644 index f3e70ab21..000000000 --- a/examples/command_handler_example.py +++ /dev/null @@ -1,87 +0,0 @@ -from telegram import CommandHandlerWithHelpAndFather, CommandHandler -class ExampleCommandHandler(CommandHandlerWithHelpAndFather): - """This is an example how to use a CommandHandlerWithHelp or just a CommandHandler. - - If You want to use a CommandHandler it is very easy. - create a class which inherits a CommandHandler. - create a method in this class which start with 'command_' and takes 1 argument: 'update' (which comes directly from - getUpdate()). - If you inherit CommandHandlerWithHelp it also creates a nice /help for you. - """ - def __init__(self, bot): # only necessary for a WithHelp - super(ExampleCommandHandler, self).__init__(bot) - self._help_title = 'Welcome this is a help file!' # optional - self._help_before_list = """ - Yeah here I explain some things about this bot. - and of course I can do this in Multiple lines. - """ # default is empty - self._help_list_title = ' These are the available commands:' # optional - self._help_after_list = ' These are some footnotes' # default is empty - self.is_reply = True # default is True - - # only necessary if you want to override to default - def _command_not_found(self, update): - """Inform the telegram user that the command was not found.""" - chat_id = update.message.chat.id - reply_to = update.message.message_id - message = "Sorry, I don't know how to do {command}.".format(command=update.message.text.split(' ')[0]) - self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) - - # creates /test command. This code gets called when a telegram user enters /test - def command_test(self, update): - """ Test if the server is online. """ - chat_id = update.message.chat.id - reply_to = update.message.message_id - message = 'Yeah, the server is online!' - self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) - - # creates /parrot command - def command_parrot(self, update): - """ Says back what you say after the command""" - chat_id = update.message.chat.id - reply_to = update.message.message_id - send = update.message.text.split(' ') - message = update.message.text[len(send[0]):] - if len(send) == 1: - message = '...' - self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) - - # creates /p command - def command_p(self, update): - """Does the same as parrot.""" - return self.command_parrot(update) - - # this doesn't create a command. - def another_test(self, update): - """ This won't be called by the CommandHandler. - - This is an example of a function that isn't a command in telegram. - Because it didn't start with 'command_'. - """ - chat_id = update.message.chat.id - reply_to = update.message.message_id - message = 'Yeah, this is another test' - self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) - - -class Exampe2CommandHandler(CommandHandler): - """ - This is an example of a small working CommandHandler with only one command. - """ - def command_test(self, update): - """ Test if the server is online. """ - chat_id = update.message.chat.id - reply_to = update.message.message_id - message = 'Yeah, the server is online!' - self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) - -if __name__ == '__main__': - import telegram as telegram - try: - from mytoken import token - except: - token = '' # use your own token here - print ('token = ', token) - Bot = telegram.Bot(token=token) - test_command_handler = ExampleCommandHandler(Bot) - test_command_handler.run() diff --git a/setup.py b/setup.py index 8d29159f7..f5b95d4ad 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ def read(*paths): setup( name='python-telegram-bot', - version='2.8.1', + version='2.8.2', author='Leandro Toledo', author_email='leandrotoledodesouza@gmail.com', license='LGPLv3', diff --git a/telegram/__init__.py b/telegram/__init__.py index 7bc5a8bf7..751b55fdd 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -19,7 +19,7 @@ """A library that provides a Python interface to the Telegram Bot API""" __author__ = 'leandrotoledodesouza@gmail.com' -__version__ = '2.8.1' +__version__ = '2.8.2' from .base import TelegramObject from .user import User @@ -46,12 +46,10 @@ from .parsemode import ParseMode from .message import Message from .update import Update from .bot import Bot -from .command_handler import * __all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction', 'Location', 'Contact', 'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat', - 'Update', 'ParseMode', 'Message', 'User', 'TelegramObject', 'NullHandler', - 'Voice', 'CommandHandler', 'CommandHandlerWithHelp', - 'CommandHandlerWithFatherCommand', 'CommandHandlerWithHelpAndFather'] + 'Update', 'ParseMode', 'Message', 'User', 'TelegramObject', + 'NullHandler', 'Voice'] diff --git a/telegram/enchancedbot.py b/telegram/enchancedbot.py deleted file mode 100644 index 79606ca2c..000000000 --- a/telegram/enchancedbot.py +++ /dev/null @@ -1,83 +0,0 @@ -import telegram - - -class NoSuchCommandException(BaseException): - pass - -class CommandDispatcher: - def __init__(self,): - self.commands = list() - self.default = None - - def addCommand(self, command, callback): - self.commands.append((command, callback)) - - def setDefault(self, callback): - self.default = callback - - def dispatch(self, update): - if hasattr(update.message, 'text'): - text = update.message.text - else: - text = '' - - user_id = update.message.from_user.id - com = text.split('@')[0] - for command, callback in self.commands: - if com == command: - return callback(command, user_id) - if self.default is not None: - return self.default(text, user_id) - else: - raise NoSuchCommandException() - - -class EnhancedBot(telegram.Bot): - """The Bot class with command dispatcher added. - - >>> bot = EnhancedBot(token=TOKEN) - >>> @bot.command('/start') - ... def start(command, user_id): - ... # should return a tuple: (text, reply_id, custom_keyboard) - ... return ("Hello, there! Your id is {}".format(user_id), None, None) - >>> while True: - ... bot.processUpdates() - ... time.sleep(3) - """ - def __init__(self, token): - self.dispatcher = CommandDispatcher() - telegram.Bot.__init__(self, token=token) - self.offset = 0 #id of the last processed update - - def command(self, *names, default=False): - """Decorator for adding callbacks for commands.""" - - def inner_command(callback): - for name in names: - self.dispatcher.addCommand(name, callback) - if default: - self.dispatcher.setDefault(callback) - return callback # doesn't touch the callback, so we can use it - return inner_command - - def processUpdates(self): - updates = self.getUpdates(offset=self.offset) - - for update in updates: - print('processing update: {}'.format(str(update.to_dict()))) - self.offset = update.update_id + 1 - if not hasattr(update, 'message'): - continue - - try: - answer, reply_to, reply_markup = self.dispatcher.dispatch(update) - except Exception as e: - print('error occured') # TODO logging - print(update.to_dict()) - raise e - - if answer is not None: - self.sendMessage(chat_id=update.message.chat_id, - text=answer, - reply_to_message_id=reply_to, - reply_markup=reply_markup) diff --git a/telegram/parsemode.py b/telegram/parsemode.py index 897693acd..12f17d4ff 100644 --- a/telegram/parsemode.py +++ b/telegram/parsemode.py @@ -17,10 +17,10 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. -"""This module contains a object that represents a Telegram Message Parse Modes""" +"""This module contains a object that represents a Telegram Parse Modes""" class ParseMode(object): - """This object represents a Telegram Message Parse Modes.""" + """This object represents a Telegram Parse Modes.""" MARKDOWN = 'Markdown' diff --git a/telegram/utils/botan.py b/telegram/utils/botan.py deleted file mode 100644 index ff2b6f82c..000000000 --- a/telegram/utils/botan.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python -# -# A library that provides a Python interface to the Telegram Bot API -# Copyright (C) 2015 Leandro Toledo de Souza -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser Public License for more details. -# -# You should have received a copy of the GNU Lesser Public License -# along with this program. If not, see [http://www.gnu.org/licenses/]. - -import json -try: - from urllib.parse import urlencode - from urllib.request import urlopen, Request - from urllib.error import HTTPError, URLError -except ImportError: - from urllib import urlencode - from urllib2 import urlopen, Request - from urllib2 import HTTPError, URLError - -DEFAULT_BASE_URL = \ - 'https://api.botan.io/track?token=%(token)&uid=%(uid)&name=%(name)' -USER_AGENT = 'Python Telegram Bot' \ - ' (https://github.com/leandrotoledo/python-telegram-bot)' -CONTENT_TYPE = 'application/json' - -class Botan(Object): - def __init__(self, - token, - base_url=None): - self.token = token - - if base_url is None: - self.base_url = DEFAULT_BASE_URL % {'token': self.token} - else: - self.base_url = base_url % {'token': self.token} - - def track(self, - uid, - text, - name = 'Message'): - - url = self.base_url % {'uid': uid, - 'message': text, - 'name': name} - - self._post(url, message) - - def _post(self, - url, - data): - headers = {'User-agent': USER_AGENT, - 'Content-type': CONTENT_TYPE} - - try: - request = Request( - url, - data=urlencode(data).encode(), - headers=headers - ) - - return urlopen(request).read() - except IOError as e: - raise TelegramError(str(e)) - except HTTPError as e: - raise TelegramError(str(e)) diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 54aabac96..5b83a9f59 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -22,13 +22,11 @@ import json try: - from urllib.parse import urlencode from urllib.request import urlopen, Request - from urllib.error import HTTPError, URLError + from urllib.error import HTTPError except ImportError: - from urllib import urlencode from urllib2 import urlopen, Request - from urllib2 import HTTPError, URLError + from urllib2 import HTTPError from telegram import (InputFile, TelegramError) diff --git a/tests/legacy/test_command_handler.py b/tests/legacy/test_command_handler.py deleted file mode 100644 index 171b24ef4..000000000 --- a/tests/legacy/test_command_handler.py +++ /dev/null @@ -1,75 +0,0 @@ -import sys -sys.path.append('.') -import telegram -import unittest -import unittest.mock -from telegram.command_handler import CommandHandler, CommandHandlerWithHelp - - -class CommandHandlerTmp(CommandHandler): - def __init__(self, *args, **kwargs): - super(CommandHandlerTmp, self).__init__(*args, **kwargs) - self.output = None - - def command_test(self, update): - self.output = 1 - - -class CommandHandlerTmp2(CommandHandlerWithHelp): - def __init__(self, *args, **kwargs): - super(CommandHandlerTmp2, self).__init__(*args, **kwargs) - self.output_test = None - - def command_test(self, update): - self.output_test = 1 - - -def fake_getUpdates(*args, **kwargs): - from_user = telegram.User(id=42, first_name='hello') - message = telegram.Message(message_id=42, from_user=from_user, date=None, chat=from_user, text='/test') - update = telegram.Update(update_id=42, message=message) - return [update] - -output_fsm = None - - -def fake_sendMessage(chat_id, message, *args, **kwargs): - global output_fsm - output_fsm = (chat_id, message) - return telegram.Message(43, 123, 000000, telegram.User(chat_id, 'test'), text=message) - - -class CommandHandlerTest(unittest.TestCase): - def setUp(self): - self.bot = unittest.mock.MagicMock() - self.bot.getUpdates = fake_getUpdates - self.bot.sendMessage = fake_sendMessage - - def test_get_command_func(self): - CH = CommandHandlerTmp(self.bot) - self.assertEqual(CH.command_test, CH._get_command_func('test')) - self.assertEqual(CH.command_test, CH._get_command_func('/test')) - self.assertEqual(None, CH._get_command_func('this function does not exsist')) - - def test_run_once(self): - CH = CommandHandlerTmp(self.bot) - self.assertEqual(CH.output, None) - threads, last_update = CH.run_once(make_thread=True) - for t in threads: - t.start() - for t in threads: - t.join() - self.assertEqual(CH.output, 1) - - def test_run(self): - pass # TODO implement test - - def test__command_not_found(self): - CH = CommandHandlerTmp(self.bot) - CH._command_not_found(self.bot.getUpdates()[0]) - self.assertEqual(output_fsm, (42, "Sorry, I didn't understand the command: /test.")) - - -if __name__ == '__main__': - import sys - unittest.main(sys.argv)