aboutsummaryrefslogtreecommitdiff
path: root/example.js
blob: b09b0c3d5aa70033eb9131c7b408700c0b03d421 (plain) (blame)
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
var ChatBot = require('iii-for-vk');

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;
}