Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const parse = require('url').parse;
  2. const { WebClient } = require('@slack/client');
  3. const chat = new WebClient(process.env.SLACK_OAUTH_TOKEN).chat;
  4.  
  5. const request = require('../src/request');
  6.  
  7. const formatPing = ping => {
  8.   return `\`\`\`${JSON.stringify(ping, null, 2)}\`\`\``;
  9. };
  10.  
  11. /**
  12.  * @param {array} urls
  13.  */
  14. module.exports = async urls => {
  15.   const pings = await Promise.all(
  16.     urls.map(url => {
  17.       return request(parse(url));
  18.     })
  19.   );
  20.   pings.forEach(ping => {
  21.     if (ping.statusCode < 200 || ping.statusCode > 299) {
  22.       chat.postMessage({
  23.         channel: process.env.SLACK_CHANNEL_ID,
  24.         attachments: [
  25.           {
  26.             text: `Failed to reach ${url}\n${formatPing(ping)}`,
  27.             color: 'danger'
  28.           }
  29.         ]
  30.       });
  31.     }
  32.   });
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement