lib/index.js   A
last analyzed

Complexity

Total Complexity 14
Complexity/F 1.56

Size

Lines of Code 248
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
nc 16
dl 0
loc 248
rs 10
c 2
b 0
f 0
wmc 14
mnd 2
bc 15
fnc 9
bpm 1.6666
cpm 1.5555
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ HSBot 0 4 1
A HSBot.getChatHistory 0 4 1
A HSBot.getUserAnalysis 0 5 1
A HSBot.getAllUserChatHistory 0 4 1
A HSBot.transformAndReply 0 67 3
1
/**
2
 * hsBot: Supply template and your bot is ready.
3
 * 
4
 * Hardik Shah <[email protected]>
5
 * 
6
 * MIT License
7
 */
8
9
'use strict';
10
11
var ChatDB = require('../db/chatdb');
12
var analysis = new(require('../analysis'))();
13
var userDB = new (require('../db/userdb'))();
14
var common = require('../util/common');
15
16
/**
17
 * A collection of `HSBOT` functions for talk with bot and analyze data.
18
 * Supply template and your bot is ready.
19
 * @name HSBOT
20
 * @constructor
21
 * @param {Array} topicList - A collection of JSON object.
22
 * @param {Array} topics - A collection of JSON object.
23
 * @example
24
 * 
25
 * var topicList = require('../db/data/topicList.json'); // Your file path as per defined template.
26
 * var topics = require('../db/data/topics.json'); // Your file path as per defined template.
27
 * const HSBot = require('hsbot');
28
 * const hsBot = new HSBot(topicList, topics);
29
 */
30
function HSBot(topicList, topics) {
31
  this.topicList = topicList;
32
  this.topics = topics;
33
}
34
35
/**
36
 * This method is responsible to reply.
37
 * @name transformAndReply
38
 * @method
39
 * @param {String} userId - User's ID.
40
 * @param {String} userName - User name.
41
 * @param {String} pattern - A human text or user query.
42
 * @return {callback} error-first callback, bot result based on user query.
43
 * @example
44
 * var topicList = require('../db/data/topicList.json'); // Your file path as per defined template.
45
 * var topics = require('../db/data/topics.json'); // Your file path as per defined template.
46
 * const HSBot = require('hsbot');
47
 * const hsBot = new HSBot(topicList, topics);
48
 * var userId = "aQ11zyTr4u7I";
49
 * var userName;
50
 *
51
 * hsBot.transformAndReply(userId, userName, human_text, function(err, data){
52
 *   console.log("HSBot:", data);
53
 * });
54
 */
55
56
HSBot.prototype.transformAndReply = function(userId, userName, pattern, cb) {
57
  var topicList = this.topicList;
58
  var topics = this.topics;
59
  var chatDB = new ChatDB(topicList, topics);
60
  pattern = common.string2literal(pattern);
61
62
  if(!userId){
63
    cb("UserId is not defined.");
64
  } else {
65
    var user = userDB._findUser(userId);
66
    var response;
67
    if(!user){
68
      response = welcomeUser();
69
      cb(null, response);
70
    } else {
71
      response = transform();
72
      cb(null, response);
73
    }
74
  }
75
76
  function welcomeUser() {
77
    var template = createUser();
78
    var postStringDict = {
79
      template: template,
80
      userName: userName
81
    };
82
    return _postTransform(postStringDict);
83
  }
84
85
  function createUser(){
86
    var topicName = chatDB._findDefaultTopic();
87
    var topicFlow  = chatDB.getTopicFlow(topicName);
88
    var templateObj  = chatDB.getTemplateObj(userName, topicFlow);
89
  
90
    userDB._insertUser(userId, userName, templateObj.pattern, templateObj.template, topicName);
91
    return templateObj.template;
92
  }
93
94
  function transform() {
95
    var lastIndex = user.activities.length -1;
96
    var preQ = user.activities[lastIndex].preQ;
97
98
    var topicName = pattern? (chatDB._findTopicByCommand(pattern) || user.activities[lastIndex].topic) : chatDB._findDefaultTopic();
99
    var topicFlow = chatDB.getTopicFlow(topicName);
100
    var templateObj = chatDB.getTemplateObj(user.userName, topicFlow, pattern, preQ);
101
  
102
    if(templateObj){
103
      userDB._updateUser(userId, user.userName || pattern, templateObj.pattern, templateObj.template, topicName);
104
    } else{
105
      templateObj = { template: chatDB.getDefaultMessage() };
106
    }
107
108
    var postStringDict = {
109
      template: templateObj.template,
110
      query: pattern,
111
      userName: user.userName || pattern
112
    };
113
114
    return _postTransform(postStringDict);
115
  }
116
117
  function _postTransform(resObj) {
118
    var s = resObj.template;
119
    s = s.replace(/[*]/g, resObj.query).replace("<userName>", resObj.userName);
120
    return s;
121
  }
122
};
123
124
/**
125
 * This method will generate user analysis based on him activities.
126
 * @name getUserAnalysis
127
 * @method
128
 * @param {String} userId - User's ID.
129
 * @returns {JSON} Analysis of aggregate of user activities
130
 * @example
131
 * var topicList = require('../db/data/topicList.json'); // Your file path as per defined template.
132
 * var topics = require('../db/data/topics.json'); // Your file path as per defined template.
133
 * const HSBot = require('hsbot');
134
 * const hsBot = new HSBot(topicList, topics);
135
 * var userId = "aQ11zyTr4u7I";
136
 *
137
 * hsBot.getUserAnalysis(userId);
138
 * 
139
 * @example
140
 * {
141
 *  "timeSpent": "100261",
142
 *  "frequentBotText": "Thank you for contacting us. you can call me by typing @hs anytime for further help.",
143
 *  "frequentUserText": "@hs"
144
 * }
145
 */
