Ability to register to BotUserSession specific events


I would like to execute a JavaScript function (provided via the “Custom Script” file) whenever the user sends an utterance to the chatbot. I’m wondering if there’s a way to do so.

Something like
BotUserSession.addEventListener(“onUserMessage”, myCustomFunction);

Background: I expect relevant customData in the messagePayload and need to keep track of the data provided during the chat session.

Similarly, events like these would be great if provided:
BotUserSession.addEventListener(“onIntentStart”, someFunction); // passes intent details to someFunction
BotUserSession.addEventListener(“onIntentEnd”, someFunction); // passes intent details to someFunction
BotUserSession.addEventListener(“onNodeStart”, someFunction); // passes node details to someFunction
BotUserSession.addEventListener(“onNodeEnd”, someFunction); // passes node details to someFunction


With version 7.1. a new Event named “End of Conversation” was introduced. If I get it right, the event always fires when a bot task / intent ends. Is that correct @Subrahmanyam?
Could you similarly add an event “Start of Conversation” which would fire when a bot task / intent starts?

What would you want to do in the “Start of Conversation” event?
The dialog is started and so you already have a processing hook.

Hi Andy,
my intention is to preload entities on “Start of Conversation”, if the BotUserSession contains some specific custom user data. And it would be handy if I could do that without adding a custom Script node to each Dialog Task. As of now, I can only implement such a behaviour by directly adding a Script node after the Intent node.

That would be tricky because each task has its own context.entities that is spun up as an implicit part of starting a dialog. So that would mean that it wouldn’t exist for the (future) task when the start of conversation task is running.

Now if the need is to just seed entities from BotUserSession then we can look at other ways of achieving that.

Maybe I need to clarify further.

Use case 1: When communicating with the WebHook Channel, every request to the chatbot carries a customData property context.session.BotUserSession.lastMessage.messagePayload.customData.isAuthenticated. I would like to keep track of the previous and current value and check upon each “onNodeStart” whether the value did change. Let’s say an authenticated user suddenly gets unauthenticated, he should not be able to proceed the regular flow in the dialog task he’s currently executing.

Use case 2: During the chat session, I enter my credit card information in Dialog Task A. After finishing Dialog Task A, I enter Dialog Task B which also expects my credit card information. Here, I’d like to be able to reuse the credit card information I entered in Dialog Task A and persist that value for this particular entity during the whole chat session. Generally speaking I need a way to persist Entities during the whole chat session.

How would you approach the situations?

By “onNodeStart” then are you suggesting that every single node in the flow would check the authenticated state?
Surely the authenticated state would only needed to be done once per volley as several nodes may be executed per volley. The typical place to do that currently would be in the botkit tier.
In a bot that I’m working with we do an authentication check only after a service call - if the service returns a 401 then we transition to a relogin process.

To persist data throughout the entire chat session then use context.session.BotUserSession. Currently you would have to use script to manage that. But I do think that some scheme to default entity values would be a good feature.
Note that if Dialog B is invoked directly from Dialog A then it is possible to seed entity values via the preAssignments option.