The most common bot use case is when a user initiates the interaction by chatting with the bot via their favorite channel. What if you wanted to have the bot contact the user based on some triggered event or lengthy job or an external event such as a state change in the system (for example, the pizza is ready to pick up)? The proactive bot template is designed to do just that!
The proactive bot scenario in the proactive template
The proactive bot template provides all the Azure resources you need to enable a very simple proactive scenario. The following diagram provides an overview of how triggered events work.
When you create a proactive bot with the Azure Bot Service, you will find these Azure resources in your resource group:
Azure Storage (used to create the queue)
Azure Bot Service (your bot) - Contains the logic that receives the message from user, adds the message with required properties (Recipient and the user’s message) to the Azure queue, and receives the triggers from Azure Function and sends back the message it received from trigger’s payload.
Azure Function App (a queueTrigger Azure Function) - Triggered whenever there is a message in the queue, and communicates to the Bot service via Direct Line. This function uses bot binding to send the message as part of the trigger’s payload. Our example function forwards the user’s message as is from the queue.
Everything is properly configured and ready to work.
Azure Bot Service: Receiving a message from the user and adding it to an Azure Storage Queue
Here’s the snippet of code that receives the message from the user, adds it to an Azure Storage Queue, and finally sends back an acknowledgment to the user. Notice that the message is wrapped in an object that contains all the information needed to send the message back to the user on the right channel (ResumptionCookie for C# and session.message.address for Node.js).
Triggering an Azure Function with the queue, and sending the message back to the user (Azure Functions)
After the message is added to the queue, the function is triggered. The message is then removed from the queue and sent back to the user. If you inspect the function’s configuration file, you will see that it contains an input binding of type “queueTrigger” and an output binding of type “bot”.
The functions.json configuration file looks like this.
Snippet of code within queueTrigger Azure Function
This template should give you the basic ideas about how to enable proactive scenarios for your bots. By leveraging the power of Azure Bot Service and Azure Functions, you can build complex systems really fast with fault-tolerant, independent pieces.