首页 > 解决方案 > 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);
    }

键入指示器显示第一个答案,但随后再也不会出现下一个答案。

有什么解决办法?谢谢!

标签: c#.netbotframeworkbotschatbot

解决方案


我找到了解决方案。

我们应该使用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);

推荐阅读