Blame view

lib/transport.js 4.62 KB
c0741a574   Adhidarma Hadiwinoto   Completed
1
2
3
4
  "use strict";
  
  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
9
10
  
  const config = require('komodo-sdk/config');
  const logger = require('komodo-sdk/logger');
  
  const modems = require('./modems');
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

f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  async function _getApproriateHandlerByLastSeen(partnerNumber) {
      logger.verbose('Looking for last seen on for partner number ' + partnerNumber);
      const lastSeenFrom = await partnerLastSeen.get(partnerNumber);
      return lastSeenFrom;
  }
  
  function _getApproriateHandlerByForced() {
      if (!config.sending_handler || !config.sending_handler.length) return;
  
      const sendingHandlerCount = config.sending_handler.length;
      const idx = Math.floor(Math.random() * sendingHandlerCount);
      return config.sending_handler[idx];
  }
  
  async function _getApproriateHandler(partnerNumber, origin) {
      let handlerToUse;
  
      if (config.handler_chooser_algorithm === 'FORCED') {
          handlerToUse = _getApproriateHandlerByForced();
          logger.verbose('Config file mentioned to using FORCED handler chooser algorithm', { handler_to_use: handlerToUse});
      }
      else {
          handlerToUse = await _getApproriateHandlerByLastSeen(partnerNumber);
          logger.verbose('Config file mentioned to using LAST-SEEN handler chooser algorithm', { handler_to_use: handlerToUse});
      }
  
      if (!modems.getModemConfig(handlerToUse, config.modems)) {
          const handlerWithSameOrigin = modems.getModemConfig(origin, config.modems);
          if (handlerWithSameOrigin) {
              logger.verbose('Invalid approriate handler, using handler from the same ORIGIN request by CORE to send sms')
              handlerToUse = origin;
          }
          else {
              logger.verbose('Invalid approriate handler, using default handler to send sms')
              handlerToUse = config.default_modem;
          }
      }
  
      return handlerToUse;
  }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
54
  function _send(destinationNumber, msg, handlerName) {
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
55

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
56
      /*
c0741a574   Adhidarma Hadiwinoto   Completed
57
58
59
60
      if (msg.length > 160 && !config.do_not_trim_long_sms) {
          logger.verbose('Message trim to 160 chars');
          msg = msg.slice(0, 156) + ' ...';
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
61
      */
c0741a574   Adhidarma Hadiwinoto   Completed
62

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
63
64
65
      if (msg.length > 160) {
          const newMsg = msg.slice(0, 160);
          const remainingMsg = msg.slice(160);
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
66

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
67
68
69
70
71
72
73
          _send(destinationNumber, newMsg, handlerName);
          setTimeout(() => {
              _send(destinationNumber, remainingMsg, handlerName);
          }, 2000);
  
          return;
      }
c0741a574   Adhidarma Hadiwinoto   Completed
74
75
76
  
      const modem = modems.getModemConfig(handlerName, config.modems);
      if (!modem) {
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
77
          logger.warn('Not knowing modem to use. Ignoring message', { destination_number: destinationNumber, msg: msg, handler_name: handlerName });
c0741a574   Adhidarma Hadiwinoto   Completed
78
79
80
81
82
83
84
          return;
      }
  
      if (!modem.url || !modem.apikey) {
          logger.warn('Invalid modem configuration', { config: modem, handler_name: handlerName });
          return;
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
85
      const reqId = uuidv4();
4fbfae95f   Adhidarma Hadiwinoto   Messages history
86
87
88
89
90
91
92
93
94
      history.push({
          ts: moment().format('YYYY-MM-DD HH:mm:ss'), 
          modem: {
              name: handlerName,
          },
          direction: 'OUTGOING',
          partner: destinationNumber,
          message: msg,
      });
c0741a574   Adhidarma Hadiwinoto   Completed
95
96
97
98
99
100
101
102
103
      const requestOptions = {
          url: modem.url,
          qs: {
              msg: msg,
              number: destinationNumber,
              reqid: reqId,
              apikey: modem.apikey
          }
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
104
      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
105
106
107
108
109
110
111
112
113
114
115
116
      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 ...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  
  }
  
  async function send(partner, msg, origin) {
      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;
  
      const destinationNumber = modems.removeSuffixFromNumber(partner, config);
  
      logger.verbose('Choosing handler name', { partner: partner, msg: msg, origin: origin });
      let handlerName = ( await _getApproriateHandler(destinationNumber) );
  
      _send(destinationNumber, msg, handlerName);
c0741a574   Adhidarma Hadiwinoto   Completed
137
138
139
  }
  
  exports.send = send;