温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - BotFramework v4 .NET SDK: send typing indicator doesn't work properly
.net botframework bots c# chatbot

c# - BotFramework v4 .NET SDK:发送打字指示器无法正常工作

发布于 2020-04-15 11:47:55

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

键入指示符显示第一个辅助词,但以后不再显示。

有什么解决方案?谢谢!

查看更多

提问者
Allain Richter
被浏览
118
Allain Richter 2020-02-05 01:12

我找到了解决方案。

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