BotFramework v4 .NET SDK:发送输入指示器无法正常工作

BotFramework v4 .NET SDK: send typing indicator doesn't work properly

使用 .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);