Skip to content

Instantly share code, notes, and snippets.

@rajan-chari
Created July 1, 2025 16:26
Show Gist options
  • Select an option

  • Save rajan-chari/6992613c22dacda359c89125433cb58c to your computer and use it in GitHub Desktop.

Select an option

Save rajan-chari/6992613c22dacda359c89125433cb58c to your computer and use it in GitHub Desktop.
[HttpPost("/api/messages")]
public async Task<IResult> OnMessage()
{
// [PA POC] Handle reactive scenario w/ BotBuilder adapter.
// [PA POC] For phased migration, BotBuilder adapter still used.
HttpContext.Request.EnableBuffering();
var body = await new StreamReader(Request.Body).ReadToEndAsync();
Activity? activity = JsonSerializer.Deserialize<Activity>(body);
HttpContext.Request.Body.Position = 0;
if (activity == null)
{
return Results.BadRequest("Missing activity");
}
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await _adapter.ProcessAsync(HttpContext.Request, HttpContext.Response, _bot);
if (Response.HasStarted)
{
return Results.Empty;
}
// Fallback logic
var authHeader = HttpContext.Request.Headers.Authorization.FirstOrDefault() ?? throw new UnauthorizedAccessException();
var token = new JsonWebToken(authHeader.Replace("Bearer ", ""));
var context = HttpContext.RequestServices.GetRequiredService<TeamsContext>();
context.Token = token;
var res = await _plugin.Do(token, activity, _lifetime.ApplicationStopping);
return Results.Json(res.Body, statusCode: (int)res.Status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment