Since the TRIGGER block can be used to trigger specific events for any user of the chatbot, we can use it to send two-way chatbot notifications to other users or even perform some background tasks like affiliate tracking. Let’s look into a couple of simple examples.
Live chat with the chatbot admin
There is a number of use cases when one chatbot user would like to send a message to another. Think of match-making chatbots, for example, or a delivery service chatbot where couriers would like to send updates to customers.
We’ll look into a simple example, using a TRIGGER block to establish two-way communication between different chatbot users – a regular user and a chatbot admin.
When a chatbot user wants to send a message to the admin (asking a question, for example), we’ll do a number of simple tasks:
- Check if admin is available (not engaged in conversation with another chatbot user already). If he’s not, we’ll send a message to the user and wait for a couple of minutes to try again.
- If the admin is available, we’ll forward him/her the message from the chatbot user and wait for the response. Once admin replies, we’ll forward the response to the original user.
- check_admin (triggered on admin’s side) – this skill will check if admin is available
- admin_delay (triggered on user’s side) – this skill will display the “Admin busy…” message to the user and wait before re-trying the check again
- ask_admin (triggered on admin’s side) – this skill will send the user’s message to the admin and wait for admin’s response
- admin_answer (triggered on user’s side) – this skill will display the admin’s reply to the user.
We’ll use $admin_state attribute to track the current state of the chatbot admin – it will be either “busy” (if admin is unavailable or busy with another conversation) or “free”.
Initiating the conversation
When the user wants to send a message to the chatbot admin, we’ll just start the “check_admin” skill for the user with admin’s ID:
Notice that the TRIGGER block allows us to send a number of parameters together with the “check_admin” event. These parameters will become attributes for the admin user, and we’ll be using it to send the user’s name, message, and ID.
Once this part is executed, the chatbot is set to idle for the user sending the message, but some action starts happening for the admin. Let’s look into it in detail.
Checking the status of the admin user
First of all, we need to check if the admin is already answering a message from another user (admin can only be talking to a single user at the moment). We’ll check the value of $admin_state attribute and if it’s not “free”, we’ll send back the “admin_delay” event to originating user, starting a delay skill for him/her.
Otherwise, we’ll proceed to the “ask_admin” skill to wait for the admin’s response to the user’s question.
Notice the use of the $uid attribute – it was set by the TRIGGER block above and holds the messenger ID of the user who sent the message to the admin.
Delaying the delivery if admin is busy
If the admin is already communicating with another chatbot user, we’ll notify the user about it and re-try sending the message in a couple of minutes. This is done in the “admin_delay” skill triggered on the previous step.
Forwarding the message to the admin and waiting for the response
Once the admin is available, we’ll forward the user’s message to him/her and wait for the response, and then deliver that response back to the originating user.
We start this skill by setting the $admin_state attribute to “busy” so that if other users send messages to the admin while this skill is running, these messages will not interrupt it (due to the logic we implemented in the “check_admin” skill). Then we display a TEXT block, using attributes acquired from the TRIGGER block:
A question from $fname $lname ($uid):This is how the message is composed
Type your answer below.
$fname, $lname, $uid, and $message attributes were set by the TRIGGER block from the initiating user in the “default” skill before:
The result displayed to the admin will be something like this:
Once the message is displayed to the admin, the bot will use the LISTEN block to receive the response, and then trigger the “admin_answer” skill for the originating user to display that response.
Finally, we’ll set $admin_state back to “free” to make this skill available for other chatbot users.
Displaying the response to the user
Finally, let’s see how these two-way chatbot notifications can be displayed back to the user who started the conversation.
We’re using some attributes here (set by the TRIGGER block from the admin’s side) to let the user know the name of the admin and what his/her response was.
So, with less than 30 blocks we managed to build a powerful two-way chatbot notifications mechanics, allowing any chatbot user to send questions to the admin and receive his/her answers.
With some imagination, you can easily upgrade this to allow even more features:
- multiple admins, assigned randomly or by user tags
- expanding the conversation to multiple messages from each side
- storing the conversation history in the Google Sheets
The sky is the limit with Activechat, so explore your options today!