146
HSBot.prototype.getUserAnalysis = function(userId) {
147
  var user = userDB._findUser(userId);
148
  var analysisData = analysis._analyzed(user);
149
  return analysisData;
150
};
151
152
/**
153
 * This method is responsible to get user details based on userId.
154
 * @name getChatHistory
155
 * @method
156
 * @param {String} userId - User's ID.
157
 * @example
158
 * var topicList = require('../db/data/topicList.json'); // Your file path as per defined template.
159
 * var topics = require('../db/data/topics.json'); // Your file path as per defined template.
160
 * const HSBot = require('hsbot');
161
 * const hsBot = new HSBot(topicList, topics);
162
 * var userId = "aQ11zyTr4u7I";
163
 *
164
 * hsBot.getChatHistory(userId);
165
 * 
166
 * @returns {JSON} user object.
167
 * @example
168
 * {
169
 *   userName: "Hardik Shah",
170
 *   userId: "aQ11zyTr4u7I",
171
 *   loggedIn: 1235637,
172
 *   activities: [
173
 *    {
174
 *      pattern: null,
175
 *      preQ: "What is your name?",
176
 *      ut: 12345688,
177
 *      topic: "call1"
178
 *    },
179
 *    {
180
 *     pattern: "Hardik Shah",
181
 *     preQ: "Welcome Hardik Shah, How can I assist you?",
182
 *     ut: 12345999,
183
 *     topic: "call1"
184
 *   }
185
 *  ]
186
 * }
187
 */
188
HSBot.prototype.getChatHistory = function(userId) {
189
  var user = userDB._findUser(userId);
190
  return user;
191
};
192
193
/**
194
 * This method is responsible to get all user details.
195
 * @name getAllUserChatHistory
196
 * @method
197
 * @example
198
 * var topicList = require('../db/data/topicList.json'); // Your file path as per defined template.
199
 * var topics = require('../db/data/topics.json'); // Your file path as per defined template.
200
 * const HSBot = require('hsbot');
201
 * const hsBot = new HSBot(topicList, topics);
202
 * var userId = "aQ11zyTr4u7I";
203
 *
204
 * hsBot.getChatHistory(userId);
205
 * 
206
 * @returns {Array} Array of all user details.
207
 * 
208
 * @example
209
 * [
210
 *   {
211
 *     userName: "Hardik Shah",
212
 *     userId: "aQ11zyTr4u7I",
213
 *     loggedIn: 1235637,
214
 *     activities: [
215
 *      {
216
 *        pattern: null,
217
 *        preQ: "What is your name?",
218
 *        ut: 12345688,
219
 *        topic: "call1"
220
 *      },
221
 *      {
222
 *        pattern: "Hardik Shah",
223
 *        preQ: "Welcome Hardik Shah, How can I assist you?",
224
 *        ut: 12345999,
225
 *        topic: "call1"
226
 *      }
227
 *     ]
228
 *   },
229
 *   {
230
 *     userName: "Kartik Shah",
231
 *     userId: "aQ11zyTr4u22",
232
 *     loggedIn: 1235637,
233
 *     activities: [
234
 *      {
235
 *        pattern: null,
236
 *        preQ: "What is your name?",
237
 *        ut: 12345688,
238
 *        topic: "call1"
239
 *      },
240
 *      {
241
 *        pattern: "Kartik Shah",
242
 *        preQ: "Welcome Kartik Shah, How can I assist you?",
243
 *        ut: 12345999,
244
 *        topic: "call1"
245
 *      }
246
 *     ]
247
 *   }
248
 * ]
249
 */
250
HSBot.prototype.getAllUserChatHistory = function() {
251
  var userList = userDB.mem;
252
  return userList;
253
};
254
255
module.exports = HSBot;
256
module.exports.version = require('../package.json').version;