Considering that you already know what is a bot, I’ll try to explain how you can easily create a telegram bot in python. Here I will be usingpython-telegram-botlibrary for python (https://python-telegram-bot.org). It is a wrapper you can not refuse.
Obviously the first step is to have a Telegram account, if you do not have one, you can download the Telegram App for your device. Here I am using Telegram’s web interface (https://web.telegram.org/ ).
One cool thing about telegram is that, it comes up with its own bot BotFather ( I do not know if it is related to GodFather), where it helps you set up and manage your bots. Just say “hi” to that BotFather, it will show you a bunch of commands which can be used to control your bots.
The moment you send “/newbot” to BotFather, it will guide you to create your own first bot. Further steps are easy to follow, so I’ll skip that part. Once you are done with naming your bot, you will receive a text containing token to use your bot. ( Keep this token confidential, if in any case, it is disclosed you can change the token any time. Do not worry BotFather is always at your service to guide you.)
I use Python 3.5 and I recommend you to use the same as well. No offence to 2.7 users, the library I am using for creating the bot supports Python 2.7+.
python-telegram-bot — We have made you a wrapper you can’t refuse
Okay, as a part of this demonstration, I have used their own tutorial code which is in their github repo —https://github.com/python-telegram-bot/python-telegram-bot
To install the library : Open up your terminal / command prompt and run:
pip install python-telegram-bot
After installing the library, fire up your python console:
First, you have to create anUpdater
object. Replace'TOKEN'
with your Bot's API token.
>>> from telegram.ext import Updater
>>> updater = Updater(token='TOKEN')
For quicker access to theDispatcher
used by yourUpdater
, you can introduce it locally:
>>> dispatcher = updater.dispatcher
Now, you can define a function that should process a specific type of update:
>>> def start(bot, update):
... bot.sendMessage(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
The goal is to have this function called every time the Bot receives a Telegram message that contains the/start
command. To accomplish that, you can use aCommandHandler
(one of the providedHandler
subclasses) and register it in the dispatcher:
>>> from telegram.ext import CommandHandler
>>> start_handler = CommandHandler('start', start)
>>> dispatcher.add_handler(start_handler)
And that’s all you need. To start the bot, run:
>>> updater.start_polling()
Give it a try! Start a chat with your bot and issue the/start
command - if all went right, it will reply.
That’s all folks! You have your bot which says “I’m a bot, please talk to me!”. This might be an end to this story but this is not an end to the adventure you will begin. I urge you to read the documentations for this library and build your own bot.
All the codes here are copied from their first bot tutorial. Read more about this library here:
This tutorial has more on basics on how to create a really cool bot of your own. Cheers!
by the author.