microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-338

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/samples/AFBot/Program.cs

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