DEV Community

Cover image for Use Telegram Bot for staying in touch with your Javascript apps health
Joe Beretta
Joe Beretta

Posted on

Use Telegram Bot for staying in touch with your Javascript apps health

👋 Hi there!

Prehistory

A few days ago in the evening, my team leader wrote to me that he found that a new feature that I added to our application was causing an error. And then I thought I needed a way to stay in touch with all critical errors.

At that moment I got an idea: make a npm package for integrating projects existing log service with telegram bot and log everything I need to know.

And here you may have questions:

- Why not to use one of existing cool packages for creating telegram bots and just send log messages?

- Because all of existing packages for creating telegram bots are overwhelmed for this usecase. For this task enough just one function for sending messages to chat

- Why do I need another one npm package?

- The answer is simple: I wanted to learn how to create and publish my own packages

And here we are: I present my first npm package ever, called node-telegram-log

Telegram Logger

Simple Telegram logger for node.js.

Install

$ npm install node-telegram-log
Enter fullscreen mode Exit fullscreen mode

Prerequisites

  1. Create telegram bot and get it's token:

  2. Add bot to group or go to bot's page and /start

  3. Get chat_id where bot will send messages. Feel free to use IDBot to get chat_id

Usage

const { TelegramLogger } = require('node-telegram-log');
const logger = new TelegramLogger(BOT_TOKEN_ID, CHAT_ID);

// Log some message
logger.log('Hooray! It works');
// Formatted message
/**
 * ℹ️ LOG
 *
 * Hooray! It works
 */

// Or debug
logger.debug('Just debugging it', { canILogObjects: true });

// Formatted message
/**
 * ⚙️ DEBUG
 *
 * Just debugging it
 * {
 *   "canILogObjects": true
 * }
 */

// Mention user, who must to pay attention to this message
// Note: @mentions work only if
Enter fullscreen mode Exit fullscreen mode

How to use

  • First of all create bot. You can read this article if you don't know how;

  • After you have finished setting up your bot, we can move on

npm install node-telegram-log # Install as project dependency
Enter fullscreen mode Exit fullscreen mode
  • Let's log it!
const { TelegramLogger } = require('node-telegram-log');

const BOT_TOKEN_ID = 'YOUR BOT TOKEN' // Got it in previous step
const CHAT_ID = 1234 // Feel free to use https://t.me/myidbot

const logger = new TelegramLogger(BOT_TOKEN_ID, CHAT_ID);

// Some logic here...

logger.log('Hooray! It works');

// Notify a colleague about a problem
// Note: @mentions work only if part of message starts with @username
logger.error('@joeberetta', 'Something went wrong:', { formatted: true });
Enter fullscreen mode Exit fullscreen mode

At last

Interesting fact: when I published this package I found that similar one exists already!

Feel free to use it (or just Ctrl+C/V it from repo 😁😁😁) and contact me if you have any questions, ideas or issues.

Thanks to @ispoljari for his cool and motivational post:

Thanks for reading this post 🙏

Top comments (0)