Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing middleware not working as expected in teams as well as web chat channel #6697

Closed
deeptishashidharaiah opened this issue Oct 9, 2023 · 14 comments · Fixed by #6743
Closed
Assignees
Labels
Bot Services Required for internal Azure reporting. Do not delete. Do not change color. bug Indicates an unexpected problem or an unintended behavior. customer-replied-to Indicates that the team has replied to the issue reported by the customer. Do not delete. customer-reported Issue is created by anyone that is not a collaborator in the repository.

Comments

@deeptishashidharaiah
Copy link

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

What package version of the SDK are you using.
->4.21.0

Describe the bug

Give a clear and concise description of what the bug is.
->The latest version 'typing' middle ware is only able to send "typing" for first response and its missing in all other subsequent responses for a single question in bot framework.
Earlier we were using 4.18.0, and it was working as expected as in typing was thrown before all responses.

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

Give a clear and concise description of what you expected to happen.
->It should be able to send "typing" for first response and as well all other subsequent responses

Screenshots

If applicable, add screenshots to help explain your problem.
-> attached video example

Additional context

Add any other context about the problem here.
Recording 2023-10-09 161456.zip

@deeptishashidharaiah deeptishashidharaiah added bug Indicates an unexpected problem or an unintended behavior. needs-triage The issue has just been created and it has not been reviewed by the team. labels Oct 9, 2023
@deeptishashidharaiah deeptishashidharaiah changed the title Typing middleware not working as expected in all teams as well web chat channel Typing middleware not working as expected in teams as well as web chat channel Oct 9, 2023
@ramfattah ramfattah self-assigned this Oct 10, 2023
@ramfattah ramfattah added customer-reported Issue is created by anyone that is not a collaborator in the repository. Bot Services Required for internal Azure reporting. Do not delete. Do not change color. and removed needs-triage The issue has just been created and it has not been reviewed by the team. labels Oct 10, 2023
@ramfattah
Copy link
Contributor

Thanks @deeptishashidharaiah, I'm investigating this issue.

@ramfattah
Copy link
Contributor

ramfattah commented Oct 12, 2023

Hi @deeptishashidharaiah,

I'm not able to reproduce this issue.

Environment:

Sample: 57.teams-conversation-bot
BotBuilder v4.21.0
Channel: Teams

TypingActivityMiddlewareRecording.mov

If the bot answers very fast, the "typing..." activity might show only for a very short time. Sometimes, it might be so quick that you don't see it.

The 'typing' middleware in my testing bot works like this:

  1. When the bot gets a message from a user, I force the bot to wait for short time.
  2. While waiting, it shows a "typing..." message to the user.
  3. It keeps showing "typing..." until the bot gives its answer.

Sharing my code below.

TypingMiddleware.cs:

This code listens for user messages. When it gets a message, it shows the "typing..." message.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace Microsoft.BotBuilderSamples.Middlewares
{
    public class TypingMiddleware : IMiddleware
    {
        public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // send typing activity
                var typingActivity = Activity.CreateTypingActivity();
                await turnContext.SendActivityAsync(typingActivity, cancellationToken);
            }

            await next(cancellationToken);
        }
    }
}

AdapterWithErrorHandler.cs:

This is where I add the 'typing' middleware to the bot. So the bot knows it should show "typing" activity when it gets a message.

using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.BotBuilderSamples.Middlewares;
using Microsoft.Extensions.Logging;

