DEV Community

Cover image for Increasing your productivity with Telegram and Node.js
Lars Wächter
Lars Wächter

Posted on

Increasing your productivity with Telegram and Node.js

This article was originally published on Medium.

Some time ago I searched for an easy way to establish a communication channel between a mobile device and a Node.js webserver. My goal was to exchange messages over this channel and receive information about the weather, public transportation and more.

For example I send the message /train and receive a response with realtime details about train departure times of preconfigured routes. So the Node.js server receives the incoming message, processes it and sends a response back to the client.

After doing some researches I finally came up with Telegram bots since they are very easy to setup and fit perfect to my needs. Besides sending text messages, you can also share data like images or audio recordings.


First of all, what exactly is a Telegram bot? Source

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our bot API.

So you simply send a message from your phone via Telegram and your webserver receives it over Telegram’s API.

Just to name some of the things you can use your own bot for. Whether just for you or for your friends as well:

  • Gathering weather information
  • Fetching arrival / departure times of public transportation
  • Receiving tweets, news, status updates
  • Sending automated messages
  • IoT

and so much more.


One big benefit of Telegram bots is that you don’t need a public server which is accessible over an IP-address from outside the network. In my case I use a Raspberry Pi to run the Node application for example.

Since the communication takes place over the Telegram API, there’s just an internet connection required.

For interacting with it you can use a runtime environment like Node.js as I did in the example app below or any other programming languages.

Here you can find an introduction about how to interact with the API.


As I mentioned above I recently created an example app for a Telegram bot server based on Node.js. Feel free to use it for your own bot and customize it according to your wishes or contribute to it.

Let me know what you use your bot for and share your experience!

GitHub logo larswaechter / telegram-bot-server

An extendable webserver for communication with the Telegram Bot API.

telegram-bot-server

An extendable webserver for communication with the Telegram Bot API.

Description

telegram-bot-server is a Node.js powered webserver with MongoDB integration for running your own Telegram Bot as well as a REST API. With the help of Telegram bots you can do quite a lot of useful things.

Just to give some examples:

  • Gathering weather information based on your location
  • Fetching arrival / departure times of public transportation
  • Sending automated messages
  • IoT

You can make your bot public for other users or just use it for private purposes.

This application provides one example bot command (ping) innately but you can easily integrate your own ones into the current system.

See the Commands section for more information.

Prerequisites

  • Node.js
  • MongoDB
  • Telegram Bot
  • Internet connection

Installation

First of all, create a new MongoDB database:

use telegram-api-server

Afterwards, copy the environment file and enter your secret information:

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

Now…

Top comments (0)