Telegram can be used for chatops using the extensive Bot API provided.Its user base,availability of mobile and web app support makes it good candidate for personal and small business use,if not enterprise.
Let’s build a sample Bot which send us random photo from Unsplash using Python,inspired by this post
- Create Telegram Bot
- Request Unsplash API key
- Install dependencies
- Main Program
- Using Bot
- What’s Missing
- Conclusion
Create Telegram Bot
Interestingly Telegram provides us an official Bot BotFather to request our Bot creation.Open the link in Telegram and request your Bot.
Request Unsplash API key
With the Bot now available we need Unsplash API key for our Bot to access and request images.The procedure is simple.
- Visit Unsplash Developer page
- Register and request API key for your account.
Additonal documentation on using API is available here
Install dependencies
With Telegram Bot and Unsplash API key its now time to prepare your Python environment.Install the Telegram Bot library using
Main Program
With our unique URL we can fetch the random image details as a JSON which will be used by our Bot to respond to the User.
Below is the sample program to create a Bot.I have updated the script in the source to use latest Bot API.
Using Bot
We are now ready to use our Bot.
- Run the Python script above.
- Start conversation with our Bot from Mobile or Web.
- Send /pic as message
- The Bot responds with random image.
Congrats you have built your first usable Telegram Bot.
What’s Missing
You might have started noticing the limitations in your script and Bot,the availability,memory usage,timeouts etc.
To overcome the same,we need to
- Design and run the Python requests as a service and Flask is best choice.
- Host the service on a dedicated Server or cloud to make it available and scale.
Conclusion
This was a simple exercise to create a Telegram Bot and to show its capability.You can extend this to create a chatbot which can serve as your personal assistant.