Hey everyone ! hope you are having a great quarantine..
NOTICE: If You don’t have patience. Here’s the code>>https://github.com/ashuprince121/Intoduction-to-telegram-Bot
Prerequisite: Python 3 installed , Telegram Account ,and Internet -_-!
So in this post I will show you how you can build a Telegram Bot from scratch. This post is going to be very very very basic, and I think it will give you enough of base to start making more complicated bots.
About Me:
Year #3, Computer Science, L.P.U.
NO idea what I am doing, still exploring stuff
What We will Learn Today?
What (and why) is Telegram?
- Telegram is an instant messaging service, similar (and in my opinion, much better) than WhatsApp.
• It is widely considered to be more secure and private than WhatsApp.
• It is cloud-based, synced acro ss different devices and free.
• It offers many features like alternate client developments and bot developments.
Why Python for Telegram Bots?
- An important reason is that I only know bot development using Python :P
- For some other reasons…
- Python is great for rapid prototyping of apps
- Offers many Telegram API wrappers which make bot development easier
- Relative
Python Refresher
I think that was enough to give you guys Flashbacks.
What are Telegram Bots ?
• Telegram bots are small programs that can embed in Telegram chats or public channels and perform a specific function.
- They can be quite powerful and do a variety of things, from entertaining you to sending email notifications.
The very first step is : Open your telegram app, search for ”BotFather” and request your bot api key by following the steps written above.
Now its time to write code, I personally prefer and use Pycharm, but you can use whatever you are comfortable with. You can even code in your notepad just take care of indentations.
Now I want you to set up some things..
Go to this Link https://www.shortto.com/telegramBot and download the folder bots.
- Navigate to the “bots” folder
Run the command `pip install -r requirements.txt` from command line
- If everything is fine till this point, we are good to go!
You can navigate on windows using “ cd ” command for eg cd C:\Users\HP\Pictures\Screenshots
or you can just go to the “bots” folder and do shift+right click and hit open windows powershell here and run the command.
You can run your code on cmd or powershell by writing out“ python filename.py.”
For this post we will create a bot that takes a text and convert it to cute version of same text.
First thing First we need to import some things
Updater is everything which gets the updates from user , updater just tracks when user input somthing.
CommandHandler and MessageHandler
In telegram there are two kindsof statement you can make , you can just write out something like a normal text, or you can put ”/” and some text, and whenever a text start with slash ,its called a command.
So , “hi” is a text , for this we need a message handler but for “/start” its a command and for this we need a CommandHandler.
Filters
Filters are just used to filter out text , you can import it but you don’t need to know how it works.
So now , when we work with telegram bots we register functions with commands or we register functions with messages.
So we will now create some functions and register them.
Now we will register our functions in the main function we can do that in this way:
dispatcher
Dispatcher that handles the updates and dispatches them to the handlers.
Starts polling updates from Telegram. You can stop the polling either by using the the interrupt R
command in the session menu or with the stop_polling
method.
That’s all our Bot is ready to be deployed , obviously you can use it without deploying from your lappy, you just need to be connected with the internet.But once your machine shut down it wont run anymore.
For Deployment we will talk about it later , I think its enough for this post, however if you re curious you deploy it on HEROKU for free.
Keep Developing…. See you in cloud :)