microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
pr-134

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Lights/Controller.cs

42lines · modecode

1using System.Text.Json;
2
3using Microsoft.Teams.AI.Models.OpenAI;
4using Microsoft.Teams.Api.Activities;
5using Microsoft.Teams.Apps;
6using Microsoft.Teams.Apps.Activities;
7using Microsoft.Teams.Apps.Annotations;
8
9namespace Samples.Lights;
10
11[TeamsController]
12public class Controller(IHttpContextAccessor httpContextAccessor)
13{
14 private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
15
16 [Message("/history")]
17 public async Task OnHistory(IContext<MessageActivity> context)
18 {
19 var state = State.From(context);
20 await context.Send(JsonSerializer.Serialize(state.Messages, new JsonSerializerOptions()
21 {
22 WriteIndented = true
23 }));
24 }
25
26 [Message]
27 public async Task OnMessage(IContext<MessageActivity> context)
28 {
29 var state = State.From(context);
30
31 var httpContext = _httpContextAccessor.HttpContext
32 ?? throw new InvalidOperationException("No active HttpContext. Cannot resolve OpenAIChatPrompt.");
33
34 var prompt = httpContext.RequestServices.GetRequiredService<OpenAIChatPrompt>();
35 await prompt.Send(context.Activity.Text, new() { Messages = state.Messages }, (chunk) => Task.Run(() =>
36 {
37 context.Stream.Emit(chunk);
38 }), context.CancellationToken);
39
40 state.Save(context);
41 }
42}