slack.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Require modules. */
  2. const superagent = require('superagent')
  3. /* Set Slack bot token. */
  4. const token = process.env.SLACK_BOT_TOKEN
  5. /**
  6. * Slack Notification
  7. *
  8. * References:
  9. * - https://api.slack.com/interactive-messages
  10. */
  11. const Slack = async function (_json) {
  12. // console.log('BOT POST RECEIVED', _msgBlock)
  13. /* Set url. */
  14. const url = 'https://slack.com/api/chat.postMessage'
  15. // NOTE: UQ5AXJW06 - Modenero & UQ3B5EP53 - Shomari
  16. const users = 'UQ5AXJW06,UQ3B5EP53'
  17. /* Set group channel. */
  18. const channel = 'GSCHHNRPF'
  19. /* Set text. */
  20. const text = ''
  21. const blocks = [{
  22. type: "section",
  23. text: {
  24. type: "mrkdwn",
  25. text: "```" + JSON.stringify(_json, null, 2) + "```"
  26. }
  27. }]
  28. /* Build a (message) block package. */
  29. const msgBlock = {
  30. channel,
  31. text,
  32. users,
  33. blocks
  34. }
  35. try {
  36. await superagent
  37. .post(url)
  38. .send(msgBlock)
  39. .set('accept', 'json')
  40. .set('Content-type', 'application/json;charset=utf-8')
  41. .set('Authorization', `Bearer ${token}`)
  42. .end((err, json) => {
  43. console.log('\nBOT POST RESPONSE', JSON.stringify(json.body, null, 4))
  44. /* Return the json body. */
  45. // res.json(json.body)
  46. })
  47. } catch (err) {
  48. console.error('BOT POST ERROR', err)
  49. /* Return the error. */
  50. // res.json({
  51. // error: err
  52. // })
  53. }
  54. }
  55. /* Export handler. */
  56. module.exports = Slack