Blame view

lib/transport.js 3.21 KB
c0741a574   Adhidarma Hadiwinoto   Completed
1
  "use strict";
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
2
  const url = require('url');
c0741a574   Adhidarma Hadiwinoto   Completed
3
4
  const request = require('request');
  const uuidv4 = require('uuid/v4');
4fbfae95f   Adhidarma Hadiwinoto   Messages history
5
  const moment = require('moment');
c0741a574   Adhidarma Hadiwinoto   Completed
6
7
8
  
  const config = require('komodo-sdk/config');
  const logger = require('komodo-sdk/logger');
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
9
10
  const modemSelect = require('./modemSelect');
  const modems = require('./modems2');
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
11
  const partnerLastSeen = require('./partner-last-seen');
4fbfae95f   Adhidarma Hadiwinoto   Messages history
12
  const history = require('./history');
c0741a574   Adhidarma Hadiwinoto   Completed
13

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
14
  function _send(destinationNumber, msg, handlerName) {
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
15

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
16
17
18
      if (msg.length > 160) {
          const newMsg = msg.slice(0, 160);
          const remainingMsg = msg.slice(160);
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
19

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
20
21
22
23
24
25
26
          _send(destinationNumber, newMsg, handlerName);
          setTimeout(() => {
              _send(destinationNumber, remainingMsg, handlerName);
          }, 2000);
  
          return;
      }
c0741a574   Adhidarma Hadiwinoto   Completed
27

f43dbc16a   Adhidarma Hadiwinoto   New modem selector
28
      const modem = modems.get('name', handlerName);
c0741a574   Adhidarma Hadiwinoto   Completed
29
      if (!modem) {
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
30
          logger.warn('Not knowing modem to use. Ignoring message', { destination_number: destinationNumber, msg: msg, handler_name: handlerName });
c0741a574   Adhidarma Hadiwinoto   Completed
31
32
          return;
      }
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
33
34
      if (!modem.reportIp || !modem.reportPort || !modem.reportApikey) {
          logger.warn('Invalid modem configuration', { modem });
c0741a574   Adhidarma Hadiwinoto   Completed
35
36
          return;
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
37
      const reqId = uuidv4();
4fbfae95f   Adhidarma Hadiwinoto   Messages history
38
39
40
41
42
43
44
45
46
      history.push({
          ts: moment().format('YYYY-MM-DD HH:mm:ss'), 
          modem: {
              name: handlerName,
          },
          direction: 'OUTGOING',
          partner: destinationNumber,
          message: msg,
      });
c0741a574   Adhidarma Hadiwinoto   Completed
47
      const requestOptions = {
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
48
49
50
51
          url: url.format({
              protocol: 'http',
              hostname: modem.reportIp,
              port: modem.reportPort,
b6d23624a   Adhidarma Hadiwinoto   ModemData.reportP...
52
              pathname: modem.reportPathSms,
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
53
          }),
c0741a574   Adhidarma Hadiwinoto   Completed
54
55
56
57
          qs: {
              msg: msg,
              number: destinationNumber,
              reqid: reqId,
b6d23624a   Adhidarma Hadiwinoto   ModemData.reportP...
58
              apikey: modem.reportApikey,
c0741a574   Adhidarma Hadiwinoto   Completed
59
60
          }
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
61
      logger.info('Sending message to modem handler', { req_id: reqId, destination_number: destinationNumber, msg: msg, msg_length: msg.length, handler_name: handlerName });
c0741a574   Adhidarma Hadiwinoto   Completed
62
63
64
65
66
67
68
69
70
71
72
73
      request(requestOptions, function(err, res, body) {
          if (err) {
              logger.warn('Error requesting to modem handler. ' + err.toString(), { req_id: reqId, handler_name: handlerName });
              
          }
          else if (res.statusCode != 200) {
              logger.warn('Modem handler not responding with HTTP status code 200.', { http_status_code: res.statusCode, req_id: reqId, handler_name: handlerName });
          }
          else {
              logger.verbose('Message sent to handler', { req_id: reqId, handler_name: handlerName, response_body: body });
          }
      })
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
74
75
  
  }
62e23ff59   Adhidarma Hadiwinoto   Hapus parameter t...
76
  async function send(partner, msg) {
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
77
78
79
80
81
82
83
84
85
      if (!partner) return;
  
      if (typeof msg !== 'string') {
          logger.warn('Message to send is not a string, ignoring message');
          return;
      }
  
      msg = msg.trim();
      if (!msg) return;
f43dbc16a   Adhidarma Hadiwinoto   New modem selector
86
87
88
89
      const destinationNumber = modemSelect.removeSuffixFromNumber(partner, config);
  
      // logger.verbose('Choosing handler name', { partner, destinationNumber, msg, origin });
      let handlerName = await partnerLastSeen.get(destinationNumber) ;
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
90

f43dbc16a   Adhidarma Hadiwinoto   New modem selector
91
92
93
94
      if (!handlerName) {
          logger.warn(`Unknown handler for sending message to partner`, { partner, destinationNumber });
          return;
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
95
96
  
      _send(destinationNumber, msg, handlerName);
c0741a574   Adhidarma Hadiwinoto   Completed
97
98
99
  }
  
  exports.send = send;