Z User commited on
Commit
6910d77
Β·
1 Parent(s): 4dda966

fix(phase4): edge functions now broadcast via Render bot /internal/broadcast

Browse files

Edge 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
- fetch(`${SUPABASE_URL}/functions/v1/telegram`, {
 
 
111
  method: 'POST',
112
- headers: { ...adminHeaders, 'X-Internal-Call': 'cellex-internal', 'Content-Type': 'application/json' },
 
 
 
113
  body: JSON.stringify({
114
- op: 'broadcast',
115
- broadcastType: 'group_buy',
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
- imageUrl: product.image_url || undefined,
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
- fetch(`${SUPABASE_URL}/functions/v1/telegram`, {
 
 
191
  method: 'POST',
192
- headers: { ...adminHeaders, 'X-Internal-Call': 'cellex-internal', 'Content-Type': 'application/json' },
 
 
 
193
  body: JSON.stringify({
194
- op: 'broadcast',
195
- broadcastType: 'live_start',
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\n${SUPABASE_URL ? 'https://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 telegram edge function (fire-and-forget)
97
  async function broadcastToTelegram(broadcastType: string, entityId: string, message: string, imageUrl?: string): Promise<void> {
98
- await fetch(`${SUPABASE_URL}/functions/v1/telegram`, {
 
 
99
  method: 'POST',
100
- headers: { ...adminHeaders, 'X-Internal-Call': 'cellex-internal', 'Content-Type': 'application/json' },
101
- body: JSON.stringify({ op: 'broadcast', broadcastType, entityId, message, imageUrl }),
 
 
 
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