Files
store-scraper-bot/api/webhook.js
T
tiennm99 c2dd35b75f feat: migrate to Vercel + Upstash with KEY_PREFIX namespacing
Phases 1-5 of consolidate-vercel-upstash plan. Replaces Cloudflare
Workers + KV with Vercel serverless functions + Upstash Redis. Inlines
app-store-scraper / google-play-scraper npm libs (drops the
store-scraper.vercel.app HTTP roundtrip). KEY_PREFIX (default
'store-scraper-bot:') namespaces all Redis keys so the Upstash DB can
be safely shared with other Vercel projects.

- vercel.json + .vercelignore + Vercel-aware package.json scripts
- api/webhook.js + api/cron.js Vercel functions (with shared
  src/app-builder.js); cron auth fails closed when CRON_SECRET unset
- src/repository/upstash.js replaces kv.js; all 4 repos take a handle
  bundling client + prefix
- scripts/migrate-atlas-to-upstash.js writes legacy Java Atlas state
  directly to Upstash with --dry-run + --include-cache flags
- .env.example refreshed for the new env surface

Phases 6 (Vercel deploy + webhook cutover) and 7 (Docker + wrangler
cleanup) remain operator-driven post-deploy.
2026-05-09 20:07:07 +07:00

48 lines
1.4 KiB
JavaScript

// Telegram webhook entry. Vercel serverless function — replaces the prior
// Cloudflare Worker `fetch` handler. Validates the X-Telegram-Bot-Api-Secret-Token
// header, acks fast, then dispatches in waitUntil so Telegram doesn't retry on
// slow downstream calls.
import { waitUntil } from '@vercel/functions';
import { buildApp } from '../src/app-builder.js';
import { dispatch } from '../src/bot/dispatch.js';
export const config = { runtime: 'nodejs' };
export default async function handler(req) {
if (req.method !== 'POST') {
return new Response('Not found', { status: 404 });
}
let app;
try {
app = buildApp(process.env);
} catch (err) {
console.log(JSON.stringify({ level: 'error', msg: 'config error', err: err.message }));
return new Response('Server misconfigured', { status: 500 });
}
const secret = req.headers.get('x-telegram-bot-api-secret-token');
if (secret !== app.config.telegramWebhookSecret) {
return new Response('Unauthorized', { status: 401 });
}
let update;
try {
update = await req.json();
} catch {
return new Response('Bad request', { status: 400 });
}
if (!update?.message) return new Response('OK');
waitUntil(
dispatch(update.message, {
sender: app.sender,
commands: app.commands,
config: app.config,
logger: app.config.logger,
}),
);
return new Response('OK');
}