Skip to content

Instantly share code, notes, and snippets.

@qawemlilo
Last active January 29, 2023 08:27
Show Gist options
  • Save qawemlilo/f650d06035d77c74d5b850167122a3fb to your computer and use it in GitHub Desktop.
Save qawemlilo/f650d06035d77c74d5b850167122a3fb to your computer and use it in GitHub Desktop.
Ideas on how to integrate notification channels with ping-monitor
const Monitor = require('ping-monitor');

// how to get channels
const Mailer = require('@ping-monitor/email');
const Slakey = require('@ping-monitor/slack');
const TwiT = require('@ping-monitor/twitter');
const SMS = require('@ping-monitor/sms');


// channel example
class Channel {

  constructor(options) {
    /*** 
    
      do something with the options
      e.g.
      
      const transporter = require("nodemailer").createTransport(options);
      
      this.transporter = transporter;
      
      this.transporter then becomes accessible within your methods
      
     ***/
  }
  
  up(res, state) {
    // send up notification
  }
  
  down(res, state) {
    // send down notification
  }
  
  stop(res, state) {
    // send stopped notification
  }
  
  error(error, res) {
    // send error notification
  }
  
  timeout(error, res) {
    // send timeout notification
  }
}


let email =  new Mailer({
    host: process.env.MAILER_HOST,
    port: process.env.MAILER_PORT,
    secure: false, 
    auth: {
      user: process.env.MAILER_USER, 
      pass: process.env.MAILER_PASS, 
    },
})

let slack =  new Slakey({
    token: process.env.SLACK_TOKEN;
})

const myMonitor = new Monitor({
    website: 'https://ragingflame.co.za',
    title: 'Raging Flame',
    interval: 10 
});


// option 1
myMonitor.addChannel(email)
myMonitor.addChannel(slack)


// option 2
myMonitor.addSubscriber(email)
myMonitor.addSubscriber(slack)


// option 3
myMonitor.subscribe(email)
myMonitor.subscribe(slack)
@kailashvele
Copy link

@qawemlilo At quick glance, I have the following questions that we may have to answer:

  • Do we need more options for emailers like: from, to, subject, cc, and bcc?
  • Do we need an option for Webhook URL and Slack channel?
  • Will the developers have the option to customize the slack message format?
  • As for SMS I believe template formats need to be approved by Telecom Organisations

But as an approach for providing an option for notification in the library, we can go with what you have made. Also, option 1 for addChannel sounds better and clear to use, rather I was thinking to make it addNotificationChannel as the name.

Let me know about your views and if we have to discuss on this then we can get on a call as well, if you are available for that.

@qawemlilo
Copy link
Author

@kailashvele Thanks for your feedback.

  1. Yes I agree, we need more options to make if fully customisable.
  2. The slack snippet was just an example, will have to look at how the api package works.
  3. Yes, will explore how to provide those options.
  4. This was just an example. We could integrate something like Vonage.

I will create a new branch to run some experiments to see what's possible. Will tag you on it.

@kailashvele
Copy link

@qawemlilo Sure I will take a look at it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment