Start ChatOps with Telegram Chatbot

Jeongsoo Park
Aug 2, 2017 · 3 min read

I’m going to build a ChatOps which manages DigitalOcean Droplets using DigitalOcean API. We’ll utilize a property feature of BotHub.Studio and Telegram keyboard. Extend it yourself to fit your own needs.

We will use python as a language and Telegram as a messenger platform and use a BotHub.Studio for host and run the code.

If you have no experience to build any chatbot before, I recommend reading Build a Telegram Chatbot with Python first.

Preparation

We need to generate an access token of Digital Ocean account. Login to DigitalOcean, go to API menu and click Generate New Token. Keep the access token to use later.

Find @BotFather in telegram search box. Or you can meet him with https://t.me/BotFather link.

Create a new bot by telling him /newbot command. Keep the access token he gave you to use later.

Create an account on BotHub.Studio which is a chatbot hosting service, and install CLI tool using the command below:

Run configure to connect to the service:

Create a project directory:

A template code will be generated after init finished:

Connect a Telegram channel to the project:

The template project is an echo bot so you can check it works without any modification. Test whether your chatbot works by deploying it:

Make it works with DigitalOcean

Create a bothub/doapi.py in the project directory and write a class below:

You can create, get a list, delete a droplet with DigitalOcean API. For convenience, I limited options to name, region, image, and size will only be 512mb.

Connect to Chatbot

Store some information for droplet creation to property.

Telegram chatbot conversation starts with /start message. We’ll ask to input access token when we get the message. Open bothub/bot.py and write Bot.handle_message method.

‘Create Droplet’ and ‘List all Droplets’ menu will show on Telegram keyboard when the access token is confirmed.

Now we’ll create a droplet. (I omitted some part of the previous code for clear understanding. Attach below code to it.)

Add below to get Droplet list.

You can get the complete code on Github repository. And you can find a working chatbot with the code above on Telegram with username @Digital_Ocean_Bot, or https://t.me/Digital_Ocean_Bot.

BotHub.Studio

News, tutorials about BotHub.Studio chatbot hosting service

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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