Commit e8e73521faa019755c9770984e6776745b3360b1
1 parent
89c3ce14df
Exists in
master
More log on transport send
Showing 1 changed file with 7 additions and 2 deletions Inline Diff
lib/transport.js
1 | const fs = require('fs'); | 1 | const fs = require('fs'); |
2 | const moment = require('moment'); | 2 | const moment = require('moment'); |
3 | const uniqid = require('uniqid'); | 3 | const uniqid = require('uniqid'); |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | async function send(partner, msg) { | 8 | async function send(partner, msg) { |
9 | if (typeof partner !== 'string' || typeof msg !== 'string') return; | 9 | if (typeof partner !== 'string' || typeof msg !== 'string') return; |
10 | if (!partner.trim() || !msg.trim()) return; | 10 | if (!partner.trim() || !msg.trim()) return; |
11 | 11 | ||
12 | const destination = partner.trim().replace(/^0/, '62').replace(/^\+/, ''); | 12 | const destination = partner.trim().replace(/^0/, '62').replace(/^\+/, ''); |
13 | 13 | ||
14 | if (!destination || !Number(destination) || destination.length < 8) return; | 14 | if (!destination || !Number(destination) || destination.length < 8) return; |
15 | 15 | ||
16 | const msgFileContent = ` | 16 | const msgFileContent = ` |
17 | To: ${destination} | 17 | To: ${destination} |
18 | 18 | ||
19 | ${msg.trim()} | 19 | ${msg.trim()} |
20 | `.trim(); | 20 | `.trim(); |
21 | 21 | ||
22 | const dir = config.outbox_dir || '/var/spool/sms/outgoing'; | 22 | const dir = config.outbox_dir || '/var/spool/sms/outgoing'; |
23 | const ts = moment().format('YYYYMMDD_HHmmss.SSS'); | 23 | const ts = moment().format('YYYYMMDD_HHmmss.SSS'); |
24 | const filename = `${dir}/komodo.${ts}.${uniqid()}`; | 24 | const filename = `${dir}/komodo.${ts}.${uniqid()}`; |
25 | 25 | ||
26 | fs.exists(dir, (isExists) => { | 26 | fs.exists(dir, (isExists) => { |
27 | if (!isExists) { | 27 | if (!isExists) { |
28 | logger.warn('Outbox dir is not exists', { dir }); | 28 | logger.warn('Outbox dir is not exists', { dir }); |
29 | return; | 29 | return; |
30 | } | 30 | } |
31 | 31 | ||
32 | logger.info('Writing outbox', { filename }); | 32 | logger.info('Writing outbox', { to: destination, msg: msg.trim(), filename }); |
33 | try { | 33 | try { |
34 | fs.promises.writeFile(filename, msgFileContent, { mode: 0o660 }); | 34 | fs.promises.writeFile(filename, msgFileContent, { mode: 0o660 }); |
35 | } catch (e) { | 35 | } catch (e) { |
36 | logger.warn('Exception on writing outbox', { filename, err: e.toString() }); | 36 | logger.warn('Exception on writing outbox', { |
37 | to: destination, | ||
38 | msg: msg.trim(), | ||
39 | filename, | ||
40 | err: e.toString(), | ||
41 | }); | ||
37 | } | 42 | } |
38 | }); | 43 | }); |
39 | } | 44 | } |
40 | 45 | ||
41 | exports.send = send; | 46 | exports.send = send; |
42 | 47 |