microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d0339232bb3a8d7c066f7aa603e99ef1b88f6752

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Samples/Samples.Quoting/Program.cs

134lines · modecode

1using Microsoft.Teams.Api.Activities;
2using Microsoft.Teams.Api.Entities;
3using Microsoft.Teams.Apps.Activities;
4using Microsoft.Teams.Apps.Extensions;
5using Microsoft.Teams.Plugins.AspNetCore.Extensions;
6
7var builder = WebApplication.CreateBuilder(args);
8builder.AddTeams();
9var app = builder.Build();
10var teams = app.UseTeams();
11
12teams.OnActivity(async (context, cancellationToken) =>
13{
14 context.Log.Info($"[ACTIVITY] Type: {context.Activity.Type}, From: {context.Activity.From?.Name ?? "unknown"}");
15 await context.Next();
16});
17
18teams.OnMessage(async (context, cancellationToken) =>
19{
20 var activity = context.Activity;
21 var text = activity.Text?.ToLowerInvariant() ?? "";
22
23 context.Log.Info($"[MESSAGE] Received: {text}");
24
25 // ============================================
26 // Read inbound quoted replies
27 // ============================================
28 var quotes = activity.GetQuotedMessages();
29 if (quotes.Count > 0)
30 {
31 var quote = quotes[0].QuotedReply!;
32 var info = $"Quoted message ID: {quote.MessageId}";
33 if (quote.SenderName != null) info += $"\nFrom: {quote.SenderName}";
34 if (quote.Preview != null) info += $"\nPreview: \"{quote.Preview}\"";
35 if (quote.IsReplyDeleted == true) info += "\n(deleted)";
36 if (quote.ValidatedMessageReference == true) info += "\n(validated)";
37
38 await context.Send($"You sent a message with a quoted reply:\n\n{info}", cancellationToken);
39 }
40
41 // ============================================
42 // Reply() — auto-quotes the inbound message
43 // ============================================
44 if (text.Contains("test reply"))
45 {
46 await context.Reply("Thanks for your message! This reply auto-quotes it using Reply().", cancellationToken);
47 return;
48 }
49
50 // ============================================
51 // Quote() — quote a previously sent message by ID
52 // ============================================
53 if (text.Contains("test quote"))
54 {
55 var sent = await context.Send("The meeting has been moved to 3 PM tomorrow.", cancellationToken);
56 await context.Quote(sent.Id, "Just to confirm — does the new time work for everyone?", cancellationToken);
57 return;
58 }
59
60 // ============================================
61 // AddQuote() — builder with response
62 // ============================================
63 if (text.Contains("test add"))
64 {
65 var sent = await context.Send("Please review the latest PR before end of day.", cancellationToken);
66 var msg = new MessageActivity()
67 .AddQuote(sent.Id, "Done! Left my comments on the PR.");
68 await context.Send(msg, cancellationToken);
69 return;
70 }
71
72 // ============================================
73 // Multi-quote with mixed responses
74 // ============================================
75 if (text.Contains("test multi"))
76 {
77 var sentA = await context.Send("We need to update the API docs before launch.", cancellationToken);
78 var sentB = await context.Send("The design mockups are ready for review.", cancellationToken);
79 var sentC = await context.Send("CI pipeline is green on main.", cancellationToken);
80 var msg = new MessageActivity()
81 .AddQuote(sentA.Id, "I can take the docs — will have a draft by Thursday.")
82 .AddQuote(sentB.Id, "Looks great, approved!")
83 .AddQuote(sentC.Id);
84 await context.Send(msg, cancellationToken);
85 return;
86 }
87
88 // ============================================
89 // AddQuote() + AddText() — manual control
90 // ============================================
91 if (text.Contains("test manual"))
92 {
93 var sent = await context.Send("Deployment to staging is complete.", cancellationToken);
94 var msg = new MessageActivity()
95 .AddQuote(sent.Id)
96 .AddText(" Verified — all smoke tests passing.");
97 await context.Send(msg, cancellationToken);
98 return;
99 }
100
101 // ============================================
102 // ToQuoteReply() — obsolete method (temporary)
103 // ============================================
104 if (text.Contains("test obsolete"))
105 {
106#pragma warning disable CS0618 // Obsolete
107 var placeholder = activity.ToQuoteReply();
108#pragma warning restore CS0618
109 await context.Send($"ToQuoteReply() returned: {placeholder}", cancellationToken);
110 return;
111 }
112
113 // ============================================
114 // Help / Default
115 // ============================================
116 if (text.Contains("help"))
117 {
118 await context.Send(
119 "**Quoting Test Bot**\n\n" +
120 "**Commands:**\n" +
121 "- `test reply` - Reply() auto-quotes your message\n" +
122 "- `test quote` - Quote() quotes a previously sent message\n" +
123 "- `test add` - AddQuote() builder with response\n" +
124 "- `test multi` - Multi-quote with mixed responses (one bare quote with no response)\n" +
125 "- `test manual` - AddQuote() + AddText() manual control\n" +
126 "- `test obsolete` - ToQuoteReply() obsolete method\n\n" +
127 "Quote any message to me to see the parsed metadata!", cancellationToken);
128 return;
129 }
130
131 await context.Send($"You said: '{activity.Text}'\n\nType `help` to see available commands.", cancellationToken);
132});
133
134app.Run();
135