Articles on: Crisp Chatbot

How to prioritize a chatbot scenario over another?

The chatbot Bot plugin is a great tool to use as an auto responder for common questions your visitor may have. Although, you may have noticed that if you have pattern match scenarios for specific words and a wildcard pattern match for any word (eg. *), the wildcard pattern match always overrides other pattern match scenarios. This article will show you how to prioritise pattern matches, so that the wildcard pattern is only triggered when no other match condition is met.

1. Create a blank scenario



Firstly, create a blank scenario and give it a meaningful name, such as Priority Bot. This scenario will be used to create the template of your multiple pattern match scenario.

Add a new Event Block directly under the entry gate and select the event type User Message Matches. Then in the message patterns, select Add a message pattern and lets add a placeholder such as: ENTER MESSAGE PATTERN HERE, you will change this later.

Select Apply Changes once you are done, your event block should look like this:

Example of first Event Block

Add an Exit Scenario block and select Run Scenario. You will now need to select any existing scenario for the time-being, you will also change this later. It will look like this:

Exit scenario for the bot

If you do not already have a second scenario created, create a new scenario and give it a name such as SELECT SCENARIO.

Using exit scenarios is better, rather than adding all message blocks in one scenario, as this will help keep the scenario neat and easier to follow!

2. Export Scenario & Edit file in code editor



Export & open the scenario



To create more pattern match event blocks, directly after the entry gate, you will need to edit the .json file. You will need a code editor to do this. Download the bot file, by clicking Export Scenario, found on the bottom left of the edit bot scenario page. Save the file to your computer and open it in your favourite code editor.

Many code editors have extensions to make the .json files easier to read. For example, in Visual Studio Code there is an extension called 'Prettier - Code formatter'.

Edit the JSON file



The next step is easy, all you will need to do is copy & paste existing code within the .json file. A JSON object consists of name/value pairs, where the value of a pair may also be another object. Furthermore, multiple objects used as a pair value, can also be placed inside an array, you will be duplicating and adding some of these objects in order to add new event blocks and run scenarios into your bot. Firstly, locate the "draft" value within the "flow" name.



You might see a "live" value within the "flow" name if you had deployed your bot. Just be sure to make all changes within the "draft" value to ensure you see the changes in the Crisp Bot Plugin.

Once you have located the "draft", you will need to highlight the code inside the "children" name, found underneath "type: entry" and copy it. Then you need to paste it directly underneath the last } from the text you copied. You will also need to add a comma after the last curly brace to look like this },. Save your file, your code should look like this:

Copy the code in the red box and paste it underneath, matching the green box

In the appendix of this article, you can copy the code and paste it in your .json file. It is the same code as the image above.

3. Re-upload you Scenario



Once you have edited the .json file, open your bot plugin in Crisp and delete the existing "Priority Bot" that you created in step one. Then upload the new "Priority Bot" scenario you just edited:

Upload Scenario

If everything is ok, you will see a green banner at the bottom of your browser saying 'Success'. Open the scenario you have just imported, it should now look like this:



As you can see the bot you have edited looks unusual to your ordinary bot, since there are two event blocks directly after the entry gate. Now you will need to edit the event blocks to match a message pattern. The patterns will be checked in order, where the left event block will run first to check if there are any message matches, if there are none, only then will the event block on the right run.

If you were to create a Pricing Bot, which also has a fallback for all message patterns, your event blocks may look like this:

Example Scenario

You will then need to create new scenarios for the pricing message pattern to run if the message matches the word pricing, and a seperate new scenario for the * pattern to run if the word pricing is not included in the first message. Your bot should now work for a message pattern, as well as a wildcard message pattern!

If you would like to add more message patterns, jump back to step 2 and repeat the instructions. Just be sure to paste the block above the * message pattern block, to ensure the wildcard scenario only runs if no other message patterns match.

Note: You can only create up to 20 message pattern event blocks, including the wildcard event block!

Appendix



Example code:



{
    "name": "Priority Bot",
    "enabled": false,
    "flow": {
        "draft": {
            "children": [
                {
                    "type": "entry",
                    "children": [
                        {
                            "type": "event",
                            "contents": [
                                {
                                    "type": "message-match",
                                    "settings": {
                                        "patterns": [
                                            "ENTER MESSAGE PATTERN HERE"
                                        ]
                                    }
                                }
                            ],
                            "children": [
                                {
                                    "type": "exit",
                                    "contents": [
                                        {
                                            "type": "scenario",
                                            "settings": {
                                                "scenario": "SELECT SCENARIO"
                                            }
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "event",
                            "contents": [
                                {
                                    "type": "message-match",
                                    "settings": {
                                        "patterns": [
                                            "*"
                                        ]
                                    }
                                }
                            ],
                            "children": [
                                {
                                    "type": "exit",
                                    "contents": [
                                        {
                                            "type": "scenario",
                                            "settings": {
                                                "scenario": "SELECT SCENARIO"
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

Updated on: 13/06/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!