Botkit: How variables works

Created on 23 Jun 2017  Â·  6Comments  Â·  Source: howdyai/botkit

Hi everyone,
I'd like to know how exactly variable works, I mean, I declare a vars inside a controller.hears this var is shared for every user or is different for each? And outside of a controller hears?
I'm new with event-driven programming and it's vars scope.

help wanted

All 6 comments

Are you talking about the variables feature of conversations? Docs

Variables are stored _on the conversation object_ in convo.vars. Set them using convo.setVar('key', value). Then use them in your conversations with Mustache templates:
convo.say('Okay, you selected {{vars.user_selection}} ')

You can load these at any time, before a conversation initializes, after, in the middle of a response handler. A common example is loading a user's profile info from FB and making it available to the convo object before the conversation begins. So you can greet the user like 'Hello, {{vars.first_name}}!' or grabbing other dynamic data you want to use in your templates.

Here is a simple example using setVar:

controller.hears(['^example vars$', '^ev$'], 'message_received', function(bot, message) {
    bot.startConversation(message, function(err, convo) {
        // Add some messages to our default thread 
        convo.addMessage('Welcome to this example conversation using variables!', 'default')
        convo.addMessage('Here is an undefined variable now, {{vars.doesNotExist}}!', 'default')
        convo.addMessage('If it is not defined yet, it just wont show up', 'default')

        // Vars exists before any variables do
        console.log('Empty convo.vars:', convo.vars)

        // Add a question, to the deafult thread, with a response handler that will set a variable
        // and then switch us to another thread
        convo.addQuestion('What is your favorite color?', function(response, convo) {
            convo.setVar('color', response.text)
            // now the convo.vars field has been updated
            console.log('convo.vars:', convo.vars)
            convo.gotoThread('step2')
        },  {key: 'response'}, 'default')

        // Since this variable is defined after our response handler runs,
        // it is available throughout our conversation after that point
        convo.addMessage('Ok, here is a variable again: {{vars.color}}', 'step2')
    })
})

@jonchurch thanks but it's not my question...
I mean sth like
```javascript
controller.hears(['counter'], 'message_received', function(bot, message) {
var sum = 0;
bot.reply("counter: "+(++sum));
bot.reply("counter: "+(++sum));
bot.reply("counter: "+(++sum));
}
´´´
could be that some user will get an unexpected results if 2 or more users say counter at the same time?

Here sum is defined in the scope of the callback, so no, two users talking
to the bot simultaneously will not affect the sum.
On Sun, Jun 25, 2017 at 3:13 PM AdanTL notifications@github.com wrote:

@jonchurch https://github.com/jonchurch thanks but it's not my
question...
I mean sth like

controller.hears(['counter'], 'message_received', function(bot, message) {
var sum = 0;
bot.reply("counter: "+(++sum));
bot.reply("counter: "+(++sum));
bot.reply("counter: "+(++sum));
}
´´´
could be that some user will get an unexpected results if 2 or more users say counter at the same time?

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/howdyai/botkit/issues/896#issuecomment-310921937, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AMUR27sbNvdAWVbrjd9fVuM54TOMaeXmks5sHrFkgaJpZM4ODdXk
.

While we're on this topic, does the variable system exist to keep dynamic data consistent per-user?

Here, for example, is a dynamic conversation that does not use the variable system. Would this break for multiple users?

controller.hears('personal question', 'message_received', function(bot, message) {
    bot.startConversation(message, async function(err, convo) {
        const userQuestion = await getUserInfo(message.channel); // dynamically question for this user
        const userAck = await getUserInfo(message.channel); // dynamically load answer for this user

        convo.addQuestion(userQuestion, function(response, convo) {
            convo.say(userAck);
            convo.next();
    })
})

(This is more of a "docs" lay-up question since others may arrive here asking the same thing)

seconding this as well, the bot I currently made can't handle more than one user. When another one enters, the content of the chat are overwritten

I've stumbled on the same problem, they should really handle the multiple user scenario. convo.vars Object changes when multiple users are present. One user data maybe accessible for another. You should reopen this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iworkforthem picture iworkforthem  Â·  3Comments

liornaar picture liornaar  Â·  3Comments

abinashmohanty picture abinashmohanty  Â·  4Comments

matueranet picture matueranet  Â·  4Comments

stelio picture stelio  Â·  4Comments