chat-platform 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/chat-platform.js CHANGED
@@ -44,6 +44,7 @@ const ChatExpress = function(options) {
44
44
  transportDescription: null,
45
45
  chatIdKey: null,
46
46
  userIdKey: null,
47
+ messageIdKey: null,
47
48
  tsKey: null,
48
49
  debug: true,
49
50
  onStart: null,
@@ -84,7 +85,7 @@ const ChatExpress = function(options) {
84
85
  }
85
86
 
86
87
  function evaluateParam(payload, newKey, optionKey, chatServer) {
87
- var options = _this.options;
88
+ const options = _this.options;
88
89
  if (options[optionKey] != null) {
89
90
  if (_.isString(options[optionKey]) && newKey != options[optionKey]) {
90
91
  payload[newKey] = payload[options[optionKey]];
@@ -111,6 +112,7 @@ const ChatExpress = function(options) {
111
112
 
112
113
  evaluateParam(payload, 'chatId', 'chatIdKey', chatServer);
113
114
  evaluateParam(payload, 'userId', 'userIdKey', chatServer);
115
+ evaluateParam(payload, 'messageId', 'messageIdKey', chatServer);
114
116
  evaluateParam(payload, 'ts', 'tsKey', chatServer);
115
117
  evaluateParam(payload, 'type', 'type', chatServer);
116
118
  evaluateParam(payload, 'language', 'language', chatServer);
@@ -118,7 +120,7 @@ const ChatExpress = function(options) {
118
120
  // on removing this constraint (some chat context may be lost)
119
121
  payload.userId = !_.isEmpty(payload.userId) ? payload.userId : payload.chatId;
120
122
  // evaluate callbacks
121
- var callbacks = chatServer.getCallbacks();
123
+ const callbacks = chatServer.getCallbacks();
122
124
  _(['chatId', 'userId', 'ts', 'type', 'language', 'messageId']).each(function(callbackName) {
123
125
  if (_.isFunction(callbacks[callbackName])) {
124
126
  payload[callbackName] = callbacks[callbackName].call(chatServer, payload)
@@ -213,7 +215,8 @@ const ChatExpress = function(options) {
213
215
  }
214
216
 
215
217
  function inboundMessage(payload, chatServer) {
216
- var contextProvider = chatServer.getOptions().contextProvider;
218
+ const options = chatServer.getOptions();
219
+ const contextProvider = options.contextProvider;
217
220
  if (chatServer.isDebug()) {
218
221
  // eslint-disable-next-line no-console
219
222
  console.log(orange('-- INBOUND MESSAGE --'));
@@ -243,7 +246,8 @@ const ChatExpress = function(options) {
243
246
  messageId: parsedMessage.messageId,
244
247
  transport: parsedMessage.transport,
245
248
  language: parsedMessage.language,
246
- ts: parsedMessage.ts
249
+ ts: parsedMessage.ts,
250
+ chatbotId: options.chatbotId
247
251
  }),
248
252
  payload: {
249
253
  type: parsedMessage.type,
@@ -257,7 +261,12 @@ const ChatExpress = function(options) {
257
261
  return contextProvider.get(
258
262
  parsedMessage.chatId,
259
263
  parsedMessage.userId,
260
- { userId: this.originalMessage.userId, transport: this.originalMessage.transport, chatId: this.originalMessage.chatId }
264
+ {
265
+ userId: this.originalMessage.userId,
266
+ transport: this.originalMessage.transport,
267
+ chatId: this.originalMessage.chatId,
268
+ chatbotId: this.originalMessage.chatbotId
269
+ }
261
270
  );
262
271
  },
263
272
  api: function() {
@@ -280,7 +289,12 @@ const ChatExpress = function(options) {
280
289
  .then(() => when(contextProvider.getOrCreate(
281
290
  parsedMessage.chatId,
282
291
  parsedMessage.userId,
283
- { userId: parsedMessage.userId, transport: parsedMessage.transport, chatId: parsedMessage.chatId }
292
+ {
293
+ userId: parsedMessage.userId,
294
+ transport: parsedMessage.transport,
295
+ chatId: parsedMessage.chatId,
296
+ chatbotId: options.chatbotId
297
+ }
284
298
  )))
285
299
  .then(chatContext => when(chatContext.set({ language: parsedMessage.language, authorized: false, pending: false })))
286
300
  .then(function() {
@@ -424,9 +438,7 @@ const ChatExpress = function(options) {
424
438
  }
425
439
 
426
440
  // create empty promise
427
- let stack = new Promise(function(resolve) {
428
- resolve(message);
429
- });
441
+ let stack = new Promise(resolve => resolve(message));
430
442
  // check for chatId, if not present check for userId-chatId translator, otherwise fail
431
443
  stack = stack.then(message => {
432
444
  const { transport } = instanceOptions;
@@ -533,15 +545,21 @@ const ChatExpress = function(options) {
533
545
  });
534
546
  // finally
535
547
  return stack
548
+ .then(function(message) {
549
+ // move the sent payload to the conventional key "sentMessage", it's up to middleware to
550
+ // update there the messageId
551
+ message.sentMessage = message.payload;
552
+ delete message.payload;
553
+ return message;
554
+ })
536
555
  .catch(function(error) {
537
556
  // eslint-disable-next-line no-console
538
557
  console.log(red(error));
539
558
  if (chatServer != null) {
540
559
  chatServer.emit('error', error);
541
560
  }
542
- })
543
- .then(function(message) {
544
- return message;
561
+ // rethrow error so it can be caught by the sender node
562
+ throw error;
545
563
  });
546
564
  }
547
565
 
@@ -590,7 +608,7 @@ const ChatExpress = function(options) {
590
608
  }
591
609
  if (RED.httpNode._router.stack.length >= routesCount) {
592
610
  // eslint-disable-next-line no-console
593
- chatServer.error('improperly removed some routes, this will cause unexpected results and tricky bugs');
611
+ chatServer.warning('Improperly removed some routes from Express. This is normal when multiple bots are running in the same server.');
594
612
  }
595
613
  }
596
614
  }
@@ -783,6 +801,12 @@ const ChatExpress = function(options) {
783
801
  console.log(red(text));
784
802
  this.emit('error', text);
785
803
  };
804
+ this.warning = function(msg) {
805
+ var text = '[' + options.transport.toUpperCase() + '] ' + msg;
806
+ // eslint-disable-next-line no-console
807
+ console.log(red(text));
808
+ this.emit('warning', text);
809
+ };
786
810
  this.log = function(obj) {
787
811
  // eslint-disable-next-line no-console
788
812
  console.log(prettyjson.render(obj));
package/index.js CHANGED
@@ -1,30 +1,6 @@
1
- const fs = require('fs');
2
- const clc = require('cli-color');
3
- const orange = clc.xterm(214);
4
- const grey = clc.blackBright;
5
- const green = clc.greenBright;
6
- const red = clc.red;
7
-
8
1
  const ChatExpress = require('./chat-platform');
9
2
  const ContextProviders = require('./chat-context-factory');
10
3
  const ChatLog = require('./chat-log');
11
4
  const UniversalPlatform = require('./universal');
12
5
 
13
- const jsonPackage = fs.readFileSync(`${__dirname}/package.json`);
14
- let version;
15
- try {
16
- const pack= JSON.parse(jsonPackage);
17
- version = pack.version;
18
- } catch(e) {
19
- // eslint-disable-next-line no-console
20
- console.log(red('Unable to open node-red-contrib-chatbot/package.json'));
21
- }
22
-
23
- // eslint-disable-next-line no-console
24
- console.log(orange('Initializing chat-platform lib, you should see this only once'));
25
- // eslint-disable-next-line no-console
26
- console.log(grey('Running at: ' + __dirname) + ' Version: ' + green(version));
27
- // eslint-disable-next-line no-console
28
- console.log('');
29
-
30
6
  module.exports = { ChatExpress, ContextProviders, ChatLog, UniversalPlatform };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chat-platform",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Universal Chat Platform",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -4,12 +4,12 @@ const _storeUserIds = {};
4
4
 
5
5
  const isEmpty = value => value == null || value === '';
6
6
 
7
- function MemoryStore(chatId, userId, statics = {}) {
7
+ function MemoryStore(chatId, userId, statics = {}, warnings = false) {
8
8
  this.chatId = chatId != null ? String(chatId) : null;
9
9
  this.userId = userId != null ? String(userId) : null;
10
10
  // make sure userId is always a string
11
11
  this.statics = Object.assign({}, statics, { userId: statics.userId != null ? String(statics.userId) : undefined });
12
- if (_.isEmpty(statics)) {
12
+ if (warnings && _.isEmpty(statics)) {
13
13
  console.trace('Warning: empty statics vars')
14
14
  }
15
15
  return this;
@@ -171,13 +171,13 @@ _.extend(FileFactory.prototype, {
171
171
  });
172
172
 
173
173
  //function FileStore(defaults, file, statics = {}) {
174
- function FileStore(chatId, userId, statics = {}, params) {
174
+ function FileStore(chatId, userId, statics = {}, params, warnings = false) {
175
175
  this.userId = userId;
176
176
  this.chatId = chatId;
177
177
  this.params = params;
178
178
  // make sure userId is always a string
179
179
  this.statics = Object.assign({}, statics, { userId: statics.userId != null ? String(statics.userId) : undefined });
180
- if (_.isEmpty(statics)) {
180
+ if (warnings && _.isEmpty(statics)) {
181
181
  console.trace('Warning: empty statics vars')
182
182
  }
183
183
  return this;
@@ -13,12 +13,12 @@ const isEmpty = value => value == null || value === '';
13
13
 
14
14
 
15
15
 
16
- function SQLiteStore(chatId, userId, statics = {}) {
16
+ function SQLiteStore(chatId, userId, statics = {}, warnings = false) {
17
17
  this.chatId = chatId != null ? String(chatId) : null;
18
18
  this.userId = userId != null ? String(userId) : null;
19
19
  // make sure userId is always a string
20
20
  this.statics = Object.assign({}, statics, { userId: statics.userId != null ? String(statics.userId) : undefined });
21
- if (_.isEmpty(statics)) {
21
+ if (warnings && _.isEmpty(statics)) {
22
22
  console.trace('Warning: empty statics vars')
23
23
  }
24
24
  return this;