-
Notifications
You must be signed in to change notification settings - Fork 16
Prompt Preview Support #433
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
035496f
e62104d
b92891f
a64557d
2026c90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Teams.Api.Entities; | ||
|
|
||
| public class TargetedMessageInfoEntity : Entity | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
|
||
| { | ||
| [JsonPropertyName("messageId")] | ||
| [JsonPropertyOrder(3)] | ||
| public required string MessageId { get; set; } | ||
|
|
||
| public TargetedMessageInfoEntity() : base("targetedMessageInfo") { } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Teams.Api.Activities; | ||
| using Microsoft.Teams.Api.Entities; | ||
|
|
||
| namespace Microsoft.Teams.Apps; | ||
|
|
||
|
|
@@ -67,6 +68,20 @@ public partial class Context<TActivity> : IContext<TActivity> | |
| { | ||
| public async Task<T> Send<T>(T activity, CancellationToken cancellationToken = default) where T : IActivity | ||
| { | ||
| // Auto-populate targetedMessageInfo entity for prompt preview | ||
| // when the incoming activity was a targeted message (reactive flow). | ||
| #pragma warning disable ExperimentalTeamsTargeted | ||
| if (Activity.Recipient?.IsTargeted == true && Activity.Id is not null) | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
Outdated
|
||
| { | ||
| var hasEntity = activity.Entities?.Any(e => e is TargetedMessageInfoEntity) ?? false; | ||
| if (!hasEntity) | ||
| { | ||
| activity.Entities ??= new List<IEntity>(); | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
Outdated
|
||
| activity.Entities.Add(new TargetedMessageInfoEntity { MessageId = Activity.Id }); | ||
| } | ||
| } | ||
| #pragma warning restore ExperimentalTeamsTargeted | ||
|
Comment on lines
69
to
+77
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also scan
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, but from the other direction - Reply skips ToQuoteReply when the incoming activity is targeted, so the blockquote is never generated. Same outcome. Since quoted reply is still not fully rolled out, going with this approach for the fix.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of this approach. I intentionally asked for the check to be for quotedReply here to avoid collision with the quoted replies sdk changes, and we don't know which order they will be merged in. |
||
|
|
||
| var res = await Sender.Send(activity, Ref, CancellationToken); | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
|
||
| await OnActivitySent(res, ToActivityType<IActivity>()); | ||
| return res; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System.Text.Json; | ||
|
|
||
| using Microsoft.Teams.Api.Entities; | ||
|
|
||
| namespace Microsoft.Teams.Api.Tests.Entities; | ||
|
|
||
| public class TargetedMessageInfoEntityTests | ||
| { | ||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonSerialize() | ||
| { | ||
| var entity = new TargetedMessageInfoEntity() | ||
| { | ||
| MessageId = "1772129782775" | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(entity, new JsonSerializerOptions() | ||
| { | ||
| WriteIndented = true, | ||
| IndentSize = 2, | ||
| DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
| }); | ||
|
|
||
| Assert.Equal(File.ReadAllText( | ||
| @"../../../Json/Entities/TargetedMessageInfoEntity.json" | ||
| ), json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonSerialize_Derived() | ||
| { | ||
| Entity entity = new TargetedMessageInfoEntity() | ||
| { | ||
| MessageId = "1772129782775" | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(entity, new JsonSerializerOptions() | ||
| { | ||
| WriteIndented = true, | ||
| IndentSize = 2, | ||
| DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
| }); | ||
|
|
||
| Assert.Equal(File.ReadAllText( | ||
| @"../../../Json/Entities/TargetedMessageInfoEntity.json" | ||
| ), json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonDeserialize() | ||
| { | ||
| var json = File.ReadAllText(@"../../../Json/Entities/TargetedMessageInfoEntity.json"); | ||
| var entity = JsonSerializer.Deserialize<TargetedMessageInfoEntity>(json); | ||
|
|
||
| Assert.NotNull(entity); | ||
| Assert.Equal("targetedMessageInfo", entity.Type); | ||
| Assert.Equal("1772129782775", entity.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonDeserialize_Derived() | ||
| { | ||
| var json = File.ReadAllText(@"../../../Json/Entities/TargetedMessageInfoEntity.json"); | ||
| var entity = JsonSerializer.Deserialize<Entity>(json); | ||
|
|
||
| Assert.NotNull(entity); | ||
| Assert.IsType<TargetedMessageInfoEntity>(entity); | ||
|
|
||
| var targeted = (TargetedMessageInfoEntity)entity; | ||
| Assert.Equal("targetedMessageInfo", targeted.Type); | ||
| Assert.Equal("1772129782775", targeted.MessageId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "targetedMessageInfo", | ||
| "messageId": "1772129782775" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using Microsoft.Teams.Api; | ||
| using Microsoft.Teams.Api.Activities; | ||
| using Microsoft.Teams.Api.Auth; | ||
| using Microsoft.Teams.Api.Entities; | ||
| using Microsoft.Teams.Apps.Activities; | ||
| using Microsoft.Teams.Apps.Testing.Plugins; | ||
|
|
||
| namespace Microsoft.Teams.Apps.Tests.Activities; | ||
|
|
||
| public class PromptPreviewTests | ||
| { | ||
| private readonly App _app = new(); | ||
| private readonly IToken _token = Globals.Token; | ||
|
|
||
| public PromptPreviewTests() | ||
| { | ||
| _app.AddPlugin(new TestPlugin()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Send("Here is the result!", cancellationToken); | ||
| }); | ||
|
|
||
| // Simulate an incoming targeted message (bot's Recipient.IsTargeted = true) | ||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.NotNull(targetedEntity); | ||
| Assert.Equal("1772129782775", targetedEntity!.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Reply_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Reply("Here is the result!", cancellationToken); | ||
| }); | ||
|
|
||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithConversation(new Api.Conversation() { Id = "conv1" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.NotNull(targetedEntity); | ||
| Assert.Equal("1772129782775", targetedEntity!.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_DoesNotAdd_TargetedMessageInfoEntity_WhenNotTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Send("Hello!", cancellationToken); | ||
| }); | ||
|
|
||
| // Normal (non-targeted) incoming message | ||
| var incomingActivity = new MessageActivity("hello") | ||
| .WithId("123456") | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| var targetedEntity = sentActivity!.Entities?.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.Null(targetedEntity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_DoesNotDuplicate_TargetedMessageInfoEntity_WhenAlreadyPresent() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| // Developer manually adds the entity (proactive-like scenario) | ||
| var activity = new MessageActivity("Result") | ||
| .AddEntity(new TargetedMessageInfoEntity { MessageId = "9999" }); | ||
|
|
||
| sentActivity = await context.Send(activity, cancellationToken); | ||
| }); | ||
|
|
||
| // Incoming activity is targeted | ||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntities = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().ToList(); | ||
| Assert.Single(targetedEntities); | ||
| // The developer-provided entity should be preserved, not overwritten | ||
| Assert.Equal("9999", targetedEntities[0].MessageId); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A parallel
AddTargetedMessageInfo(string messageId)onActivitywould matchAddFeedback/AddCitationand the TSaddTargetedMessageInfoalready in this PR. Gives a single home for the §1 P0 "one PP per message" dedup plus the usage XML doc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.