microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/core-claude-agents

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/samples/AFBot/Program.cs

62lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.ClientModel;
5using AFBot;
6using Azure.AI.OpenAI;
7using Azure.Monitor.OpenTelemetry.AspNetCore;
8using Microsoft.Agents.AI;
9using Microsoft.Extensions.AI;
10using Microsoft.Teams.Bot.Core;
11using Microsoft.Teams.Bot.Core.Hosting;
12using Microsoft.Teams.Bot.Core.Schema;
13using OpenAI;
14
15WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
16WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args);
17webAppBuilder.Services.AddOpenTelemetry().UseAzureMonitor();
18webAppBuilder.Services.AddBotApplication<BotApplication>();
19WebApplication webApp = webAppBuilder.Build();
20BotApplication botApp = webApp.UseBotApplication<BotApplication>();
21
22AzureOpenAIClient azureClient = new(
23 new Uri("https://tsdkfoundry.openai.azure.com/"),
24 new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_OpenAI_KEY")!));
25
26ChatClientAgent agent = azureClient.GetChatClient("gpt-5-nano").CreateAIAgent(
27 instructions: "You are an expert acronym maker, made an acronym made up from the first three characters of the user's message. " +
28 "Some examples: OMW on my way, BTW by the way, TVM thanks very much, and so on." +
29 "Always respond with the three complete words only, and include a related emoji at the end.",
30 name: "AcronymMaker");
31
32botApp.UseMiddleware(new DropTypingMiddleware());
33
34botApp.OnActivity = async (activity, cancellationToken) =>
35{
36 ArgumentNullException.ThrowIfNull(activity);
37
38 CancellationTokenSource timer = CancellationTokenSource.CreateLinkedTokenSource(
39 cancellationToken, new CancellationTokenSource(TimeSpan.FromSeconds(15)).Token);
40
41 CoreActivity typing = CoreActivity.CreateBuilder()
42 .WithType(ActivityType.Typing)
43 .WithConversationReference(activity)
44 .Build();
45 await botApp.SendActivityAsync(typing, cancellationToken);
46
47 AgentRunResponse agentResponse = await agent.RunAsync(activity.Properties["text"]?.ToString() ?? "OMW", cancellationToken: timer.Token);
48
49 ChatMessage? m1 = agentResponse.Messages.FirstOrDefault();
50 Console.WriteLine($"AI:: GOT {agentResponse.Messages.Count} msgs");
51 CoreActivity replyActivity = CoreActivity.CreateBuilder()
52 .WithType(ActivityType.Message)
53 .WithConversationReference(activity)
54 .WithProperty("text", m1!.Text)
55 .Build();
56
57 SendActivityResponse? res = await botApp.SendActivityAsync(replyActivity, cancellationToken);
58
59 Console.WriteLine("SENT >>> => " + res?.Id);
60};
61
62webApp.Run();
63