Forging Your Telegram Bot

Bot Stack

Tech Stack

  • Python, since it offers abundant easy to use libraries to perform our backend tasks easily with minimal code
  • Flask, it is a lightweight web application framework for quick and easy development

Service Platforms

  • Herokuapp.com, a freemium model based website with great dashboard to track app activity and direct deploy feature for Github repositories
  • Telegram, we’ll use telegram’s Bot Father to create and deploy our bot

Getting Bot Token

  • Search for Bot Father using search button in Telegram app
  • Type and send “/newbot”
  • Enter a name for your bot
  • You’ll be given your token now

Forging the Bot

  • Start a new Flask project
  • You can set up the project and run the app to see if it’s working properly
  • Install and import the required dependencies like below
  • We will initialise our bot as follows
  • We create a queue to store all incoming requests since there may be more than one request that needs processing, so we store and later process them one by one
  • Now we need the url endpoint at which we will set a webhook for incoming requests from our bot redirected by the Telegram server. I used the endpoint “/work” for it.
  • We need the endpoint to accept incoming POST requests
  • Now we define the handle function which gets msg object as input
  • You can print msg to see the structure of the object for better clarity of the structure of msg dictionary
  • The text from user resides in msg[‘text’]
  • We check the first word to see which type of query it is and respond accordingly
  • Finally we ask the bot to reply via sendMessage to the user

Making Modules

  • Beautiful soup is an amazing library to parse and filter content from webpage html via tags
  • You can go to the actual website and see their html structure
  • The result object has an array of jokes from which we return a random joke
  • Just like with the joke module, we select a random meme from the webpage and get it’s src url
  • We then use the url to read the image data and write the bytes in meme.jpg file
  • In the main file when we get response “done” from our module, we use the bot to send the image as follows
  • We use an API to fetch news urls and send them to the user
  • You can register for your own API key and start making requests like below

Final Touches to Code

  • You need to freeze requirements for your project as given here
  • Then you need to add a Procfile file to your project

Uploading on HerokuApp

  • Now go to deploy tab
  • Select deployment method as Github
  • Search for your repository in the search bar below and connect to it
  • Now click on Deploy Branch

Setting Webhook

https://api.telegram.org/bot{your_bot_token}/setWebhook?url=https://________.herokuapp.com/work

Test the Bot

Bot Prettifying

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aarnav Jindal

Aarnav Jindal

Avid programmer chasing developments in the dynamic and invigorating world of technology 🤓