1

I am using an Azure Function to send a Proactive message to the client. How do i "reset" a conversation when a Proactive message is sent.

Within the bot, a user might be prompted for something (ex. time of day). A proactive message may get sent to them before they respond. In this scenario, I would like to reset/cancel the previous dialog and start fresh.

I am already able to reset the dialog using CancelAllDialogsAsync which works fine for user-driven messages.

I am sending my proactive message using ConnectorClient, which bypasses the framework, and sends directly to the client, thus never hitting my middleware to reset the dialog.

How can I get the proactive message sent to the framework (i can send the response from the bot no problem)

Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
NiteLordz
  • 597
  • 2
  • 18
  • Are you just trying to cancel a Waterfall dialog from a proactive message? If you can access your `dialogState` in the proactive message you should be able to create a `dialogContext` with the `turnContext` and then call `cancelAllDialogs` from the `dialogContext`. – tdurnford Jan 10 '19 at 01:45
  • Within the Azure Function, i have a ConversationReference. How would i go about creating the dialogContext from a ConversationReference. This is v4 just for clarity. – NiteLordz Jan 10 '19 at 01:51
  • I found a solution, just not sure if it's the best one. Following [this](https://stackoverflow.com/questions/51040452/send-message-to-bot-from-web-service) example, i am able to send a message to my bot, for the given user | conversation | channel, and then i can cancel the conversation accordingly. – NiteLordz Jan 10 '19 at 02:36
  • If you have the dialogState, you can create an instance of dialogSet where you can call createContext to get the dialogContext. You can then use that dialogContext to clear the dialog stack and cancel any active conversations. This process is very similar to how you'd typically get the dialogContext in a bot. The difficult part is getting the dialogState in your Azure Function which you might be able to get from the conversationState in Azure Blob Storage. – tdurnford Jan 10 '19 at 02:38

1 Answers1

1

I would highly recommend you solve this by having your function send your bot a backchannel event under the context of the ConversationReference via the ConnectorClient. This way the bot maintains ownership for all the details about state and what should happen when this event occurs rather than that responsibility leaking to the function. The bot then watches for this custom event and responds to it however it sees fit.

If you need any more details let me know and I'll update my answer.

Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
  • I have a poc working using a custom client sending a back channel event. How would I do this using the ConnectorClient. I couldn't find any method under the Conversations property that sent to the bot. All the ones I tested sent directly to the client. – NiteLordz Jan 10 '19 at 02:52
  • I'm away at the moment, but I believe you're looking for `connectorClient.Conversations.SendToConversationAsync` which will take the `Activity` that you construct based on the `ConversationReference`. – Drew Marsh Jan 10 '19 at 03:23
  • I wasn't able to get connectorClient.Conversations.SendToConversationAsync to work, but it is probably similar to what I am doing. get the access token, pass in the activity to the {host}/api/messages with Activity Type set to "event'. – NiteLordz Jan 11 '19 at 01:33
  • @NiteLordz hmm, not sure why it's not working, but keep in mind that u won't be able to post to `/api/messages` in a production environment where your bot's endpoint is secured by a MicrosoftAppId/Secret. At that point the bot runtime will be expecting a JWT token that's signed by the Bot Framework Service itself... something u wouldn't be able to supply. That's why u need to post to your bot _through_ DirectLine. An alternative, if u didn't want to post through DL, is to expose a web service endpoint (e.g. an MVC controller) and use the `BotAdapter::ContinueConversationAsync` API from there. – Drew Marsh Jan 13 '19 at 23:48