message.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. op-mattermost provides an integration for Mattermost and Open Project.
  3. Copyright (C) 2020 to present , Girish M
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>
  14. */
  15. class Message {
  16. constructor(mmURL) {
  17. this.config = {
  18. headers: { 'Authorization': 'Bearer ' + process.env.MATTERMOST_BOT_TOKEN }
  19. };
  20. this.mmURL = mmURL;
  21. }
  22. showMsg(req, res, axios, msg) {
  23. this.channel_id = req.body.channel_id;
  24. this.user_id = req.body.user_id;
  25. this.msgObj = {
  26. "user_id": this.user_id,
  27. "post": {
  28. "channel_id": this.channel_id,
  29. "message": msg
  30. }
  31. };
  32. axios.post(this.mmURL + 'posts/ephemeral',
  33. this.msgObj, this.config).then((result) => {
  34. if (result.data) {
  35. res.send('Show message post succeeded!').status(200);
  36. }
  37. else {
  38. console.log('Show message post failed!');
  39. res.send().status(400);
  40. }
  41. }).catch((err) => {
  42. console.log('Show message post failed: %o', err);
  43. res.send().status(500);
  44. });
  45. }
  46. showNotification(res, axios, msg) {
  47. console.log("Creating notification for ChannelID: ", this.channel_id);
  48. this.msgObj.post.message = msg;
  49. console.log("Notification post message: ", this.msgObj);
  50. axios.post(this.mmURL + 'posts/ephemeral',
  51. this.msgObj, this.config).then((result) => {
  52. if (result.data) {
  53. res.send('Show notification post succeeded!').status(200);
  54. }
  55. else {
  56. console.log('Show notification post failed!');
  57. res.send().status(400);
  58. }
  59. }).catch((err) => {
  60. console.log('Show notification post failed: %o', err);
  61. res.send().status(500);
  62. });
  63. }
  64. }
  65. module.exports = Message;