Sunday, 17 May 2020

slack webhook nodejs tutorial

Slack has introduced ways to manage and send information via app. This method is still accurate and there hasn’t been any report regarding deprecating it anytime soon.

Slack have a very feature rich integration layer via smooth to set up WebHooks. It’s is even available to have a fully interactive message flow that deliver actions directly from the Slack chat to your application and also updates the messages depending on the return from your server.

This post will tell you how to send some custom error message or info messages to slack for notifications or history. slack webhook nodejs help you in how to create slack webhook and how to integrate it with nodejs helper.

Lets start the slack webhook api integration.

Three things you need it before this.

  • Nodejs express project setup
  • Slack package for nodejs
  • Slack webhook
So for setting up nodejs express project i'm going into detail you can check my other article to use nodejs+express setup. I'm supposing that you already had a setup of project.

Second thing you need is slack npm package.

npm install winston winston-slack-webhook-transport --save

Third thing you need is slack webhook url . So please follow this weblink to generate webhook.

slack,webhook,webhooks,slack webhooks,nodejs,slack bot,node.js,nodejs slack,slack bot nodejs,slack app,slack tutorial,slack web hook,slack web hook demo,slack integration,nodejs slackbot,node slack app,node.js slackbot,connect slack with node,what is a webhook,slack room,discord webhook,node connect with slack,webhooks explained,send message to slack using web hook and nodejs,what are webhooks,web-hooks
slack webhook integration

3.1 Add a new configuration so click on it to create new webhook. You also need to specify a channel or create a new one.

slack message builder


3.2 After that you need to specify your channel and create webhook.

node slack webclient


After adding on incoming webhook it will give you a weblink save the link for further use.


Now lets come to the coding part. Now we are creating a helper function using winston logger . winston logger is provide you many transport hook like file logging, console logging and we are using slack logging feature.

Let's create notify.js in your nodejs project.Put your webhook in webhookUrl variable.

var winston = require('winston');
const { createLogger, format, transports } = require ('winston');
const SlackHook = require ('winston-slack-webhook-transport');
const moment = require ('moment');

var notify = createLogger ({
    level: 'warn',
    timestamp: true,
    transports: [
      new winston.transports.Console (),
      new SlackHook ({
        webhookUrl: 'https://hooks.slack.com/services/TJ5JXXXXXJ/BPPPPPQXGV/XXXXXXXXHRzTZqpdOtaDUzj8',
        formatter: info => {
          return {
            text: `*${process.env.name}*:*${process.env.NODE_ENV}* => ${info.level}: ${info.message}`,
          };
        },
        username: 'WantCodeBot',
      }),
    ],
});


Now everything is ready now you can import this notify.js file in your controller or your file to push your custom logs into slack.

const notify = require('../../../helpers/notify');
notify.warn(
`this api throw error \n> *request*: \n>${JSON.stringify(req.body)} *response*: ${JSON.stringify(response)}`
);

Now visit slack you will see a new notification in your channel. isn't it cool.


Featured post

How to create dynamic schema in mongodb

Sometime what we need in our application is to create mongodb schema for collection and some of our collection schema are same but names are...

Popular Posts