Let's build a Pizza ordering bot!

Here we explain the implicit history and context management offered by Bot compiler out of the box. Just specify schema and leave the rest to Bot compiler.

Creating dialog tree and scaffolding code

Install the latest version of Bot compiler cli

npm i -g botc

Initialize an Bot compiler project and go to the directory

botc init -n pizza
cd pizza
code .

Install visual studio code plugin to get intellisense while editing Bot compiler file (bot.json)

https://marketplace.visualstudio.com/items?itemName=abhivijay96.Bot compiler-schema-intellisense

Add the following content to the bot.json file

Token here is the developer token of an agent created in dialogflow. You can leave it and not specify -d in botc build -d and manually create intents and entities in dialogflow and just specify them in this file and leave entities and utteranaces empty.

{
    "token": "Your_dialogflow_token",
    "intents": [
        {
            "name": "menu",
            "parameters": [],
            "utterances": [
                "what is the menu",
                "show me menu",
                "menu",
                "I want to see the menu"
            ],
            "response": {
                "value": "menu",
                "type": "mb"
            }
        },
        {
            "name": "order",
            "parameters": [
                {
                    "name": "size",
                    "type": "@size",
                    "isList": false
                },
                {
                    "name": "base",
                    "type": "@base",
                    "isList": false
                },
                {
                    "name": "toppings",
                    "type": "@toppings",
                    "isList": true
                }
            ],
            "utterances": [
                "order",
                "let me order",
                "I want a @size pizza with @toppings"
            ],
            "response": {
                "value": "order",
                "type": "mb"
            }
        },
        {
            "name": "status",
            "parameters": [],
            "utterances": [
                "what is the status of my order",
                " status",
                "what happened to my order"
            ],
            "response": {
                "type": "function",
                "value": "getOrdersStatus"
            }
        }
    ],
    "entities": [
        {
            "name": "size",
            "values": [
                "small",
                "medium",
                "large"
            ]
        },
        {
            "name": "base",
            "values": [
                "pan",
                "cheese burst",
                "thin"
            ]
        },
        {
            "name": "toppings",
            "values": [
                "onion",
                "mushrrom",
                "tomato"
            ]
        },
        {
            "name": "drink",
            "values": [
                "sprite",
                "coke",
                "fanta"
            ]
        }
    ],
    "subIntents": [
        {
            "name": "yes",
            "utterances": [
                "yes",
                "ya",
                "yup"
            ],
            "parameters": []
        },
        {
            "name": "no",
            "utterances": [
                "nah",
                "no",
                "Nope"
            ],
            "parameters": []
        },
        {
            "name": "breverage",
            "utterances": [
                "@drink",
                "I want @drink"
            ],
            "parameters": []
        }
    ],
    "microBots": [
        {
            "name": "order",
            "states": [
                {
                    "name": "start",
                    "response": {
                        "type": "text",
                        "value": "Ok, would you like a breverage along with your order?"
                    },
                    "transitions": [
                        {
                            "name": "yes",
                            "nextState": "question1"
                        },
                        {
                            "name": "no",
                            "nextState": "confirm"
                        },
                        {
                            "name": "string",
                            "reply": "I did not quite get that, do you want extra breverage?"
                        }
                    ]
                },
                {
                    "name": "question1",
                    "response": {
                        "type": "text",
                        "value": "Sure, Which breverage do you want?"
                    },
                    "transitions": [
                        {
                            "name": "breverage",
                            "nextState": "confirm"
                        }
                    ]
                },
                {
                    "transitions": [

                    ],
                    "name": "confirm",
                    "response": {
                        "type": "function",
                        "value": "uploadOrder"
                    }
                }
            ]
        },
        {
            "name": "menu",
            "states": [
                {
                    "name": "start",
                    "response": {
                        "type": "text",
                        "value": "We have pizzas, toppings available are onion, mushroom, tomato. Would you like to see what breverages we have?"
                    },
                    "transitions": [
                        {
                            "name": "yes",
                            "nextState": "showDrinks"
                        },
                        {
                            "name": "no",
                            "reply": "Okay"
                        },
                        {
                            "name": "string",
                            "reply": "I did not quite get that, do you want to see breverage menu?"
                        }
                    ]
                },
                {
                    "transitions": [],
                    "name": "showDrinks",
                    "response": {
                        "value": "Fanta, coke, sprite",
                        "type": "text"
                    }
                }
            ]
        }
    ]
}

Above JSON is an Bot compiler schema used to build a pizza ordering and order status tracking bot. Here is a GIF of the working bot.

Execute

botc build -d

More about cli here.

This creates a directory structure as shown below

├── bot.json
├── bot.js
├── conversations.js
├── data.js
├── functions.json
├── index.js
├── Machines
│   ├── Machines.txt
│   ├── menuBot.js
│   ├── orderBot.js
│   └── RootDialog.js
├── menuBotImpl.js
├── msft.js
├── orderBotImpl.js
├── package.json
├── package-lock.json
├── rootImpl.js
├── store.json
└── Utils
├── logger.js
└── reply.js

Execute

npm install

Replace <PUT YOUR DIALOGFLOW APP ID HERE> in msft.js file with client access token of the dialogflow agent.

Replace function in rootImpl.js file to

More about what are the other things that can be done using replyCallback here

getOrdersStatus: function(uuid, store, replyCallback) {
        replyCallback('text', 'Here are your orders ....');
        conversations.done(uuid);
        // Delete the above lines and implement your own function 
}

Replace function in orderBotImpl.js file to

uploadOrder: function(uuid, store, replyCallback) {
        replyCallback('text', 'Your order has been recorded with size as ' + store['size'] + 
        ' toppings as ' + store['toppings'] + ' base as ' + store['base']);
        conversations.done(uuid);
        // Delete the above lines and implement your own function 
}

Comment line #4 in uitls/logger.js to keep the console neat while chatting

Execute

node msft.js

DEMO

You will be able to interact with the bot as shown below.

Here Bot compiler supports multi initiative conversation out of the box. User was able to see the menu and then decide what beverage to order. No extra code was required to handle this.

Let's breakdown the schema which will help you get started quickly in Bot compiler

Sections in the JSON root:

  • Intents: These are the main triggering intents which strike a conversation with your bot.
    • Response: This is the action to be taken when this particular intent is dispatched to the state machine. Various types of response can be found here.
  • Entities: Parameters to be collected with the intents.
  • SubIntents: These are the intents which are useful in the subsequent states of different state machines (micro bots) in the bot, but not involved in the triggering intents.
  • MicroBots: Different microbots defined in this bot.

State and context management

Here the main bot calls Order microbot and Menu microbot respectively when the event is Order or Menu. These microbots continue the conversation once invoked. Here in the example GIF, order microbot is invoked in response to the order intent and this microbot asks if the user wants a beverage, at this points user decides to see the menu and says 'menu', notice that menu microbot was invoked as a response to this and once the user finished seeing the menu and then said, "yes give me sprite", order microbot took over the conversation and finished the order. This way history of conversation and context management is implicitly managed by Bot compiler. Just specify the dialog tree in the JSON format and leave the rest to Bot compiler. Bot compiler internally maintains a history of the states and microbots. When an even not expected by the current state but is caused by a intent or subintent is dispatched, history is traversed and the most recent state expecting the current event is brought into the foreground more about this in Conversation Tree.

Pizza Bot compiler explained:

Here we have 4 triggering intents and 4 entities. There are 3 triggering intents (menu, order and status). Response for menu intent is the microbot menu, response type for order is microbot order, response type for status is a function.

Main Bot with triggering intents

Order MicroBot

results matching ""

    No results matching ""