microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/AFBot/Program.cs
68lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ClientModel; |
| 5 | using AFBot; |
| 6 | using Azure.AI.OpenAI; |
| 7 | using Azure.Monitor.OpenTelemetry.AspNetCore; |
| 8 | using Microsoft.Agents.AI; |
| 9 | using Microsoft.Extensions.AI; |
| 10 | using Microsoft.Teams.Core; |
| 11 | using Microsoft.Teams.Core.Hosting; |
| 12 | using Microsoft.Teams.Core.Schema; |
| 13 | using OpenAI; |
| 14 | |
| 15 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 16 | WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args); |
| 17 | webAppBuilder.Services.AddOpenTelemetry().UseAzureMonitor(); |
| 18 | webAppBuilder.Services.AddBotApplication<BotApplication>(); |
| 19 | WebApplication webApp = webAppBuilder.Build(); |
| 20 | BotApplication botApp = webApp.UseBotApplication<BotApplication>(); |
| 21 | |
| 22 | AzureOpenAIClient azureClient = new( |
| 23 | new Uri("https://tsdkfoundry.openai.azure.com/"), |
| 24 | new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_OpenAI_KEY")!)); |
| 25 | |
| 26 | ChatClientAgent 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 | |
| 32 | botApp.UseMiddleware(new DropTypingMiddleware()); |
| 33 | |
| 34 | botApp.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 | .WithServiceUrl(activity.ServiceUrl!) |
| 44 | .WithChannelId(activity.ChannelId!) |
| 45 | .WithConversation(activity.Conversation!) |
| 46 | .WithFrom(activity.Recipient) |
| 47 | .Build(); |
| 48 | await botApp.SendActivityAsync(typing, cancellationToken: cancellationToken); |
| 49 | |
| 50 | AgentRunResponse agentResponse = await agent.RunAsync(activity.Properties["text"]?.ToString() ?? "OMW", cancellationToken: timer.Token); |
| 51 | |
| 52 | ChatMessage? m1 = agentResponse.Messages.FirstOrDefault(); |
| 53 | Console.WriteLine($"AI:: GOT {agentResponse.Messages.Count} msgs"); |
| 54 | CoreActivity replyActivity = CoreActivity.CreateBuilder() |
| 55 | .WithType(ActivityType.Message) |
| 56 | .WithServiceUrl(activity.ServiceUrl!) |
| 57 | .WithChannelId(activity.ChannelId!) |
| 58 | .WithConversation(activity.Conversation!) |
| 59 | .WithFrom(activity.Recipient) |
| 60 | .WithProperty("text", m1!.Text) |
| 61 | .Build(); |
| 62 | |
| 63 | SendActivityResponse? res = await botApp.SendActivityAsync(replyActivity, cancellationToken: cancellationToken); |
| 64 | |
| 65 | Console.WriteLine("SENT >>> => " + res?.Id); |
| 66 | }; |
| 67 | |
| 68 | webApp.Run(); |
| 69 | |