c# - 在发送第一条消息之前访问用户/对话状态
问题描述
在 Bot Framework V4 中,解释了可以通过在OnTurnAsync
var dc = await Dialogs.CreateContextAsync(turnContext);
或在对话上下文类中使用访问器
var state = await HogehogeSettingAccessor.GetAsync(stepContext.Context);
但是,在向对话框发送消息之前如何访问它们?
我目前正在开发 Directline API,并希望在发送第一条消息之前参考语言设置(例如,如果书面语言与设置不匹配,则忽略用户输入)。
private async Task OnMessageReceive(SocketMessage socketMessage)
{
if (IsLanguageMatch(socketMessage)){
await channel.SendMessageAsync(response);
}
}
我如何实现这一目标?
解决方案
您可以让 OnTurnAsync 仅在满足您的条件时启动第一个/主对话框。当用户第一次向机器人发送消息时,不会有任何活动的 Dialog。您可以利用该条件并在那里添加您的条件,只有在满足两者时才启动对话框:
// Getting the bot accessor state you want to use
LanguageAccessor languageAccessor = await _accessors.LanguageAccessor.GetAsync(turnContext, () => new LanguageAccessor(), cancellationToken);
// Every step sends a response. If no dialog is active, no response is sent and turnContext.Responded is null
//where turnContext.Activity.Text is the message sent by the user
if (!turnContext.Responded && ((languageAccessor.property)turnContext.Activity.Text))
OnTurnAsync 应如下所示:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
// Establish dialog state from the conversation state.
DialogContext dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
// Get the user's info.
LanguageAccessor languageAccessor = await _accessors.LanguageAccessor.GetAsync(turnContext, () => new LanguageAccessor(), cancellationToken);
await _accessors.UserInfoAccessor.SetAsync(turnContext, userInfo, cancellationToken);
// Continue any current dialog.
DialogTurnResult dialogTurnResult = await dc.ContinueDialogAsync();
// Every dialog step sends a response, so if no response was sent,
// then no dialog is currently active and the Else if is entered.
if (!turnContext.Responded && ((languageAccessor.property)turnContext.Activity.Text))
{
//This starts the MainDialog if there's no active dialog when the user sends a message
await dc.BeginDialogAsync(MainDialogId, null, cancellationToken);
}
//Else if the validation is not passed
else if (!turnContext.Responded && !
((languageAccessor.property)turnContext.Activity.Text))
{ await turnContext.SendActivityAsync("Thank you, see you next time"); }
}
}
另一种选择是将访问器对象发送到您要使用它的对话框,并使用第一个对话框的瀑布步骤,如果满足验证则继续对话框,否则结束它。
瀑布步骤应如下所示:
private async Task<DialogTurnResult> ValidationFirstStepAsync(
WaterfallStepContext stepContext,
CancellationToken cancellationToken = default(CancellationToken))
{
// Access the bot UserInfo accessor so it can be used to get state info.
LanguageAccessor languageAccessor = await
_accessors.LanguageAccessor.GetAsync(stepContext.Context, null,
cancellationToken);
if ((languageAccessor)stepContext.Context.Activity.Text)
{
await stepContext.Context.SendActivityAsync(
"Hi!");
return await stepContext.NextAsync();
}
else
{
await stepContext.Context.SendActivityAsync("Sorry, your language is not supported");
return await stepContext.EndDialogAsync(); }
}
}
推荐阅读
- python - 如何允许作者和管理员同时修补和删除权限?
- java - 用java spring处理不断增长的excel文件
- javascript - 具有空值的firebase推送方法是否仅用于获取ID,触发child_added事件?
- r - 使用 stringr 匹配所有换行符
- php - 如何制作 Laravel 动态数据表?
- python - 不能接受多个输入
- javascript - 如何自动取消静音
- amazon-web-services - AWS IAM:条件上下文键“aws:RequestTag”如何工作?
- javascript - 如何在 Element ui 表中对本地日期进行排序并重新格式化?
- java - 为什么 gmail 会显示使用 MimeMessageHelper addInline 与 Amazon SES 一起发送的图像的“无名”附件?