microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c92c1919ec3b5a0b25a067ad4ad46da598a2fdca

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/samples/TeamsBot/Program.cs

82lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Bot.Apps;
5using Microsoft.Teams.Bot.Apps.Handlers;
6using Microsoft.Teams.Bot.Apps.Schema;
7using Microsoft.Teams.Bot.Apps.Schema.Entities;
8using Microsoft.Teams.Bot.Apps.Schema.MessageActivities;
9using TeamsBot;
10
11var builder = TeamsBotApplication.CreateBuilder();
12var teamsApp = builder.Build();
13
14
15teamsApp.OnMessageUpdate(async (context, cancellationToken) =>
16{
17 string updatedText = context.Activity.Text ?? "<no text>";
18 MessageActivity reply = new($"I saw that you updated your message to: `{updatedText}`");
19 await context.SendActivityAsync(reply, cancellationToken);
20});
21
22teamsApp.OnMessage(async (context, cancellationToken) =>
23{
24 await context.SendTypingActivityAsync(cancellationToken);
25
26 string replyText = $"You sent: `{context.Activity.Text}` in activity of type `{context.Activity.Type}`.";
27
28 MessageActivity reply = new(replyText);
29 reply.AddMention(context.Activity.From!);
30
31 await context.SendActivityAsync(reply, cancellationToken);
32
33 TeamsAttachment feedbackCard = TeamsAttachment.CreateBuilder()
34 .WithAdaptiveCard(Cards.FeedbackCardObj)
35 .Build();
36 MessageActivity feedbackActivity = new([feedbackCard]);
37 await context.SendActivityAsync(feedbackActivity, cancellationToken);
38});
39
40teamsApp.OnMessageReaction( async (context, cancellationToken) =>
41{
42 string reactionsAdded = string.Join(", ", context.Activity.ReactionsAdded?.Select(r => r.Type) ?? []);
43 string reactionsRemoved = string.Join(", ", context.Activity.ReactionsRemoved?.Select(r => r.Type) ?? []);
44
45 TeamsAttachment reactionsCard = TeamsAttachment.CreateBuilder()
46 .WithAdaptiveCard(Cards.ReactionsCard(reactionsAdded, reactionsRemoved))
47 .Build();
48 MessageActivity reply = new([reactionsCard]);
49
50 await context.SendActivityAsync(reply, cancellationToken);
51});
52
53teamsApp.OnMessageDelete(async (context, cancellationToken) =>
54{
55
56 await context.SendActivityAsync("I saw that message you deleted", cancellationToken);
57});
58
59
60teamsApp.OnInvoke(async (context, cancellationToken) =>
61{
62 var valueNode = context.Activity.Value;
63 string? feedbackValue = valueNode?["action"]?["data"]?["feedback"]?.GetValue<string>();
64
65 var reply = TeamsActivity.CreateBuilder()
66 .WithAttachment(TeamsAttachment.CreateBuilder()
67 .WithAdaptiveCard(Cards.ResponseCard(feedbackValue))
68 .Build()
69 )
70 .Build();
71
72 await context.SendActivityAsync(reply, cancellationToken);
73
74 return new CoreInvokeResponse(200)
75 {
76 Type = "application/vnd.microsoft.activity.message",
77 Body = "Invokes are great !!"
78 };
79});
80
81
82teamsApp.Run();
83