version con express js que no funciona

This commit is contained in:
2026-02-25 12:17:59 -06:00
parent dcaeeb72dd
commit 8aeb1d0a64
10 changed files with 4445 additions and 3 deletions
@@ -0,0 +1,48 @@
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.
// Setup type definitions for built-in Supabase Runtime APIs
import "@supabase/functions-js/edge-runtime.d.ts";
import express from "express";
import OpenAI from "openai";
const app = express();
const client = new OpenAI({
webhookSecret: Deno.env.get("OPENAI_WEBHOOK_SECRET"),
});
app.use(express.text({ type: "application/json" }));
app.post("/webhook", async (req: express.Request, res: express.Response) => {
try {
const event = await client.webhooks.unwrap(req.body, req.headers);
if (event.type === "response.completed") {
const response_id = event.data.id;
const response = await client.responses.retrieve(response_id);
const output_text = response.output
.filter((item) => item.type === "message")
.flatMap((item) => item.content)
.filter((contentItem) => contentItem.type === "output_text")
.map((contentItem) => contentItem.text)
.join("");
console.log("Response output:", output_text);
}
res.status(200).send();
} catch (error) {
if (error instanceof OpenAI.InvalidWebhookSignatureError) {
console.error("Invalid signature", error);
res.status(400).send("Invalid signature");
} else {
throw error;
}
}
});
Deno.serve(async (req) => {
console.log("Inicio de openai-webhook-responses");
return await app.handle(req);
});