namespace Microsoft.BotBuilderSamples
{
    public class AdapterWithErrorHandler : CloudAdapter
    {
        public AdapterWithErrorHandler(BotFrameworkAuthentication auth, ILogger<IBotFrameworkHttpAdapter> logger)
            : base(auth, logger)
        {
            Use(new TypingMiddleware());
            OnTurnError = async (turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                // NOTE: In production environment, you should consider logging this to
                // Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
                // to add telemetry capture to your bot.
                logger.LogError($"Exception caught : {exception.Message}");

                // Send a catch-all apology to the user.
                await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");

                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    }
}

TeamsActivityHandler:

This is bot's main code. When it gets a message, it waits for 1 second (await Task.Delay(1000);) and then sends back an answer.

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            await Task.Delay(1000);
            var userMessage = turnContext.Activity.Text;
            var echo = $"You said: {userMessage}";
            await turnContext.SendActivityAsync(MessageFactory.Text(echo, echo), cancellationToken);
        }

@deeptishashidharaiah
Copy link
Author

@ramfattah
The issue is not happening with first reply in a series of replies. It happened when suppose user asks a question and we have multiple responses for that question, bot will try to send them all one by one and there could be delay between each response and we wanted typing.. to work there before each response is sent to user. Is this scenario working at your end and not just for a single response for a question. Also, I can see you are using TypingMiddleware class and we are trying to use ShowTypingMiddleware class. Is ShowTypinMiddleware no more a viable class to use then?

@ramfattah
Copy link
Contributor

ramfattah commented Oct 17, 2023

Hi @deeptishashidharaiah, thanks for the update.

1. Clarification on Middleware:

The "TypingMiddleware" is a custom middleware class I created for demonstration purposes. It's not part of the official Bot Framework SDK. The SDK provides a built-in middleware called "ShowTypingMiddleware" that sends repeated "typing..." indicators at regular intervals until the bot sends its actual response.

2. Handling Multiple Responses:

For your specific use case where you want to send multiple bot responses with "typing..." activities in between, it's more effective to manually send the "typing..." activity within the OnMessageActivityAsync handler. This approach gives you more control and flexibility over when and how the "typing..." indicators appear.

TypingActivity.mov

Here's a code snippet for reference:

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
    await Task.Delay(1000);
    await turnContext.SendActivityAsync(Activity.CreateTypingActivity(), cancellationToken);
    await Task.Delay(2000);
    var userMessage = turnContext.Activity.Text;
    var echo = $"You said: {userMessage}";
    await turnContext.SendActivityAsync(MessageFactory.Text(echo, echo), cancellationToken);

    await Task.Delay(3000);
    await turnContext.SendActivityAsync(Activity.CreateTypingActivity(), cancellationToken);
    await Task.Delay(2000);
    var secondEcho = $"Second Echo: You said: {userMessage}";
    await turnContext.SendActivityAsync(MessageFactory.Text(secondEcho, secondEcho), cancellationToken);
}

With this approach, you don't need to use the custom TypingMiddleware I shared earlier.

I hope this clears things up. Please let me know if you have further questions or if there's anything else I can assist with.

@deeptishashidharaiah
Copy link
Author

Thanks for your response and suggestions.

  1. ShowTypingMiddleware worked for us in version 4.18.0. it's the latest version that has the problem. so just want to clear it off if its regression in the latest version.
  2. The suggested approach will still send typing only once but we would like to send it repeatedly till next response is sent and delay between each response is not fixed in our bot. So we want existing middleware to work as it did before. this will helps other SDK users as well and avoid redundant work.

Hope to know what's the change that introduced between these versions.

@ramfattah
Copy link
Contributor

ramfattah commented Oct 18, 2023

Thanks @deeptishashidharaiah, Interesting, from my end I used version 4.21.0 to do the tests.

are you able to share a minimal reproducible .zip bot?

@deeptishashidharaiah
Copy link
Author

please give it a try here: https://polarisbot-test.microsoft.com/

@ramfattah
Copy link
Contributor

Thanks @deeptishashidharaiah, I was referring to the bot code.

are you able to share a zipped file of your bot that I could test from my end? If so, please be sure to remove any secrets/keys beforehand.

@deeptishashidharaiah
Copy link
Author

deeptishashidharaiah commented Oct 18, 2023

Cant share entire code here but please reach out to me on teams/outlook, will arrange a call
Deeptish@microsoft.com

sharing small snippet hete
image

@ramfattah ramfattah added the customer-replied-to Indicates that the team has replied to the issue reported by the customer. Do not delete. label Oct 19, 2023
@deeptishashidharaiah
Copy link
Author

@ramfattah any update?

@deeptishashidharaiah
Copy link
Author

@ramfattah gentle ping! please update

@ramfattah
Copy link
Contributor

CC @stevkan @dmvtech

@ramfattah ramfattah removed their assignment Nov 7, 2023
@deeptishashidharaiah
Copy link
Author

@stevkan and @dmvtech any update?

@tracyboehrer
Copy link
Member

@ceciliaavila Can your team help with this? Related to the "New Teams" issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bot Services Required for internal Azure reporting. Do not delete. Do not change color. bug Indicates an unexpected problem or an unintended behavior. customer-replied-to Indicates that the team has replied to the issue reported by the customer. Do not delete. customer-reported Issue is created by anyone that is not a collaborator in the repository.
Projects
None yet
4 participants