Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ Setting things up

4. Install dependencies:

``$ pip install -r requirements.txt``

``$ pip install -r requirements-dev.txt``
``$ pip install -r requirements.txt -r requirements-dev.txt``

Finding something to do
-----------------------
Expand Down Expand Up @@ -56,6 +54,8 @@ Here's how to make a one-off code change.

- You can refer to relevant issues in the commit message by writing, e.g., "#105".

- Your code should adhere to the `PEP 8 Style Guide`_, with the exception that we have a maximum line length of 99.

- For consistency, please conform to `Google Python Style Guide`_ and `Google Python Style Docstrings`_. In addition, code should be formatted consistently with other code around it.

- The following exceptions to the above (Google's) style guides applies:
Expand Down Expand Up @@ -116,7 +116,7 @@ Here's how to make a one-off code change.

- At the end, the reviewer will merge the pull request.

6. **Tidy up!** Delete the feature branch from your both your local clone and the GitHub repository:
6. **Tidy up!** Delete the feature branch from both your local clone and the GitHub repository:

``$ git branch -D your-branch-name``

Expand All @@ -127,6 +127,7 @@ Here's how to make a one-off code change.
.. _`Code of Conduct`: https://www.python.org/psf/codeofconduct/
.. _`issue tracker`: https://github.com/python-telegram-bot/python-telegram-bot/issues
.. _`developers' mailing list`: mailto:devs@python-telegram-bot.org
.. _`PEP 8 Style Guide`: https://www.python.org/dev/peps/pep-0008/
.. _`Google Python Style Guide`: https://google-styleguide.googlecode.com/svn/trunk/pyguide.html
.. _`Google Python Style Docstrings`: http://sphinx-doc.org/latest/ext/example_google.html
.. _AUTHORS.rst: https://github.com/python-telegram-bot/python-telegram-bot/blob/master/AUTHORS.rst
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ _`API`

Note: Using the ``Bot`` class directly is the 'old' method, we have an easier way to make bots described in the next section. All of this is however still important information, even if you're using the ``telegram.ext`` submodule!

The API is exposed via the ``telegram.Bot`` class.
The API is exposed via the ``telegram.Bot`` class. The methods have names as described in the official `Telegram Bot API <https://core.telegram.org/bots/api>`_, but equivalent snake_case methods are available for `PEP8 <https://www.python.org/dev/peps/pep-0008/>`_ enthusiasts. So for example `telegram.Bot.send_message` is the same as `telegram.Bot.sendMessage`.

To generate an Access Token you have to talk to `BotFather <https://telegram.me/botfather>`_ and follow a few simple steps (described `here <https://core.telegram.org/bots#6-botfather>`_).

Expand Down Expand Up @@ -355,7 +355,7 @@ We want this function to be called on a Telegram message that contains the ``/st

>>> from telegram.ext import CommandHandler
>>> start_handler = CommandHandler('start', start)
>>> dispatcher.addHandler(start_handler)
>>> dispatcher.add_handler(start_handler)

The last step is to tell the ``Updater`` to start working:

Expand All @@ -372,7 +372,7 @@ Our bot is now up and running (go ahead and try it)! It's not doing anything yet
...
>>> from telegram.ext import MessageHandler, Filters
>>> echo_handler = MessageHandler([Filters.text], echo)
>>> dispatcher.addHandler(echo_handler)
>>> dispatcher.add_handler(echo_handler)

Our bot should now reply to all text messages that are not a command with a message that has the same content.

Expand All @@ -385,7 +385,7 @@ Let's add some functionality to our bot. We want to add the ``/caps`` command, t
... bot.sendMessage(chat_id=update.message.chat_id, text=text_caps)
...
>>> caps_handler = CommandHandler('caps', caps, pass_args=True)
>>> dispatcher.addHandler(caps_handler)
>>> dispatcher.add_handler(caps_handler)

To enable our bot to respond to inline queries, we can add the following (you will also have to talk to BotFather):

Expand All @@ -400,7 +400,7 @@ To enable our bot to respond to inline queries, we can add the following (you wi
...
>>> from telegram.ext import InlineQueryHandler
>>> inline_caps_handler = InlineQueryHandler(inline_caps)
>>> dispatcher.addHandler(inline_caps_handler)
>>> dispatcher.add_handler(inline_caps_handler)

People might try to send commands to the bot that it doesn't understand, so we can use a ``RegexHandler`` to recognize all commands that were not recognized by the previous handlers. **Note:** This handler has to be added last, else it will be triggered before the ``CommandHandlers`` had a chance to look at the update:

Expand All @@ -411,7 +411,7 @@ People might try to send commands to the bot that it doesn't understand, so we c
...
>>> from telegram.ext import RegexHandler
>>> unknown_handler = RegexHandler(r'/.*', unknown)
>>> dispatcher.addHandler(unknown_handler)
>>> dispatcher.add_handler(unknown_handler)

If you're done playing around, stop the bot with this:

Expand Down
23 changes: 11 additions & 12 deletions examples/clibot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from time import sleep
import logging

from future.builtins import input

# Enable Logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
Expand Down Expand Up @@ -112,24 +114,24 @@ def main():
dp = updater.dispatcher

# This is how we add handlers for Telegram messages
dp.addHandler(CommandHandler("start", start))
dp.addHandler(CommandHandler("help", help))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# Message handlers only receive updates that don't contain commands
dp.addHandler(MessageHandler([Filters.text], message))
dp.add_handler(MessageHandler([Filters.text], message))
# Regex handlers will receive all updates on which their regex matches,
# but we have to add it in a separate group, since in one group,
# only one handler will be executed
dp.addHandler(RegexHandler('.*', any_message), group=1)
dp.add_handler(RegexHandler('.*', any_message), group=1)

# String handlers work pretty much the same. Note that we have to tell
# the handler to pass the args or update_queue parameter
dp.addHandler(StringCommandHandler('reply', cli_reply, pass_args=True))
dp.addHandler(StringRegexHandler('[^/].*', cli_noncommand,
pass_update_queue=True))
dp.add_handler(StringCommandHandler('reply', cli_reply, pass_args=True))
dp.add_handler(StringRegexHandler('[^/].*', cli_noncommand,
pass_update_queue=True))

# All TelegramErrors are caught for you and delivered to the error
# handler(s). Other types of Errors are not caught.
dp.addErrorHandler(error)
dp.add_error_handler(error)

# Start the Bot and store the update Queue, so we can insert updates
update_queue = updater.start_polling(timeout=10)
Expand All @@ -153,10 +155,7 @@ def main():

# Start CLI-Loop
while True:
try:
text = raw_input()
except NameError:
text = input()
text = input()

# Gracefully stop the event handler
if text == 'stop':
Expand Down
8 changes: 4 additions & 4 deletions examples/echobot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def main():
dp = updater.dispatcher

# on different commands - answer in Telegram
dp.addHandler(CommandHandler("start", start))
dp.addHandler(CommandHandler("help", help))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))

