Skip to content

Commit 33cc56c

Browse files
authored
Stream Management
Added plugin 'Stream Managment' for browser / node env.
1 parent b041b66 commit 33cc56c

File tree

16 files changed

+2471
-1049
lines changed

16 files changed

+2471
-1049
lines changed

quickblox.min.js

Lines changed: 1330 additions & 995 deletions
Large diffs are not rendered by default.

samples/chat/js/config.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
var QBApp = {
2-
appId: 28287,
3-
authKey: 'XydaWcf8OO9xhGT',
4-
authSecret: 'JZfqTspCvELAmnW'
2+
appId: 28287,
3+
authKey: 'XydaWcf8OO9xhGT',
4+
authSecret: 'JZfqTspCvELAmnW'
55
};
66

77
var config = {
8-
chatProtocol: {
9-
active: 2
10-
},
11-
debug: {
12-
mode: 1,
13-
file: null
14-
},
15-
stickerpipe: {
16-
elId: 'stickers_btn',
8+
chatProtocol: {
9+
active: 2
10+
},
11+
streamManagement: {
12+
enable: true
13+
},
14+
debug: {
15+
mode: 1,
16+
file: null
17+
},
18+
stickerpipe: {
19+
elId: 'stickers_btn',
20+
apiKey: '847b82c49db21ecec88c510e377b452c',
21+
enableEmojiTab: false,
22+
enableHistoryTab: true,
23+
enableStoreTab: true,
1724

18-
apiKey: '847b82c49db21ecec88c510e377b452c',
25+
userId: null,
1926

20-
enableEmojiTab: false,
21-
enableHistoryTab: true,
22-
enableStoreTab: true,
23-
24-
userId: null,
25-
26-
priceB: '0.99 $',
27-
priceC: '1.99 $'
28-
}
27+
priceB: '0.99 $',
28+
priceC: '1.99 $'
29+
}
2930
};
3031

3132
var QBUser1 = {
3233
id: 6729114,
3334
name: 'quickuser',
3435
login: 'chatusr11',
3536
pass: 'chatusr11'
36-
},
37+
},
3738
QBUser2 = {
3839
id: 6729119,
3940
name: 'bloxuser',

samples/chat/js/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ function connectToChat(user) {
5252

5353
// setup scroll events handler
5454
setupMsgScrollHandler();
55+
56+
setupStreamManagementListeners();
5557
}
5658
});
5759
}

