What?

Telegram is a messenger app I really like and I use the most for communication. The two things which make it best for me are: native Linux application (which is important for me as my machine is running Ubuntu) and awesome bot platform with an easy and powerful API.
I developed and run couple of bots for Telegram which are used by me and some other people.

Why?

Sometimes people ask me to give them a quickstart on how to develop bot for telegram. So I decided to write this blog post to give some quick tips on this and give some code snippets.

How?

In this tutorial I will show how to develop the simple bot using NodeJS and node-telegram-bot-api module.

Step 1: Creating and configuring the bot

First of all, you will need to talk with @botfather. Create your bot there, get bot token (will look like some_number:some_symbols) and enable inline queries support if you want your bot to work in inline mode (What is inline mode?).
The process is easy and straightforward, so I think there is no need to describe it here.

Step 2: Let's begin writing code!

This step assumes that you have installed and configured NodeJS and NPM. First of all, install required library with npm: npm install node-telegram-bot-api. Next, let's create a file bot.js where we will write all of our code.
First few lines are simple:

const TelegramBot = require('node-telegram-bot-api');

const api_token = 'your_api_token';
const bot = new TelegramBot(api_token, {polling: true});
Here we import required module, define api token (you have to place yours here, which you previously obtained by talking to BotFather) and create our bot instance.

Polling or Web Hooks?

There are two ways your bot can receive updates: by doing regular requests to telegram server (which is called polling) or by setting up a webhook (you need externally accessible by HTTP server with HTTPS configured for this). When using webhook, telegram server will send you all updates. So bots using webhooks are generally faster. However, for simplicity reasons we will use polling in this tutorial.

Step 3: Let's add code to handle messages

node-telegram-bot-api makes it ridiculously easy to handle chat events.
Basic messange handler looks like this:

bot.on('message', (msg) => {
    if (msg.text == '/ping') {
        bot.sendMessage(msg.chat.id, 'pong!');
    }
});
The handler we implement here receives all messages. If message is /ping, it replies with "pong!". You can save the code now, run it with node bot.js and send some test messages to your bot.
This should be enough for basic bot quickstart, but if you are interested, you may continue to more advanced part: inline queries.

Step 4 (optional): Let's add inline queries support

This is a really interesting feature, however it may be a bit more difficult at implementing. To start with, please check that you have enabled inline mode for your bot using BotFather. To demonstrate inline mode, we will build a function which will let users quickly make search links for different search engines.
The code:

bot.on('inline_query', (msg) => {
    let query = encodeURIComponent(msg.query.trim());
    bot.answerInlineQuery(msg.id, [{
        type: 'article',
        id: query + '_google',
        title: 'Google',
        input_message_content: {
            message_text: 'https://www.google.com/search?q=' + query,
        }
    },
    {
        type: 'article',
        id: query + '_bing',
        title: 'Bing',
        input_message_content: {
            message_text: 'https://www.bing.com/search?q=' + query,
        }
    },
    {
        type: 'article',
        id: query + '_yahoo',
        title: 'Yahoo',
        input_message_content: {
            message_text: 'https://search.yahoo.com/search?q=' + query,
        }
    }]);
});
Explanation to the code: here we setup a handler for inline queries. It is called when user types our bot name in message input and starts to write some message. The message is available in msg.query.
After getting inline query request, we need to answer it with answerInlineQuery. We need to provide query id and an array of results. These results will be displayed to user above the message input. Results can be of different content types: images (including animated gifs), videos, stickers, text, etc. The one thing I found a bit confusing at first is that if you want to send text using inline queries, you need to use article content type at first.
Another important thing is that each result has to have it's unique id. Results are cached, this means that sometimes telegram server provides user inline query results even without calling your bot (don't forget to take this into account while developing your bots).
Everything else is easy here: we specify result title in title, and message text (which will be sent after user selects this result) in message_text

All code from this tutorial

const TelegramBot = require('node-telegram-bot-api');
const api_token = 'your_api_token';
const bot = new TelegramBot(api_token, {polling: true});

bot.on('message', (msg) => {
    if (msg.text == '/ping') {
    	bot.sendMessage(msg.chat.id, 'pong!');
    }
});

bot.on('inline_query', (msg) => {
    let query = encodeURIComponent(msg.query.trim());
    bot.answerInlineQuery(msg.id, [{
        type: 'article',
        id: query + '_google',
        title: 'Google',
        input_message_content: {
            message_text: 'http://lmgtfy.com/?q=' + query,
        }
    },
    {
        type: 'article',
        id: query + '_bing',
        title: 'Bing',
        input_message_content: {
            message_text: 'http://lmgtfy.com/?s=b&q=' + query,
        }
    },
    {
        type: 'article',
        id: query + '_yahoo',
        title: 'Yahoo',
        input_message_content: {
        message_text: 'http://lmgtfy.com/?s=y&q=' + query,
    }
    }]);
});

Thank you for reading this! Hopefully this will help you to start making your telegram bots. I am also planing to edit and extend this post in the future.