Setting up a Telegram Bot in Python and Docker
This post is my personal introduction to using Telegram and Bluemix and while it is incredibly simple, it is useful to see how to do the basics before integrating peripheral API’s or extraneous processes.
First, set up a telegram account however you’d like. I personally used the Desktop client and it worked great. Once you have an account, you need to interact with @BotFather to set up a bot. Message @BotFather /start and you will receive these options:
Reply with /newbot and walk through the steps to get a bot with a name and handle. Also /setinline to allow inline of bot.
For this I just wanted to experiment with telegram and see how to do very simple basic interactions and deploy it via Bluemix.
Set up Docker
I want to allow the entire bot to be run as a Docker container to allow for easy deployment to any cloud container service as well as make changes and push an image without having to worry about much else. To do this, if you already are using Docker it just requires adding the cf CLI tool and a plugin to interface with the hosted images/containers. This is fairly straightforward and the official directions from here are incredibly concise Edit: I’ve actually just made a script for install of cf and bluemix stuff with container plugins.
Make sure you can login (or if using a different platform, have the ability to push images) and from there you can easily push images.
Using Python to interact with Inline Queries and Commands
There is an easy to use and well maintained python module for the telegram API. I’m using python rather than something like node to interface with keras/tensorflow/etc in the future for various tasks and want to simplify and streamline my learning process. python-telegram-bot
Setting up the bot we are just going to be doing a single inline ability and a single telegram command. It’s fairly straightforward and I used most of the example file from the python-telegram-bot library for simplicity but it consists of:
from configparser import ConfigParser import random from uuid import uuid4 #used to create individual id's for querys from telegram import InlineQueryResultArticle, InputTextMessageContent from telegram.ext import Updater, InlineQueryHandler, CommandHandler vids = [...list of assorted videos...] def get_config_token(): config = ConfigParser() config.read_file(open('config')) token = config['default']['token'] return token def tellem(bot, update): bot.sendMessage(update.message.chat_id, text=msg + random.choice(vids)) def inlinequery(bot, update): results = list() results.append(InlineQueryResultArticle( id=uuid4(), title="tell em", input_message_content=InputTextMessageContent(msg + random.choice(vids)))) bot.answerInlineQuery(update.inline_query.id, results=results) def main(): # updater with config updater = Updater(get_config_token()) # Get the dispatcher to register handlers d = updater.dispatcher # example of standard command d.addHandler(CommandHandler("tellem", tellem)) # inline part d.addHandler(InlineQueryHandler(inlinequery)) # Start the Bot updater.start_polling() # Block until the user presses Ctrl-C or the process receives SIGINT, updater.idle() if __name__ == '__main__': main()
From here, we will deploy with a Docker image to Bluemix. Since the bot is incredibly simple, the Dockerfile isn’t much:
FROM python:3.5-onbuild ENTRYPOINT ["python", "main.py"]
Since the python:#-onbuild image takes a requirements.txt and installs dependencies for us, make a requirements.txt file and put “python-telegram-bot” in it since that is the only dependency.
I’m also reading the API token from a config file that looks like:
[default] token = 'token here'
Then to build and deploy you need to have the Bluemix command line tool and the containers command line plugin that we (hopefully) previously installed and run the following commands for an image/bot named Testbot on a your personal registry called your-registry:
Docker build -t Testbot . Docker tag Testbot registry.ng.bluemix.net/your-registry/Testbot:latest Docker push registry.ng.bluemix.net/your-registry/Testbot:latest cf ic run --name testbot registry.ng.bluemix.net/your-registry/Testbot:latest
The last command, which uses cf ic is how you will interact with the containers and for instance to stop this bot would require you to run “cf ic stop -t 1 testbot”. The bot is now running and available to anyone. An example of how it will look via inline and using standard command:
With this basic outline you can easily add other functionality and using @BotFather you can also add an image, help text, etc. This could also easily be done on a multitude of other platforms besides IBM’s Bluemix.
Using Bluemix I will next use a Watson API to do something actually useful (will post soon).
If you are looking to make a bot to do something or integrate with the IBM Bluemix Platform let me know, I’d love to help! Email Me