-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickblox_service.js
67 lines (54 loc) · 1.74 KB
/
quickblox_service.js
1
2
3
4
5
6
7
8
9
10
11
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var QB = require("quickblox");
// QuickBloxService constructor.
var QuickBloxService = function(appId, authKey, authSecret, botUser, chatDialogId) {
this.appId = appId;
this.authKey = authKey;
this.authSecret = authSecret;
this.botUser = botUser;
this.chatDialogId = chatDialogId;
this.sessionExpirationTime = null;
var CONFIG = {
debug: {mode: 1} // set DEBUG mode
};
QB.init(appId, authKey, authSecret, CONFIG);
};
QuickBloxService.prototype.checkSession = function(callback){
if(!QB.service.qbInst.session || (new Date()).toISOString() > this.sessionExpirationTime){
var params = {login: this.botUser.login, password: this.botUser.password};
QB.createSession(params, function(err, result) {
if (typeof callback === 'function'){
callback(err);
}
});
}else{
if (typeof callback === 'function'){
callback(null);
}
}
}
QuickBloxService.prototype.buildMessage = function(feedEntry){
var message = "New activity: " + feedEntry.title +
". " + feedEntry.link; // feedEntry.categories.join(", ")
return message;
}
QuickBloxService.prototype.fire = function(data, successCallback, errorCallback) {
var self = this;
this.checkSession(function(err){
var params = {
chat_dialog_id: self.chatDialogId,
message: data,
send_to_chat: 1
};
QB.chat.message.create(params, function(err, res) {
self.sessionExpirationTime = (new Date()).toISOString();
if(err){
console.log("error sending QuickBlox API request: " + JSON.stringify(err));
errorCallback(err);
}else{
successCallback();
}
});
});
};
// Export the QuickBloxService constructor from this module.
module.exports = QuickBloxService;