telegram bot
This post is my personal introduction to using Telegram and Bluemix. I wanted to set up a simple bot in Python, run it in a Docker container, and deploy it on Bluemix. It is super simple, but the intent is to show how to do the basics before integrating peripheral APIs and other processes.
First, set up a Telegram account however you’d like. I used the desktop client, and it worked great.
Once you have an account, you need to interact with @BotFather to set up a bot. Send /start to @BotFather and you will receive these options:
Reply with /newbot and walk through the steps to get a bot with a name and handle. Also use /setinline to let the bot be used inline.
Purpose
For this, I just wanted to experiment with Telegram, learn how to do simple interactions, and deploy the bot via Bluemix.
Set up Docker
I want the entire bot to run as a Docker container so it can be deployed easily to any cloud container service. This also makes it easier to change the bot and push a new image without having to worry about much else. If you are already using Docker, this just requires adding the cf CLI tool and a plugin to interface with the hosted images and containers.
This is fairly straightforward, and the official directions from here are incredibly concise. Edit: I’ve actually just made a script to install the cf and Bluemix container plugins.
Make sure you can log in (or, if using a different platform, have the ability to push images), and from there pushing images is straightforward.
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 because I want to interface with Keras/TensorFlow/etc. in the future for various tasks and simplify my learning process: python-telegram-bot.
When setting up the bot, we are just going to add a single inline query handler 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. It consists of:
from configparser import ConfigParser
import random
from uuid import uuid4 # used to create individual id's for queries
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 to Bluemix with a Docker image. Since the bot is incredibly simple, the Dockerfile isn’t much:
FROM python:3.5-onbuild
ENTRYPOINT ["python", "main.py"]
Since the python:3.5-onbuild image takes a requirements.txt file 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. Run the following commands for an image/bot named Testbot on 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 uses cf ic, which is how you will interact with the containers. For instance, stopping this bot would require you to run cf ic stop -t 1 testbot. The bot is now running and available to anyone. Here is an example of how it looks when used inline and with a standard command:
With this basic outline, you can easily add other functionality. 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.
From here, my plan is to use the Watson API with Bluemix and extend the bot into something a bit more useful and interesting, which will be in a future post.
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