Spaces:
Sleeping
Sleeping
Z User commited on
Commit Β·
6910d77
1
Parent(s): 4dda966
fix(phase4): edge functions now broadcast via Render bot /internal/broadcast
Browse filesEdge functions (seller-products, live, group-buy) now call the Render bot's
/internal/broadcast endpoint instead of the Supabase telegram edge function.
This uses the bot's TELEGRAM_BOT_TOKEN (which never leaves Render) to send
actual Telegram messages to subscribers.
Auth: X-Internal-Key header (BOT_INTERNAL_KEY env var on both sides).
supabase/functions/group-buy/index.ts
CHANGED
|
@@ -106,16 +106,20 @@ async function handleCreate(userId: string, body: Record<string, unknown>): Prom
|
|
| 106 |
const freshRows = await refreshed.json();
|
| 107 |
const freshGroupBuy = freshRows?.[0] || created[0];
|
| 108 |
|
| 109 |
-
// Phase 4: Auto-broadcast to Telegram (fire-and-forget)
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
method: 'POST',
|
| 112 |
-
headers: {
|
|
|
|
|
|
|
|
|
|
| 113 |
body: JSON.stringify({
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
entityId: groupBuyId,
|
| 117 |
message: `ποΈ <b>New group buy!</b>\n${escapeHtml(product.name)} β $${Number(product.price).toFixed(2)}\nGet ${discountPct}% off when ${targetCount} friends join.\n\nJoin: https://eeshaai-cellex-web.hf.space/group-buy.html?id=${groupBuyId}`,
|
| 118 |
-
|
| 119 |
}),
|
| 120 |
}).catch(() => {});
|
| 121 |
|
|
|
|
| 106 |
const freshRows = await refreshed.json();
|
| 107 |
const freshGroupBuy = freshRows?.[0] || created[0];
|
| 108 |
|
| 109 |
+
// Phase 4: Auto-broadcast to Telegram via the Render bot (fire-and-forget)
|
| 110 |
+
const BOT_RENDER_URL = Deno.env.get('BOT_RENDER_URL') || 'https://eesha-shop-buying-and-selling.onrender.com';
|
| 111 |
+
const BOT_INTERNAL_KEY = Deno.env.get('BOT_INTERNAL_KEY') || 'CellexInternal2024';
|
| 112 |
+
fetch(`${BOT_RENDER_URL}/internal/broadcast`, {
|
| 113 |
method: 'POST',
|
| 114 |
+
headers: {
|
| 115 |
+
'Content-Type': 'application/json',
|
| 116 |
+
'X-Internal-Key': BOT_INTERNAL_KEY,
|
| 117 |
+
},
|
| 118 |
body: JSON.stringify({
|
| 119 |
+
broadcast_type: 'group_buy',
|
| 120 |
+
entity_id: groupBuyId,
|
|
|
|
| 121 |
message: `ποΈ <b>New group buy!</b>\n${escapeHtml(product.name)} β $${Number(product.price).toFixed(2)}\nGet ${discountPct}% off when ${targetCount} friends join.\n\nJoin: https://eeshaai-cellex-web.hf.space/group-buy.html?id=${groupBuyId}`,
|
| 122 |
+
image_url: product.image_url || undefined,
|
| 123 |
}),
|
| 124 |
}).catch(() => {});
|
| 125 |
|
supabase/functions/live/index.ts
CHANGED
|
@@ -186,14 +186,18 @@ async function handleStart(userId: string, body: Record<string, unknown>): Promi
|
|
| 186 |
// System message
|
| 187 |
await postSystemMessage(session.id, `π΄ Live now: ${title}`);
|
| 188 |
|
| 189 |
-
// Phase 4: Auto-broadcast to Telegram (fire-and-forget)
|
| 190 |
-
|
|
|
|
|
|
|
| 191 |
method: 'POST',
|
| 192 |
-
headers: {
|
|
|
|
|
|
|
|
|
|
| 193 |
body: JSON.stringify({
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
entityId: session.id,
|
| 197 |
message: `π΄ <b>LIVE NOW:</b> ${escapeHtml(title)}\n${(body.description as string) || ''}\n\nWatch: https://eeshaai-cellex-web.hf.space/live-watch.html?id=${session.id}`,
|
| 198 |
}),
|
| 199 |
}).catch(() => {});
|
|
|
|
| 186 |
// System message
|
| 187 |
await postSystemMessage(session.id, `π΄ Live now: ${title}`);
|
| 188 |
|
| 189 |
+
// Phase 4: Auto-broadcast to Telegram via the Render bot (fire-and-forget)
|
| 190 |
+
const BOT_RENDER_URL = Deno.env.get('BOT_RENDER_URL') || 'https://eesha-shop-buying-and-selling.onrender.com';
|
| 191 |
+
const BOT_INTERNAL_KEY = Deno.env.get('BOT_INTERNAL_KEY') || 'CellexInternal2024';
|
| 192 |
+
fetch(`${BOT_RENDER_URL}/internal/broadcast`, {
|
| 193 |
method: 'POST',
|
| 194 |
+
headers: {
|
| 195 |
+
'Content-Type': 'application/json',
|
| 196 |
+
'X-Internal-Key': BOT_INTERNAL_KEY,
|
| 197 |
+
},
|
| 198 |
body: JSON.stringify({
|
| 199 |
+
broadcast_type: 'live_start',
|
| 200 |
+
entity_id: session.id,
|
|
|
|
| 201 |
message: `π΄ <b>LIVE NOW:</b> ${escapeHtml(title)}\n${(body.description as string) || ''}\n\nWatch: https://eeshaai-cellex-web.hf.space/live-watch.html?id=${session.id}`,
|
| 202 |
}),
|
| 203 |
}).catch(() => {});
|
supabase/functions/seller-products/index.ts
CHANGED
|
@@ -84,21 +84,26 @@ async function handleCreate(sellerId: string, body: Record<string, unknown>): Pr
|
|
| 84 |
if (!resp.ok) return errorResponse(created?.message || 'Failed to create product', 500);
|
| 85 |
const product = created?.[0];
|
| 86 |
|
| 87 |
-
// Phase 4: Auto-broadcast to Telegram (fire-and-forget)
|
| 88 |
broadcastToTelegram('new_product', String(product.id),
|
| 89 |
-
`π <b>New product:</b> ${escapeHtml(name)}\nπ΅ $${Number(price).toFixed(2)}\nπ¦ ${payload.category}\n\
|
| 90 |
payload.image_url as string | undefined
|
| 91 |
).catch(() => {});
|
| 92 |
|
| 93 |
return jsonResponse({ success: true, product });
|
| 94 |
}
|
| 95 |
|
| 96 |
-
// Helper: broadcast to Telegram via the
|
| 97 |
async function broadcastToTelegram(broadcastType: string, entityId: string, message: string, imageUrl?: string): Promise<void> {
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
method: 'POST',
|
| 100 |
-
headers: {
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
}).catch(() => {});
|
| 103 |
}
|
| 104 |
|
|
|
|
| 84 |
if (!resp.ok) return errorResponse(created?.message || 'Failed to create product', 500);
|
| 85 |
const product = created?.[0];
|
| 86 |
|
| 87 |
+
// Phase 4: Auto-broadcast to Telegram via the Render bot (fire-and-forget)
|
| 88 |
broadcastToTelegram('new_product', String(product.id),
|
| 89 |
+
`π <b>New product:</b> ${escapeHtml(name)}\nπ΅ $${Number(price).toFixed(2)}\nπ¦ ${payload.category}\n\nhttps://eeshaai-cellex-web.hf.space/Eesha buying folder/product.html?id=${product.id}`,
|
| 90 |
payload.image_url as string | undefined
|
| 91 |
).catch(() => {});
|
| 92 |
|
| 93 |
return jsonResponse({ success: true, product });
|
| 94 |
}
|
| 95 |
|
| 96 |
+
// Helper: broadcast to Telegram via the Render bot's /internal/broadcast endpoint
|
| 97 |
async function broadcastToTelegram(broadcastType: string, entityId: string, message: string, imageUrl?: string): Promise<void> {
|
| 98 |
+
const BOT_RENDER_URL = Deno.env.get('BOT_RENDER_URL') || 'https://eesha-shop-buying-and-selling.onrender.com';
|
| 99 |
+
const BOT_INTERNAL_KEY = Deno.env.get('BOT_INTERNAL_KEY') || 'CellexInternal2024';
|
| 100 |
+
await fetch(`${BOT_RENDER_URL}/internal/broadcast`, {
|
| 101 |
method: 'POST',
|
| 102 |
+
headers: {
|
| 103 |
+
'Content-Type': 'application/json',
|
| 104 |
+
'X-Internal-Key': BOT_INTERNAL_KEY,
|
| 105 |
+
},
|
| 106 |
+
body: JSON.stringify({ broadcast_type: broadcastType, entity_id: entityId, message, image_url: imageUrl }),
|
| 107 |
}).catch(() => {});
|
| 108 |
}
|
| 109 |
|