How to set typing indicator in azure bot
I work with c# making azure bot have web chat and teams channels. I want to make a typing indicator for the web chat user if the teams user is typing and another one for teams user if the web chat user typing. I can get it that if the bot talking to the teams user or web chat user than the last two get the typing indicator.
Azure AI Bot Service
-
navba-MSFT 19,480 Reputation points • Microsoft Employee
2024-05-31T03:58:36.8133333+00:00 @Riham Mushmush Welcome to Microsoft Q&A Forum, Thank you for posting your query here!
.
To set a typing indicator in an Azure bot using C#, you can use the
Activity.CreateTypingActivity()
method. Here’s a sample code snippet:.
public async override Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) { ITypingActivity replyActivity = Activity.CreateTypingActivity(); await turnContext.SendActivityAsync((Activity)replyActivity); await Task.Delay(5000); await base.OnTurnAsync(turnContext, cancellationToken); }
.
More info here.
The above code will send a typing activity to the user, wait for 5 seconds, and then continue with the rest of the bot’s activities.
.
However, customizing the typing indicator (like changing the typing symbol to a custom message) is not straightforward. For the WebChat channel, you might be able to modify the CSS to change the appearance of the typing indicator.
.
Please refer this thread: https://learn.microsoft.com/en-us/answers/questions/320951/how-to-add-customized-typing-indicator-using-c-in
https://stackoverflow.com/questions/62293920/bot-framework-net-add-typing-indicator-on-bot
Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.
-
navba-MSFT 19,480 Reputation points • Microsoft Employee
2024-06-03T08:59:57.1+00:00 @Riham Mushmush Just following up to check if my suggestion helped. Please let me know if you have any further queries. I would be happy to help.
-
Riham Mushmush 0 Reputation points
2024-06-04T10:33:37.28+00:00 Thanks for answering, but I meant that my bot has multiple channels and the typing indicator is sent if the bot is typing. Can the bot handle my typing activity? I want the users on other channels, like the web chat, to know that I am typing when I am actually writing in Teams, not just a fake timer typing indicator.
-
navba-MSFT 19,480 Reputation points • Microsoft Employee
2024-06-05T10:26:36.7266667+00:00 @Riham Mushmush Thanks for getting back and clarifying the ask.
.
To achieve the real-time typing indicator synchronization between users on different channels (Teams and Web Chat), you'll need to extend the bot's functionality to detect and propagate typing activities across channels. Here's a detailed approach to implement this:
.
- Detect Typing Activity:
- In Microsoft Teams, you can detect when a user is typing using the
typing
event. - For the Web Chat channel, detect the
typing
activity from the user.
- In Microsoft Teams, you can detect when a user is typing using the
- Broadcast Typing Activity:
- Once a typing activity is detected from a user on one channel, broadcast this activity to users on the other channels.
- You can use the
ActivityHandler.OnTypingActivityAsync
method in your bot to handle typing activities.
- Relay Typing Indicator:
- Send a typing activity to the other channel to indicate that a user is typing.
.
Here is a sample implementation in C#:
.
Step 1: Extend
OnTurnAsync
to Handle Typing Activitiespublic class TypingIndicatorBot : ActivityHandler { public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) { if (turnContext.Activity.Type == ActivityTypes.Typing) { // If a typing activity is detected, relay it to the other channel await HandleTypingActivityAsync(turnContext, cancellationToken); } else { await base.OnTurnAsync(turnContext, cancellationToken); } } private async Task HandleTypingActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) { var typingActivity = Activity.CreateTypingActivity(); typingActivity.From = turnContext.Activity.From; // Depending on the channel, relay the typing activity to the other channel if (turnContext.Activity.ChannelId == "msteams") { // Relay typing indicator to Web Chat users // You need to implement the logic to find the conversation reference for the Web Chat users await RelayTypingToWebChatUsers(typingActivity, cancellationToken); } else if (turnContext.Activity.ChannelId == "webchat") { // Relay typing indicator to Teams users // You need to implement the logic to find the conversation reference for the Teams users await RelayTypingToTeamsUsers(typingActivity, cancellationToken); } } private async Task RelayTypingToWebChatUsers(IActivity typingActivity, CancellationToken cancellationToken) { // Implement logic to send typing activity to Web Chat users // For example, you can use the conversation reference of the Web Chat users var conversationReference = ... // get the conversation reference for Web Chat users await turnContext.Adapter.ContinueConversationAsync( botAppId, conversationReference, async (context, token) => await context.SendActivityAsync((Activity)typingActivity), cancellationToken); } private async Task RelayTypingToTeamsUsers(IActivity typingActivity, CancellationToken cancellationToken) { // Implement logic to send typing activity to Teams users // For example, you can use the conversation reference of the Teams users var conversationReference = ... // get the conversation reference for Teams users await turnContext.Adapter.ContinueConversationAsync( botAppId, conversationReference, async (context, token) => await context.SendActivityAsync((Activity)typingActivity), cancellationToken); } }
.
Step 2: Handle Conversation References
To send activities across channels, you need to store and retrieve conversation references for users in different channels. You can use a database or an in-memory store to manage these references.
.
Step 3: Integrate with Bot Framework
Ensure your bot is properly configured to handle multiple channels and has the necessary permissions to send messages across these channels.
.
Hope this answers.
- Detect Typing Activity:
Sign in to comment