c# - BotFramework v4 .NET SDK:发送打字指示器无法正常工作
问题描述
使用 .NET SDK BotFramework v4。我正在尝试为机器人的每个答案添加一个打字指示器。
1)第一种方法:
var reply = MessageFactory.Attachment(attachments);
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Type = ActivityTypes.Typing;
await turnContext.SendActivityAsync(reply, cancellationToken);
也试过:
var typingMsg = stepContext.Context.Activity.CreateReply();
typingMsg.Type = ActivityTypes.Typing;
typingMsg.Text = "some text";
await stepContext.Context.SendActivityAsync(typingMsg);
但是对于两者来说,机器人只是用打字指示符来回答,没有文字,并且不停地循环发送它
2)第二种方法:我使用了ShowTypingMiddleware
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
CancellationTokenSource cts = null;
if (turnContext.Activity.Type == ActivityTypes.Message)
{
cts = new CancellationTokenSource();
cancellationToken.Register(() => cts.Cancel());
var task = Task.Run(() => SendTypingAsync(turnContext, _delay, _period, cts.Token), cancellationToken);
DialogContext dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
// Continue any current dialog.
DialogTurnResult dialogTurnResult = await dc.ContinueDialogAsync();
var conversationStateAccessors = ConversationState.CreateProperty<ConversationData>(nameof(ConversationData));
var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData());
if (conversationData != null)
{
var messageTimeOffset = (DateTimeOffset)turnContext.Activity.Timestamp;
var localMessageTime = messageTimeOffset.ToLocalTime();
conversationData.Timestamp = localMessageTime.ToString();
conversationData.ChannelId = turnContext.Activity.ChannelId.ToString();
}
var dialogsExist = dialogs.GetDialogs();
// First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
var recognizerResult = await _botServices.Dispatch.RecognizeAsync(turnContext, cancellationToken);
// Top intent tell us which cognitive service to use.
var (intent, _) = recognizerResult.GetTopScoringIntent();
// Next, we call the dispatcher with the top intent.
await DispatchToTopIntentAsync(turnContext, dc, intent, recognizerResult, cancellationToken);
}
if (cts != null)
{
cts.Cancel();
}
await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
}
键入指示器显示第一个答案,但随后再也不会出现下一个答案。
有什么解决办法?谢谢!
解决方案
我找到了解决方案。
我们应该使用dialogContext而不是turnContext。
在OnTurnAsync函数中,使用这个:
DialogContext dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
Activity replyTyping = turnContext.Activity.CreateReply();
replyTyping.Type = ActivityTypes.Typing;
await dc.Context.SendActivityAsync(replyTyping);
await Task.Delay(3000);
推荐阅读
- laravel - Laravel:为 json 字段/列定义规则或模式
- javascript - 无法在字符串上创建属性“键”
- java - 在 Java 中查找样条函数的导数
- dart - 为什么 cupertino_icons 中的图标很少?
- linux - 日期:无效日期'2018-10-21 +1天'
- android - 如何在 Android Studio 中创建新的 Android AAR
- amazon-web-services - 从 Beanstalk/EC2 Web 应用程序读取/写入 S3 图像
- css - 非静态页脚导航栏出现在移动设备屏幕底部上方
- ios - SpriteKit 中的 SKVideoNode 问题,模拟器仅显示灰屏
- java - java.lang.ClassNotFoundException:javax.ws.rs.ProcessingException