samples/chat/js/dialogs.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ function updateDialogsList(dialogId, text){
146146

147147
// Choose dialog
148148
function triggerDialog(dialogId){
149-
console.info('Select a dialog with id: ' + dialogId + ', name: ' + dialogs[dialogId].name);
150-
151149
// deselect
152150
var kids = $('#dialogs-list').children();
153151
kids.removeClass('active').addClass('inactive');

samples/chat/js/messages.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function retrieveChatMessages(dialog, beforeDateSent){
145145
// sending messages after confirmation
146146
function clickSendMessage() {
147147
var currentText = $('#message_text').val().trim();
148-
148+
149149
if (!currentText.length){
150150
return;
151151
}
@@ -261,6 +261,14 @@ function setupIsTypingHandler() {
261261
});
262262
}
263263

264+
function setupStreamManagementListeners(){
265+
QB.chat.onSentMessageCallback = function(messageLost, messageSent){
266+
console.group('onSentMessageCallback');
267+
messageLost ? console.log('Message was lost', messageLost) : console.log('Message was sent successfully', messageSent)
268+
console.groupEnd();
269+
};
270+
}
271+
264272
// delete timer and send 'stop typing' status
265273
function isTypingTimeoutCallback() {
266274
isTypingTimerId = undefined;

samples/chat/js/ui_helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
function buildMessageHTML(messageText, messageSenderId, messageDateSent, attachmentFileId, messageId, status){
33
var messageAttach;
44
if(attachmentFileId){
5-
messageAttach = '<img src="https://api.quickblox.com/blobs/'+attachmentFileId+'/download.xml?token='+token+'" alt="attachment" class="attachments img-responsive" />';
5+
messageAttach = '<img src="' + QB.content.publicUrl(attachmentFileId) + '/' + '/download.xml?token='+token+'" alt="attachment" class="attachments img-responsive" />';
66
}
77

88
var isMessageSticker = stickerpipe.isSticker(messageText);
@@ -26,7 +26,7 @@ function buildMessageHTML(messageText, messageSenderId, messageDateSent, attachm
2626
}
2727

2828
var messageHtml =
29-
'<div class="list-group-item" id="'+messageId+'" onclick="clickToAddMsg('+"'"+messageId+"'"+')">'+
29+
'<div class="list-group-item" id="'+messageId+'">'+
3030
'<time datetime="'+messageDateSent+ '" class="pull-right">'
3131
+jQuery.timeago(messageDateSent)+
3232
'</time>'+
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'use strict';
2+
3+
/**
4+
* Before using you need to install universal_rivescript
5+
* by the command `npm i`
6+
*/
7+
8+
const CONFIGURATION = require('./config');
9+
const USER = CONFIGURATION.QBUser;
10+
const CONFIG = CONFIGURATION.config;
11+
const QBApp = CONFIGURATION.QBApp;
12+
const QB = require('../../../src/qbMain.js');
13+
const RiveScript = require('rivescript');
14+
15+
// Init RiveScript
16+
var riveScriptGenerator = new RiveScript();
17+
18+
function loadingDone(batch_num) {
19+
console.log(`[RiveScript] Batch #${batch_num} has finished loading!`);
20+
21+
riveScriptGenerator.sortReplies();
22+
}
23+
24+
function loadingError(batch_num, error) {
25+
console.log(`[RiveScript] Load the batch #${batch_num} is failed`, JSON.stringify(error));
26+
}
27+
28+
riveScriptGenerator.loadFile('replies.rive', loadingDone, loadingError);
29+
30+
process.on('exit', function () {
31+
console.log('The qbot is gone.');
32+
QB.chat.disconnect();
33+
});
34+
35+
/** Start fun */
36+
QB.init(QBApp.appId, QBApp.authKey, QBApp.authSecret, CONFIG);
37+
38+
var qbListeners = {
39+
onSubscribeListener: function (userId) {
40+
console.log(`[QB] onSubscribeListener. Subscribe from ${userId}`);
41+
42+
QB.chat.roster.confirm(userId, function() {
43+
console.log(`[QB] Confirm subscription from user ${userId}`);
44+
});
45+
},
46+
onSystemMessageListener: function (msg) {
47+
if(msg.extension.notification_type === '1'){
48+
console.log(`[QB] The user ${msg.userId} adds you to dialog`);
49+
50+
var roomJid = QB.chat.helpers.getRoomJidFromDialogId(msg.extension.dialog_id);
51+
52+
QB.chat.muc.join(roomJid);
53+
}
54+
},
55+
onMessageListener: function (userId, msg) {
56+
var answer;
57+
58+
if (msg.type == 'groupchat') {
59+
60+
// - skip own messages in the group chat, don't replay to them
61+
// - reply only when someone mentions you. For example: "@YourBotBestFriend how are you?"
62+
var mentionStartIndex = -1;
63+
var mentionPattern = '@' + USER.name;
64+
var mentionLength = mentionPattern.length;
65+
66+
if(msg.body){
67+
mentionStartIndex = msg.body.indexOf(mentionPattern);
68+
}
69+
70+
if(userId != USER.id && mentionStartIndex >= 0){
71+
// build a reply
72+
var realBody;
73+
74+
if(mentionStartIndex === 0 && msg.body.substring(mentionLength, mentionLength+1) == ' '){
75+
realBody = msg.body.substring(mentionLength + 1);
76+
}else{
77+
realBody = "What's up? I react only for commands like this: '@YourBotBestFriend <text>'";
78+
}
79+
80+
answer = {
81+
type: 'groupchat',
82+
body: riveScriptGenerator.reply(userId, realBody),
83+
extension: {
84+
save_to_history: 1
85+
}
86+
};
87+
88+
QB.chat.send(QB.chat.helpers.getRoomJidFromDialogId(msg.dialog_id), answer);
89+
}
90+
} else if (msg.type == 'chat') {
91+
if(msg.body){
92+
answer = {
93+
type: 'chat',
94+
body: riveScriptGenerator.reply(userId, msg.body),
95+
extension: {
96+
save_to_history: 1
97+
}
98+
};
99+
100+
QB.chat.send(userId, answer);
101+
}
102+
}
103+
}
104+
};
105+
106+
QB.createSession({
107+
login: USER.name,
108+
password: USER.pass
109+
}, (createSessionError, res) => {
110+
if(createSessionError) {
111+
console.error('[QB] createSession is failed', JSON.stringify(createSessionError));
112+
process.exit(1);
113+
}
114+
115+
QB.chat.connect({
116+
userId: USER.id,
117+
password: USER.pass
118+
}, (chatConnectError) => {
119+
if (chatConnectError) {
120+
console.log('[QB] chat.connect is failed', JSON.stringify(chatConnectError));
121+
process.exit(1);
122+
}
123+
124+
var getContacts = new Promise((res, rej) => {
125+
return QB.chat.roster.get((contacts) => {
126+
res(contacts);
127+
});
128+
});
129+
130+
131+
var dialogList = new Promise((res, rej) => {
132+
return QB.chat.dialog.list({type: 2},(dialogListError, dialogList) => {
133+
if(dialogListError){
134+
rej(dialogListError);
135+
}
136+
res(dialogList);
137+
});
138+
}).catch(err => {
139+
console.log('[QB] dialog.list is failed', JSON.stringify(err));
140+
process.exit(1);
141+
});
142+
143+
144+
Promise.all([getContacts, dialogList]).then((result) => {
145+
var contacts = result[0],
146+
dialogs = result[1];
147+
148+
// connect to all group chats.
149+
dialogs.items.forEach((dialog) => {
150+
QB.chat.muc.join(dialog.xmpp_room_jid);
151+
});
152+
});
153+
154+
QB.chat.onMessageListener = qbListeners.onMessageListener;
155+
QB.chat.onSubscribeListener = qbListeners.onSubscribeListener;
156+
QB.chat.onSystemMessageListener = qbListeners.onSystemMessageListener;
157+
158+
QB.chat.onSentMessageCallback = function (err, success) {
159+
if (err) {
160+
console.log('sendErrorCallback', err);
161+
} else {
162+
console.log('sendMessageSuccessCallback', success);
163+
}
164+
};
165+
});
166+
}
167+
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
module.exports = {
4+
QBApp: {
5+
appId: 28287,
6+
authKey: 'XydaWcf8OO9xhGT',
7+
authSecret: 'JZfqTspCvELAmnW'
8+
},
9+
config: {
10+
chatProtocol: {
11+
active: 2
12+
},
13+
streamManagement: {
14+
enable: true
15+
},
16+
debug: {
17+
mode: 0,
18+
file: null
19+
}
20+
},
21+
QBUser: {
22+
id: 19636368,
23+
name: 'qbotuser',
24+
login: 'qbotuser',
25+
pass: 'qbotuser1'
26+
}
27+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "universal_rivescript",
3+
"version": "0.0.1",
4+
"description": "www.rivescript.com enabled chat bot",
5+
"main": "universal_rivescript.js",
6+
"author": "QuickBlox team",
7+
"license": "BSD-2-Clause",
8+
"dependencies": {
9+
"rivescript": "^1.12.2"
10+
}
11+
}

0 commit comments

Comments
 (0)