Let's get started!
In this tutorial you'll get a Telegram Bot up and running on your local enviornment. Later you'll also learn how to deploy it to your OpenShift server.
We'll use the Python library provided by Telegram called python-telegram-bot.
To begin, you'll need a Telegram Access Token for your Bot.
-
Create or sign in to your Telegram Account
-
Search for
@BotFather
-
Initialize a conversation with the
@BotFather
-
Create a new bot by typing
/newbot
and follow the guided process.Congratulations, you've created your Telegram Bot :)
-
Generate an access token which you'll need to communicate with your bot by typing
/token
-
Replace your Telegram access token in
telegram.py
byYOUR TOKEN
-
Fire up a command line and install the
python-telegram-bot
library through your command line by running:pip install python-telegram-bot
-
To start your bot run
python bot.py
(stop your bot by running CTRL+Z) -
Your Bot supports the following inputs:
Coming soon …
Add @Hello_Telegram_Bot
and send him a message to test the features in this tutorial.
Find more infos and documentation about python-telegram-bot
at https://github.com/python-telegram-bot/
To send 🔊 audio, 🖼 photos, etc check out the Telegram Bot API 🙂
Get inspired by more Examples here:
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.
This bot uses the JobQueue
class to send timed messages. The user sets a timer by using /set
command with a specific time, for example /set 30
. The bot then sets up a job to send a message to that user after 30 seconds. The user can also cancel the timer by sending /unset
. To learn more about the JobQueue
, read this wiki article.
A common task for a bot is to ask information from the user. In v5.0 of this library, we introduced the ConversationHandler
for that exact purpose. This example uses it to retrieve user-information in a conversation-like style.
This example sheds some light on inline keyboards, callback queries and message editing.
A basic example of an inline bot. Don't forget to enable inline mode with @BotFather.