diff options
author | Valentin Popov <valentineus@gmail.com> | 2017-06-05 13:51:29 +0300 |
---|---|---|
committer | Valentin Popov <valentineus@gmail.com> | 2017-06-05 13:51:29 +0300 |
commit | a0780435720234c1b638fe9e8d7dd628dd824b30 (patch) | |
tree | 1d062aeef8c27ef2e2da25be5e08dd09b4b08a3f /example.js | |
download | iii-for-vk-a0780435720234c1b638fe9e8d7dd628dd824b30.tar.xz iii-for-vk-a0780435720234c1b638fe9e8d7dd628dd824b30.zip |
Release of version 0.0.1.
Diffstat (limited to 'example.js')
-rw-r--r-- | example.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/example.js b/example.js new file mode 100644 index 0000000..c90812d --- /dev/null +++ b/example.js @@ -0,0 +1,56 @@ +var ChatBot = require('./lib/core.js'); + +var bot = new ChatBot({ + appID: "id", // The application ID + appSecret: "secret", // Secret application key + uuid: "uuid", // Bot 's ID + token: "token" // The authorization key for the user profile +}); + +bot.init(function(cuid) { + // We view all events with messages + bot.on('messages', function(raw) { + // We receive the message by ID + bot.getMessageByID(raw[1], function(message) { + if (!message.out) { + answer({ + cuid: cuid, + message: message + }); + } + }); + }); +}); + +/** + * We respond to the user with a delay. + * @param {Object} options - Object with parameters. + * @param {Object} options.cuid - Session ID + * @param {Object} options.message - Incoming message. + */ +function answer(options) { + setTimeout(function() { + var text = options.message.body || ':-|'; + // Sending a message to the bot + bot.sendMessageToBot({ + cuid: options.cuid, + text: text + }, function(message) { + // Sending a response to the user + bot.sendMessageToVK({ + user_id: options.message.user_id, + message: message.text.tts + }); + }); + }, random(1000, 5000)); +} + +/** + * Returns a random number in the specified range. + * @param {Number} min - Minimum value. + * @param {Number} max - Maximum value. + * @returns {Number} - Random number. + */ +function random(min, max) { + return Math.floor(Math.random() * (max - min)) + min; +}
\ No newline at end of file |