-
Notifications
You must be signed in to change notification settings - Fork 16
Update quoted replies & new quotes features (next/core) #390
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
Open
corinagum
wants to merge
15
commits into
next/core
Choose a base branch
from
cg/core-quoted-replies
base: next/core
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b11c677
Remove manipulations to ReplyToId
924fed2
Implement quoted replies features
8ad9b09
Add QuotedReplies sample
667c6ca
Move ActivityQuotedReplyExtention to its own file
967d7d9
Add Quoted Replies sample to slnx
784fcbf
Add README.md to sample
a0abc27
Add XML doc for Time field format (IC3 epoch)
0c08f20
Refactor ReplyAsync to delegate to QuoteReplyAsync and gate entity on…
341ccbb
Move stamping to model layer
598b9d6
Update to Quote with verbiage improvements
corinagum 87059ea
Rename sample
corinagum 9955fef
Remove experiemental from reply - reply is pre-existing
corinagum 1e7aed7
Add further protections to MessageActivity type for WithQuote
corinagum 8fc0526
Narrowed AddQuote() from TeamsActivity to MessageActivity to prevent …
corinagum eed46fd
Remove duplicate WithRecipient overload
corinagum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Teams.Bot.Apps; | ||
| using Microsoft.Teams.Bot.Apps.Handlers; | ||
| using Microsoft.Teams.Bot.Apps.Schema; | ||
| using Microsoft.Teams.Bot.Apps.Schema.Entities; | ||
|
|
||
| WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args); | ||
| webAppBuilder.Services.AddTeamsBotApplication(); | ||
| WebApplication webApp = webAppBuilder.Build(); | ||
|
|
||
| TeamsBotApplication teamsApp = webApp.UseTeamsBotApplication(); | ||
|
|
||
| teamsApp.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| var activity = context.Activity; | ||
| var text = activity.Text?.ToLowerInvariant()?.Trim() ?? ""; | ||
|
|
||
| // Read inbound quoted replies | ||
| var quotes = activity.GetQuotedMessages().ToList(); | ||
| if (quotes.Count > 0) | ||
| { | ||
| var quote = quotes[0].QuotedReply; | ||
| var info = $"Quoted message ID: {quote?.MessageId}"; | ||
| if (quote?.SenderName != null) info += $"\nFrom: {quote.SenderName}"; | ||
| if (quote?.Preview != null) info += $"\nPreview: \"{quote.Preview}\""; | ||
| if (quote?.IsReplyDeleted == true) info += "\n(deleted)"; | ||
| if (quote?.ValidatedMessageReference == true) info += "\n(validated)"; | ||
|
|
||
| await context.SendActivityAsync( | ||
| new MessageActivity($"You sent a message with a quoted reply:\n\n{info}") { TextFormat = TextFormats.Markdown }, | ||
| cancellationToken); | ||
| return; | ||
| } | ||
|
|
||
| // ReplyAsync() — auto-quotes the inbound message | ||
| if (text.Contains("test reply")) | ||
| { | ||
| await context.ReplyAsync("Thanks for your message! This reply auto-quotes it.", cancellationToken); | ||
| return; | ||
| } | ||
|
|
||
| // QuoteAsync() — quote a previously sent message by ID | ||
| if (text.Contains("test quote")) | ||
| { | ||
| var sent = await context.SendActivityAsync("The meeting has been moved to 3 PM tomorrow.", cancellationToken); | ||
| if (sent?.Id != null) | ||
| { | ||
| await context.QuoteAsync(sent.Id, "Just to confirm — does the new time work for everyone?", cancellationToken); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // AddQuote() extension — builder with response | ||
| if (text.Contains("test add")) | ||
| { | ||
| var sent = await context.SendActivityAsync("Please review the latest PR before end of day.", cancellationToken); | ||
| if (sent?.Id != null) | ||
| { | ||
| MessageActivity msg = new(); | ||
| msg.AddQuote(sent.Id, "Done! Left my comments on the PR."); | ||
| await context.SendActivityAsync(msg, cancellationToken); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Multi-quote with mixed responses | ||
| if (text.Contains("test multi")) | ||
| { | ||
| var sentA = await context.SendActivityAsync("We need to update the API docs before launch.", cancellationToken); | ||
| var sentB = await context.SendActivityAsync("The design mockups are ready for review.", cancellationToken); | ||
| var sentC = await context.SendActivityAsync("CI pipeline is green on main.", cancellationToken); | ||
|
|
||
| if (sentA?.Id != null && sentB?.Id != null && sentC?.Id != null) | ||
| { | ||
| MessageActivity msg = new(); | ||
| msg.AddQuote(sentA.Id, "I can take the docs — will have a draft by Thursday."); | ||
| msg.AddQuote(sentB.Id, "Looks great, approved!"); | ||
| msg.AddQuote(sentC.Id); | ||
| await context.SendActivityAsync(msg, cancellationToken); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Builder pattern — WithQuote on TeamsActivityBuilder | ||
| if (text.Contains("test builder")) | ||
| { | ||
| var sent = await context.SendActivityAsync("Deployment to staging is complete.", cancellationToken); | ||
| if (sent?.Id != null) | ||
| { | ||
| TeamsActivity reply = TeamsActivity.CreateBuilder() | ||
| .WithType(TeamsActivityType.Message) | ||
| .WithQuote(sent.Id, "Verified — all smoke tests passing.") | ||
| .Build(); | ||
| await context.SendActivityAsync(reply, cancellationToken); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Help / Default | ||
| await context.SendActivityAsync( | ||
| new MessageActivity( | ||
| "**Quoting Test Bot**\n\n" + | ||
| "**Commands:**\n" + | ||
| "- `test reply` - ReplyAsync() auto-quotes your message\n" + | ||
| "- `test quote` - QuoteAsync() quotes a previously sent message\n" + | ||
| "- `test add` - AddQuote() extension with response\n" + | ||
| "- `test multi` - Multi-quote with mixed responses\n" + | ||
| "- `test builder` - WithQuote() on TeamsActivityBuilder\n\n" + | ||
| "Quote any message to me to see the parsed metadata!") | ||
| { TextFormat = TextFormats.Markdown }, | ||
| cancellationToken); | ||
| }); | ||
|
|
||
| webApp.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);ExperimentalTeamsQuotedReplies</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Microsoft.Teams.Bot.Apps\Microsoft.Teams.Bot.Apps.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Quoting Sample | ||
|
|
||
| Demonstrates various ways to quote previous messages in a Teams bot using the `quotedReply` entity. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Bot registered and installed in a chat or channel | ||
|
|
||
| --- | ||
|
|
||
| ## Commands | ||
|
|
||
| | Command | Behavior | | ||
| |---------|----------| | ||
| | `test reply` | `ReplyAsync()` — auto-quotes the inbound message | | ||
| | `test quote` | `QuoteAsync()` — sends a message, then quotes it by ID | | ||
| | `test add` | `AddQuote()` — sends a message, then quotes it with extension method + response | | ||
| | `test multi` | Sends three messages, then quotes all with interleaved responses | | ||
| | `test builder` | `WithQuote()` on `TeamsActivityBuilder` | | ||
| | *(quote a message)* | Bot reads and displays the quoted reply metadata | | ||
|
|
||
| --- | ||
|
|
||
| ## Running the Sample | ||
|
|
||
| 1. Build and run: | ||
| ```bash | ||
| dotnet run --project samples/Quoting/Quoting.csproj | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Warning", | ||
| "Microsoft.Teams": "Information" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/src/Microsoft.Teams.Bot.Apps/Schema/Entities/ActivityQuotedReplyExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.Teams.Bot.Apps.Schema.Entities; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for Activity to handle quoted replies. | ||
| /// </summary> | ||
| [Experimental("ExperimentalTeamsQuotedReplies")] | ||
| public static class ActivityQuotedReplyExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets all quoted reply entities from the activity's entity collection. | ||
| /// </summary> | ||
| /// <param name="activity">The activity to extract quoted replies from. Cannot be null.</param> | ||
| /// <returns>An enumerable of QuotedReplyEntity instances found in the activity's entities.</returns> | ||
| public static IEnumerable<QuotedReplyEntity> GetQuotedMessages(this TeamsActivity activity) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(activity); | ||
| if (activity.Entities == null) | ||
| { | ||
| return []; | ||
| } | ||
| return activity.Entities.Where(e => e is QuotedReplyEntity).Cast<QuotedReplyEntity>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a quoted message reference and append a placeholder to the message text. | ||
| /// Teams renders the quoted message as a preview bubble above the response text. | ||
| /// If text is provided, it is appended to the quoted message placeholder. | ||
| /// </summary> | ||
| /// <param name="activity">The message activity to add the quote to. Cannot be null.</param> | ||
| /// <param name="messageId">The ID of the message to quote. Cannot be null or whitespace.</param> | ||
| /// <param name="text">Optional text, appended to the quoted message placeholder.</param> | ||
| /// <returns>The created QuotedReplyEntity that was added to the activity.</returns> | ||
| public static QuotedReplyEntity AddQuote(this MessageActivity activity, string messageId, string? text = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(activity); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(messageId); | ||
|
|
||
| QuotedReplyEntity entity = new() { QuotedReply = new QuotedReplyData { MessageId = messageId } }; | ||
| activity.Entities ??= []; | ||
| activity.Entities.Add(entity); | ||
|
|
||
| var placeholder = $"<quoted messageId=\"{messageId}\"/>"; | ||
| activity.Text = (activity.Text ?? "") + placeholder; | ||
| if (text != null) | ||
| { | ||
| activity.Text += $" {text}"; | ||
| } | ||
|
|
||
| activity.Rebase(); | ||
| return entity; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Prepend a QuotedReply entity and placeholder before existing text. | ||
| /// Used by ReplyAsync()/QuoteAsync() for quote-above-response. | ||
| /// </summary> | ||
| /// <param name="activity">The message activity to prepend the quoted reply to.</param> | ||
| /// <param name="messageId">The ID of the message to quote.</param> | ||
| public static void PrependQuote(this MessageActivity activity, string messageId) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(activity); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(messageId); | ||
|
|
||
| activity.Entities ??= []; | ||
| activity.Entities.Add(new QuotedReplyEntity { QuotedReply = new QuotedReplyData { MessageId = messageId } }); | ||
| var placeholder = $"<quoted messageId=\"{messageId}\"/>"; | ||
| var text = activity.Text?.Trim() ?? ""; | ||
| activity.Text = string.IsNullOrEmpty(text) ? placeholder : $"{placeholder} {text}"; | ||
| activity.Rebase(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.