microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
kavin/agents-sdk-interop

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/samples/AFBot/Program.cs

68lines · 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.Core;
11using Microsoft.Teams.Core.Hosting;
12using Microsoft.Teams.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 .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
68webApp.Run();
69