|
| 1 | +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; |
| 2 | + |
| 3 | +const request = require('request-promise-native'); |
| 4 | + |
| 5 | +function sendRequest(namespace, text, level='INFO', options={}) { |
| 6 | + if (options.chat == undefined) { |
| 7 | + console.error('PushNotice: chat object has to be provided'); |
| 8 | + return; |
| 9 | + } |
| 10 | + if (options.chat && options.chat.id == undefined) { |
| 11 | + console.error('PushNotice: chat id has to be provided'); |
| 12 | + return; |
| 13 | + } |
| 14 | + if (options.chat && options.chat.secret == undefined) { |
| 15 | + console.error('PushNotice: chat secret has to be provided'); |
| 16 | + return; |
| 17 | + } |
| 18 | + if (namespace == undefined) { |
| 19 | + console.error('PushNotice: namespace has to be provided'); |
| 20 | + return; |
| 21 | + } |
| 22 | + if (text == undefined) { |
| 23 | + console.error('PushNotice: text has to be provided'); |
| 24 | + return; |
| 25 | + } |
| 26 | + if (level == undefined) { |
| 27 | + console.error('PushNotice: level has to be provided'); |
| 28 | + return; |
| 29 | + } |
| 30 | + if (options.url == undefined) options.url = 'https://pushnotice.chat/api/vi/chatnotice'; |
| 31 | + if (options.debug == undefined) options.debug = false; |
| 32 | + let requestOptions = { |
| 33 | + method: 'POST', |
| 34 | + uri: options.url, |
| 35 | + body: { |
| 36 | + chatId: options.chat.id, |
| 37 | + chatSecret: options.chat.secret, |
| 38 | + namespace: namespace, |
| 39 | + level: level, |
| 40 | + text: text, |
| 41 | + }, |
| 42 | + json: true, |
| 43 | + headers: { |
| 44 | + 'content-type': 'application/json', |
| 45 | + } |
| 46 | + }; |
| 47 | + request(requestOptions).then((res) => { |
| 48 | + if (options.debug) console.log("Successfully sent Notification to PushNotice API"); |
| 49 | + }).catch((err) => { |
| 50 | + if (options.debug) console.error("Error sending Notification to PushNotice API"); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +module.exports = (namespace, options={}) => { |
| 55 | + return (text, level='INFO') => { |
| 56 | + sendRequest(namespace, text, level, options); |
| 57 | + } |
| 58 | +} |
0 commit comments