55 * by the command `npm i`
66 */
77
8- const CONFIG = require ( './config' ) . CONFIG ;
98const QB = require ( '../../../src/qbMain.js' ) ;
109const RiveScript = require ( 'rivescript' ) ;
1110
12- // Init RiveScript
11+ // In order to start develop you first Chat Bot you have to register new QuickBlox account and create your first application.
12+ // Go to https://quickblox.com/signup and get a QuickBlox account, then go to https://admin.quickblox.com/apps/new
13+ // and create your first application. Then put here Application ID, Authorization key, Authorization secret.
14+ //
15+ // also
16+ //
17+ // Go to Users module in QuickBlox dashboard
18+ // (e.g. <your_app_id>/service/users https://admin.quickblox.com/apps/<your_app_id>/service/users) and create
19+ // new user for you chat bot. Then put here user's ID, password, login and full name.
20+ //
21+ const CONFIG = {
22+ "appId" : "13318" ,
23+ "authKey" : "WzrAY7vrGmbgFfP" ,
24+ "authSecret" : "xS2uerEveGHmEun" ,
25+ "user" : {
26+ "id" : "2740296" ,
27+ "login" : "qbot" ,
28+ "password" : "mehdoh00" ,
29+ "fullname" : "qbot"
30+ }
31+ } ;
32+
33+ // Init RiveScript logic
1334var riveScriptGenerator = new RiveScript ( ) ;
1435
1536function loadingDone ( batch_num ) {
1637 console . log ( `[RiveScript] Batch #${ batch_num } has finished loading!` ) ;
17-
1838 riveScriptGenerator . sortReplies ( ) ;
1939}
2040
@@ -24,23 +44,21 @@ function loadingError(batch_num, error) {
2444
2545riveScriptGenerator . loadFile ( 'replies.rive' , loadingDone , loadingError ) ;
2646
27- process . on ( 'exit' , function ( ) {
28- console . log ( 'The qbot is gone.' ) ;
29- QB . chat . disconnect ( ) ;
30- } ) ;
3147
32-
33- /** Start fun */
48+ // Initialise QuickBlox
3449QB . init ( CONFIG . appId , CONFIG . authKey , CONFIG . authSecret ) ;
3550
3651var qbListeners = {
52+ // Contact list listener
3753 onSubscribeListener : function onSubscribeListener ( userId ) {
3854 console . log ( `[QB] onSubscribeListener. Subscribe from ${ userId } ` ) ;
3955
4056 QB . chat . roster . confirm ( userId , function ( ) {
4157 console . log ( `[QB] Confirm subscription from user ${ userId } ` ) ;
4258 } ) ;
4359 } ,
60+
61+ // System messages listener
4462 onSystemMessageListener : function onSystemMessageListener ( msg ) {
4563 if ( msg . extension . notification_type === '1' ) {
4664 console . log ( `[QB] The user ${ msg . userId } adds you to dialog` ) ;
@@ -50,9 +68,12 @@ var qbListeners = {
5068 QB . chat . muc . join ( roomJid ) ;
5169 }
5270 } ,
71+
72+ // Chat messages listener
5373 onMessageListener : function onMessageListener ( userId , msg ) {
5474 var answer ;
5575
76+ // process group chat messages
5677 if ( msg . type == 'groupchat' ) {
5778
5879 // - skip own messages in the group chat, don't replay to them
@@ -85,6 +106,8 @@ var qbListeners = {
85106
86107 QB . chat . send ( QB . chat . helpers . getRoomJidFromDialogId ( msg . dialog_id ) , answer ) ;
87108 }
109+
110+ // process 1-1 messages
88111 } else if ( msg . type == 'chat' ) {
89112 if ( msg . body ) {
90113 answer = {
@@ -101,40 +124,50 @@ var qbListeners = {
101124 }
102125 } ;
103126
127+ // Create QuickBlox session
104128QB . createSession ( {
105- login : CONFIG . user . name ,
106- password : CONFIG . user . password
129+ login : CONFIG . user . login ,
130+ password : CONFIG . user . password
107131} , ( createSessionError , res ) => {
108- if ( createSessionError ) {
109- console . error ( '[QB] createSession is failed' , JSON . stringify ( createSessionError ) ) ;
132+ if ( createSessionError ) {
133+ console . error ( '[QB] createSession is failed' , JSON . stringify ( createSessionError ) ) ;
134+ process . exit ( 1 ) ;
135+ }
136+
137+ // Connect to Real-Time Chat
138+ QB . chat . connect ( {
139+ userId : CONFIG . user . id ,
140+ password : CONFIG . user . password
141+ } , ( chatConnectError ) => {
142+ if ( chatConnectError ) {
143+ console . log ( '[QB] chat.connect is failed' , JSON . stringify ( chatConnectError ) ) ;
110144 process . exit ( 1 ) ;
111145 }
112146
113- QB . chat . connect ( {
114- userId : CONFIG . user . id ,
115- password : CONFIG . user . password
116- } , ( chatConnectError ) => {
117- if ( chatConnectError ) {
118- console . log ( '[QB] chat.connect is failed' , JSON . stringify ( chatConnectError ) ) ;
147+ console . log ( '[QB] bot is up and running' ) ;
148+
149+ // Retireve all group chats where bot is in occupants list.
150+ QB . chat . dialog . list ( { type : 2 } , ( dialogListError , dialogList ) => {
151+ if ( dialogListError ) {
152+ console . log ( '[QB] dialog.list is failed' , JSON . stringify ( dialogListError ) ) ;
119153 process . exit ( 1 ) ;
120154 }
121155
122- console . log ( '[QB] bot is up and running' ) ;
123-
124- QB . chat . dialog . list ( { type : 2 } , ( dialogListError , dialogList ) => {
125- if ( dialogListError ) {
126- console . log ( '[QB] dialog.list is failed' , JSON . stringify ( dialogListError ) ) ;
127- process . exit ( 1 ) ;
128- }
129-
130- dialogList . items . forEach ( ( dialog ) => {
131- QB . chat . muc . join ( dialog . xmpp_room_jid ) ;
132- } ) ;
156+ // Join bot's group chats
157+ dialogList . items . forEach ( ( dialog ) => {
158+ QB . chat . muc . join ( dialog . xmpp_room_jid ) ;
133159 } ) ;
134-
135- QB . chat . onMessageListener = qbListeners . onMessageListener ;
136- QB . chat . onSubscribeListener = qbListeners . onSubscribeListener ;
137- QB . chat . onSystemMessageListener = qbListeners . onSystemMessageListener ;
138160 } ) ;
139- }
161+
162+ // Add listeners
163+ QB . chat . onMessageListener = qbListeners . onMessageListener ;
164+ QB . chat . onSubscribeListener = qbListeners . onSubscribeListener ;
165+ QB . chat . onSystemMessageListener = qbListeners . onSystemMessageListener ;
166+ } ) ;
167+ }
140168) ;
169+
170+ process . on ( 'exit' , function ( ) {
171+ console . log ( 'The qbot is gone.' ) ;
172+ QB . chat . disconnect ( ) ;
173+ } ) ;
0 commit comments