|
| 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 | +); |
0 commit comments