You have been tasked to add logic to your chatbot in order to prompt the user proactively when there is a price drop from the product that the user requested to monitor.Additionally, if there is a delay expected in the response, suggest the user to continue the conversation and notify the user when information is ready.In this process, the bot retrieves the conversation reference and calls the adapter method and turn context to send the proactive message.
Review the code snippet below and complete by choosing the methods used to send the proactive message.
(select two answer choices)
public async Task<IActionResult> Get() { foreach (var conversationReference in _conversationReferences.Values) { await ((BotAdapter)_adapter)..............................................(_appId, conversationReference, BotCallback, default(CancellationToken)); } return new ContentResult() { Content = "<html><body><h1>Proactive messages sent.</h1></body></html>", ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK, }; } private async Task ………………………………………...(ITurnContext turnContext, CancellationToken cancellationToken) { await turnContext.SendActivityAsync("proactive message"); }
Click on the arrows to vote for the correct answer
A. B. C. D.Correct Answers: B and C.
Option A is incorrect because GetConversationReference is used to retrieve the conversation reference from the activity.
<pre class="brush:java;">public async Task&lt;IActionResult&gt; Get()
{
foreach (var conversationReference in _conversationReferences.Values)
{
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
}
return new ContentResult()
{
Content = "&lt;html&gt;&lt;body&gt;&lt;h1&gt;Proactive messages sent.&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;",
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
};
}
private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync("proactive message");
}
</pre>
Option B is correct because ContinueConversationAsync and BotCallback methods are used to send the proactive message.
Here is the completed code.
Option C is correct because ContinueConversationAsync and BotCallback methods are used to send the proactive message.
Option D is incorrect because OnConversationUpdateActivityAsync is used when conversation update activity is received from the channel.
Reference:
To learn more about proactive messaging in bot framework, use the link given below:
To send a proactive message from a bot in response to a particular event, you need to have the conversation reference for the specific conversation you want to send the message to. The conversation reference can be obtained using the GetConversationReference
method, which takes in a ITurnContext
object representing the current turn of the conversation.
In the given code snippet, the Get
method retrieves the conversation references for all active conversations and sends a proactive message to each one of them using the BotAdapter
's ContinueConversationAsync
method, passing in the conversation reference, a callback method BotCallback
, and a CancellationToken
object.
The BotCallback
method is responsible for sending the actual proactive message to the user. In the given code, it simply sends a static "proactive message" using the SendActivityAsync
method of the ITurnContext
object passed in as a parameter.
Therefore, the correct answer choices to complete the code snippet are A. GetConversationReference
and B. ContinueConversationAsync
. Option A retrieves the conversation reference, and option B sends the proactive message using the BotAdapter
's ContinueConversationAsync
method.