How to create a bot telegram using Python: a practical guide - #1

in #python6 years ago

This post is the beginning of my gradual study of Python.

The documentation describing the process of interaction with the Telegram bots is here

For creat and work in next time we use python lib - telepot

In order to serve users of our bot, we will develop a web application using the Django framework.

How to create a bot telegram?

First, we need to register our future bot in Telegram. This is done as follows:

  • You must install the Telegram application on your phone or computer. Download the application here
  • We add to our contact list a bot named BotFather
  • We start the procedure of "communication" with the bot by pressing the Start button. Next in front of us will appear a list of commands exactly as in the screenshot.
  • In order to create a new bot, you must run the / newbot command and follow the instructions. Note that the username for the bot should always contain the word bot in the end. For example, DjangoBot or Django_bot.









In addition to the botgram creation function, BotFather also has a number of other features:

  • Assign bot description
  • Set Avatar
  • Change token
    and so on. A full description of available commands can be seen on the chat.

Let's start encoding

As I mentioned earlier, we will write a web application on Django. But it is worth noting that this is not necessary. You can get by with the usual Python script, though in this case it will be necessary to periodically poll Telegram for new requests from bot users (using the getUpdates method) and incrementing the offset to get the most recent data without repetition. In Telegram, there are two mutually exclusive methods of getting commands / messages for your bot.

  • Using the getUpdates API call
  • Installing Webhook

Installing Webhook is to send the bot a special URL address to which the POST request will be sent each time someone starts sending messages to the bot. This is the version we will use for the interaction between the bot and its user. In order to specify a URL, you must use the setWebhook API method. I note that the URL should start with https, that is, have an SSL-secured connection with a valid certificate. Telegram allows you to use a self-signed certificate, although for this you need to transfer the public key in PEM format (ASCII base64) in the setWebhook method. Or you can get a valid free SSL certificate from Let's Encrypt.

More information about getUpdates and setWebhook can be read accordingly here and here.

So, back to the python library for working with Telegram - telepot. Install it in a virtual environment python virtualenv:

pip install telepot

The easiest way to interact with the Telegram bot in Python is as follows:

import telepot
token = 'YOUR_token'
TelegramBot = telepot.Bot(token)
print(TelegramBot.getMe())

The variable token is assigned the value of the token received when creating the bot through BotFather. As a result, after executing these commands, we get:

{u'username': u'PythonPlanetBot', u'first_name': u'Python Planet Bot', u'id': *****}



Congratulations! We called the simplest API request getMe, which returns information about the bot: username, id, first_name.

Sort:  

You got a 11.11% upvote from @luckyvotes courtesy of @billibong!

This post has received a 3.92% upvote from @aksdwi thanks to: @allup.

You got a 1.68% upvote from @upmewhale courtesy of @allup!

Earn 100% earning payout by delegating SP to @upmewhale. Visit http://www.upmewhale.com for details!

Coin Marketplace

STEEM 0.19
TRX 0.16
JST 0.032
BTC 64122.04
ETH 2745.21
USDT 1.00
SBD 2.66