If you are a regular user of chat apps like Slack or Telegram you may have seen the increase of “bots” available on each platform. Bots? Yes! Like in the old times of IRC. But this time they’re supposed to be smarter. And it’s supposed to be easier for developers to build their own bots. Some people predict that 2016 will be the year of bots, with companies building bots instead of applications. That will change the way we consume information and the way we interact with applications. You can already see that with Birdly, a Slack bot that gathers data about your customers from different services like Salesforce and Mixpanel. Another example is Ottspott, a Slack bot that lets you receive calls directly into Slack. If you’re interested in learning more about this trend and what’s happening with bots, you should check out this announcement from Slack about their bot funds or Chris Messina’s article on conversational commerce.

To help you get started in this bot world, I wanted to show you how easy it is to create a bot for Telegram. Last week at a hackathon I built a bot to help farmers irrigate crops better. I thought I would use pieces of it to write a simple tutorial. We will build a simple weather bot using the Weather Unlocked API.

Create your bot

Before getting started, you need to register your bot on Telegram and give it an identity. To do so, you will use another bot: BotFather.

On Telegram, just start a new conversation with BotFather. Type /newbot command and follow instructions. In the end you should receive a message from BotFather similar to this one with a token. Keep the token safe. We will need it to identify our program against the Telegram API.

botfather result

First simple program

Time to code! I fancy JS at the moment, so the following examples will be using JS and some packages developed for Telegram bots. You can build something similar in any language using other packages or the Telegram API directly.

We will use the great package by yagop.

In a new directory, install the package npm install node-telegram-bot-api.

In a bot.js file paste the following code:

var TelegramBot = require('node-telegram-bot-api');
var token = 'YOUR_BOT_TOKEN';

var bot = new TelegramBot(token, {polling: true});
bot.getMe().then(function (me) {
  console.log('Hi my name is %s!', me.username);

When you run the program in your terminal node bots.js you should see:

~ ❯❯❯ node bot.js                                                                                                                                                             ⏎
Hi my name is YOUR_BOT_NAME!

This means it works and we can move on to the next step to create more complex interactions.

Get your bot to answer commands

Before making your bot as smart as HAL, we will make it understand a command like /weather [postcode]. Also, if you already started a conversation you may have seen that you send a /start automatically to the bot. If we intercept it, we can send a welcome message to the user.

Add the following code to bots.js:

//matches /start
bot.onText(/\/start/, function (msg, match) {
  var fromId = msg.from.id; // get the id, of who is sending the message
  var message = "Welcome to your WeatherBot\n"
  message += "Get weather update by sending /weather [your_city] command."
  bot.sendMessage(fromId, message);

//match /weather [whatever]
bot.onText(/\/weather (.+)/, function (msg, match) {
  var fromId = msg.from.id; // get the id, of who is sending the message
  var postcode = match[1];
  var message = "We will look for weather on postcode "+postcode;
  bot.sendMessage(fromId, message);

Now we will add some logic to gather the weather data for this specific postal code using the Weather Unlocked API. You should create an account on their portal. On the Weather Unlocked API there is a nice feature that lets us ask for weather in a particular US postal code (Docs here).

On top of bots.js we have added two new libraries.

var Q = require('Q');
var request = Q.denodeify(require("request"));

and changed the code to:

// match /weather [whatever]
bot.onText(/\/weather (.+)/, function (msg, match) {
  var fromId = msg.from.id; // get the id, of who is sending the message
  var postcode = match[1];
    var message = "Weather today in "+postcode+"\n";
    message += data.wx_desc+"\n"
    message += "temp: "+data.temp_c+"C or "+data.temp_f+"F"
    bot.sendMessage(fromId, message);


function getWeatherData(postcode){
  var url = "http://api.weatherunlocked.com/api/current/us."+postcode
  url += "?app_id="+app_id+"&app_key="+app_key

  var options ={
      url: url,
      method: "GET",
    var response = request(options);
    return response.then(function (r){
        return r[0].body

You can now relaunch your bot again and send a command like /weather 94107 to get weather in San Francisco. Here is the expected result in Telegram:

conversaiton with weather bot

Go beyond

Congrats! You have built your first Telegram bot! It was really simple and gave you an overview on interactions between users and bots.
There’s a lot more you can do:

  • Change the description and avatar of the bot (ask BotFather ;) )
  • Ask the user if they want current weather or the weather forecast for the whole week (hint: check custom keyboard feature)
  • Go beyond US postal code – let the user put any city name and retrieve latitude and longitude using OpenCage Geocoder
  • Ask the user to send their location directly from Telegram
  • Add some intelligence, so the user can ask “What’s the weather like, now in San Francisco?” and the bot will understand the request (APIs like: Hutoma, motion.ai or Pandoratbots)

The complete project can be found on GitHub here.

Looking forward to seeing what you’ll build with this.