Blame view

lib/transport.js 4.32 KB
c0741a574   Adhidarma Hadiwinoto   Completed
1
2
3
4
5
6
7
8
9
  "use strict";
  
  const request = require('request');
  const uuidv4 = require('uuid/v4');
  
  const config = require('komodo-sdk/config');
  const logger = require('komodo-sdk/logger');
  
  const modems = require('./modems');
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
10
  const partnerLastSeen = require('./partner-last-seen');
c0741a574   Adhidarma Hadiwinoto   Completed
11

f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
12
13
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
  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 ...
52
  function _send(destinationNumber, msg, handlerName) {
f53fe083f   Adhidarma Hadiwinoto   Sender chooser algo
53

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
54
      /*
c0741a574   Adhidarma Hadiwinoto   Completed
55
56
57
58
      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 ...
59
      */
c0741a574   Adhidarma Hadiwinoto   Completed
60

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

077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
65
66
67
68
69
70
71
          _send(destinationNumber, newMsg, handlerName);
          setTimeout(() => {
              _send(destinationNumber, remainingMsg, handlerName);
          }, 2000);
  
          return;
      }
c0741a574   Adhidarma Hadiwinoto   Completed
72
73
74
  
      const modem = modems.getModemConfig(handlerName, config.modems);
      if (!modem) {
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
75
          logger.warn('Not knowing modem to use. Ignoring message', { destination_number: destinationNumber, msg: msg, handler_name: handlerName });
c0741a574   Adhidarma Hadiwinoto   Completed
76
77
78
79
80
81
82
          return;
      }
  
      if (!modem.url || !modem.apikey) {
          logger.warn('Invalid modem configuration', { config: modem, handler_name: handlerName });
          return;
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
83
      const reqId = uuidv4();
c0741a574   Adhidarma Hadiwinoto   Completed
84
85
86
87
88
89
90
91
92
      const requestOptions = {
          url: modem.url,
          qs: {
              msg: msg,
              number: destinationNumber,
              reqid: reqId,
              apikey: modem.apikey
          }
      }
077f3b0aa   Adhidarma Hadiwinoto   Support long sms ...
93
      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
94
95
96
97
98
99
100
101
102
103
104
105
      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 ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  
  }
  
  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
126
127
128
  }
  
  exports.send = send;