Documentation

Powered by Algolia

Commands and responses

On Alan Platform there are lots of ways how you can define a user-given commands as well as the actions that should be taken should your app users say this command and what responses they will hear. Take a note that you can't define new command inside of the other command.

Intent

intent() is the basic and the most commonly used function to define a user-given command. To define it you'll need to specify a pattern of the user input and the action that should occur once the command is given (intent is matched).

intent('hello world', p => {
    p.play('Hello');
});

Your project can have many intents. Each intent will be evaluated separately when user gives a command, and each intent will have their own match score. In this example if the user will ask what is the weather - first intent will be matched over the second one (second intent has more words and user request is missing one of them). And vice versa - if the user will ask what is the weather today - second intent will be matched over the first one (it has more words that has matched).

intent('what is the weather', p => {
    p.play('The weather is a word');
});

intent('what is the weather today', p => {
    p.play('The weather today is great!')
});

This will be different in case if Alan will not be able to find a 100% match, to learn about other ways of writing patterns refer to Patterns section of Alan Platform documentation.

While writing intents you can also use different pre- and user-defined slots. For details on how to use different slot types, see Slots.

intent('Add $(NUMBER) pineapples to my order', p => {
    p.play('As you wish');
});

Play

play() is the predefined function that is used to respond back to the user or to send a command to the app.

Sending responses to the user

To give a response to the user we will send strings (patterns) to play() function.

intent('Speak to me', p => {
    p.play('Sure, what you want to speak about?');
});

You can send more than one pattern. In this case only one of them will be picked at random and played back to the user.

intent('Speak to me', p => {
    p.play('Sure, what you want to speak about?','Sorry, I am not interested in this');
});

You can also use slots in the response.

intent('Say $(W hello|amen)', p => {
    p.play(`${p.W.value}`);
});

To learn more about patterns and slots refer to Patterns section and Slots section.

Sending commands to the application

play() can be used to send commands to your app which has Alan Button integrated in it. To do so you will need to pass an arbitrary JSON to play() function.

intent('open menu', p => {
    p.play({"command": "navigate", "screen": "menu"});
    p.play('Opening menu');
});

intent('go back', p => {
    p.play({"command": "navigate", "screen": "back"});
});

Then in your app you will define Alan Button handler for commands received from Alan Platform script.

Question

question() is a special predefined function for user-given commands which has the lowest priority. It should be used to provide general guidance to the user.

question('what is this', p => {
    p.play('This is the generic dialog with me, Alan');
});

question('what can I do here', p => {
    p.play('You can ask me anything, and I will try to answer you');
});

You can use patterns and slots with question() function.

Due to having the lowest priority, if there is another command in your script with the same pattern - it will be matched and activated instead of a question().

question('what is this', p => {
    p.play('This is the generic dialog with me, Alan');
});

question('what can I do here', p => {
    p.play('You can ask me anything, and I will try to answer you');
});

intent('what is this', p => {
    p.play('This is an intent');
});

Reply

reply() is the predefined action function and can be used when you need to give the response back to the user without making other complex actions.

intent('hello world',
    reply('Hello'));

You can use patterns and slots the same way as for play().

intent('Say $(W hello|amen)',
    reply('$(W)'));

The uniqueness of reply() function is that you can define simple conversational flows with it. Such flows might contain more than just request-response pair. In this case you will add a special follow() function. This function will only be active after the parrent intent is matched. To learn more about this refer to Contexts section of Alan Platform documentation.

intent('How are you doing',
    reply('Fine, thank you. What about you?',
        follow('Terrible',
            reply('Sad to hear this')),
        follow('Good',
            reply('Nice to hear that'))
    )
);