Skip to content

Introduction to the API

Jasmin Bom edited this page May 2, 2019 · 17 revisions

The API is exposed via the telegram.Bot class. The methods are the snake_case equivalents of the methods described in the official Telegram Bot API. The exact camelCase method names as in the Telegram docs are also available for your convenience. 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 and follow a few simple steps (described here).

For full details see the official Telegram documentation at Bots: An introduction for developers.

Hello, Telegram!

To get a feeling for the API and how to use it with python-telegram-bot, please open a Python command line and follow the next few steps.

First, create an instance of the telegram.Bot. 'TOKEN' should be replaced by the API token you received from @BotFather:

>>> import telegram
>>> bot = telegram.Bot(token='TOKEN')

To check if your credentials are correct, call the getMe API method:

>>> print(bot.get_me())
{"first_name": "Toledo's Palace Bot", "username": "ToledosPalaceBot"}

Note: Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use<bot_username> links or username search to find your bot.

What to read next?

To get real and start building your first bot using the telegram.ext classes, read Extensions – Your first Bot

If you want to continue learning about the API, read Code snippets.

You can’t perform that action at this time.