# on noncommand i.e message - echo the message on Telegram
dp.addHandler(MessageHandler([Filters.text], echo))
dp.add_handler(MessageHandler([Filters.text], echo))

# log all errors
dp.addErrorHandler(error)
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()
Expand Down
8 changes: 4 additions & 4 deletions examples/inlinebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def main():
dp = updater.dispatcher

# on different commands - answer in Telegram
dp.addHandler(CommandHandler("start", start))
dp.addHandler(CommandHandler("help", help))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))

# on noncommand i.e message - echo the message on Telegram
dp.addHandler(InlineQueryHandler(inlinequery))
dp.add_handler(InlineQueryHandler(inlinequery))

# log all errors
dp.addErrorHandler(error)
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()
Expand Down
10 changes: 5 additions & 5 deletions examples/inlinekeyboard_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def error(bot, update, error):
# The command
updater.dispatcher.addHandler(CommandHandler('set', set_value))
# The answer
updater.dispatcher.addHandler(MessageHandler([Filters.text], entered_value))
updater.dispatcher.add_handler(MessageHandler([Filters.text], entered_value))
# The confirmation
updater.dispatcher.addHandler(CallbackQueryHandler(confirm_value))
updater.dispatcher.addHandler(CommandHandler('start', help))
updater.dispatcher.addHandler(CommandHandler('help', help))
updater.dispatcher.addErrorHandler(error)
updater.dispatcher.add_handler(CallbackQueryHandler(confirm_value))
updater.dispatcher.add_handler(CommandHandler('start', help))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_error_handler(error)

# Start the Bot
updater.start_polling()
Expand Down
10 changes: 5 additions & 5 deletions examples/state_machine_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def help(bot, update):
updater = Updater("TOKEN")

# The command
updater.dispatcher.addHandler(CommandHandler('set', set_value))
updater.dispatcher.add_handler(CommandHandler('set', set_value))
# The answer and confirmation
updater.dispatcher.addHandler(MessageHandler([Filters.text], set_value))
updater.dispatcher.addHandler(CommandHandler('cancel', cancel))
updater.dispatcher.addHandler(CommandHandler('start', help))
updater.dispatcher.addHandler(CommandHandler('help', help))
updater.dispatcher.add_handler(MessageHandler([Filters.text], set_value))
updater.dispatcher.add_handler(CommandHandler('cancel', cancel))
updater.dispatcher.add_handler(CommandHandler('start', help))
updater.dispatcher.add_handler(CommandHandler('help', help))

# Start the Bot
updater.start_polling()
Expand Down
8 changes: 4 additions & 4 deletions examples/timerbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def main():
dp = updater.dispatcher

# on different commands - answer in Telegram
dp.addHandler(CommandHandler("start", start))
dp.addHandler(CommandHandler("help", start))
dp.addHandler(CommandHandler("set", set, pass_args=True))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", start))
dp.add_handler(CommandHandler("set", set, pass_args=True))

# log all errors
dp.addErrorHandler(error)
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ all_files = 1
upload-dir = docs/build/html

[flake8]
max-line-length = 99
max-line-length = 99
8 changes: 8 additions & 0 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

"""A library that provides a Python interface to the Telegram Bot API"""

from sys import version_info

from .base import TelegramObject
from .user import User
from .chat import Chat
Expand Down Expand Up @@ -142,3 +144,9 @@
'Venue',
'Video',
'Voice']


if version_info < (2, 7):
from warnings import warn
warn("python-telegram-bot will stop supporting Python 2.6 in a future release. "
"Please upgrade your Python!")
26 changes: 26 additions & 0 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,29 @@ def __reduce__(self):
return (self.__class__, (self.token,
self.base_url.replace(self.token, ''),
self.base_file_url.replace(self.token, '')))

# snake_case (PEP8) aliases
get_me = getMe
send_message = sendMessage
forward_message = forwardMessage
send_photo = sendPhoto
send_audio = sendAudio
send_document = sendDocument
send_sticker = sendSticker
send_video = sendVideo
send_voice = sendVoice
send_location = sendLocation
send_venue = sendVenue
send_contact = sendContact
send_chat_action = sendChatAction
answer_inline_query = answerInlineQuery
get_user_profile_photos = getUserProfilePhotos
get_file = getFile
kick_chat_member = kickChatMember
unban_chat_member = unbanChatMember
answer_callback_query = answerCallbackQuery
edit_message_text = editMessageText
edit_message_caption = editMessageCaption
edit_message_reply_markup = editMessageReplyMarkup
get_updates = getUpdates
set_webhook = setWebhook
14 changes: 10 additions & 4 deletions telegram/ext/callbackqueryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate


class CallbackQueryHandler(Handler):
Expand All @@ -29,7 +30,7 @@ class CallbackQueryHandler(Handler):

Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
Expand All @@ -39,10 +40,15 @@ class CallbackQueryHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(CallbackQueryHandler, self).__init__(callback, pass_update_queue)

def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, Update) and update.callback_query

def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)

self.callback(dispatcher.bot, update, **optional_args)

# old non-PEP8 Handler methods
m = "telegram.CallbackQueryHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")
14 changes: 10 additions & 4 deletions telegram/ext/choseninlineresulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate


class ChosenInlineResultHandler(Handler):
Expand All @@ -30,7 +31,7 @@ class ChosenInlineResultHandler(Handler):

Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
Expand All @@ -41,10 +42,15 @@ def __init__(self, callback, pass_update_queue=False):
super(ChosenInlineResultHandler, self).__init__(callback,
pass_update_queue)

def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, Update) and update.chosen_inline_result

def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)

self.callback(dispatcher.bot, update, **optional_args)

# old non-PEP8 Handler methods
m = "telegram.ChosenInlineResultHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")
Loading