feat: add telegram business connect onboarding and status sync
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
- `RECEIVER_FLOW_QUEUE_NAME` (default: `receiver.flow`)
|
||||
- `INBOUND_QUEUE_NAME` (legacy alias, optional)
|
||||
- `TELEGRAM_WEBHOOK_SECRET` (optional, но обязателен для production)
|
||||
- `TELEGRAM_CONNECT_WEBHOOK_FORWARD_URL` (optional; URL CRM endpoint для линковки Telegram Business)
|
||||
- `MAX_BODY_SIZE_BYTES` (default: `1048576`)
|
||||
|
||||
## Запуск
|
||||
|
||||
@@ -37,6 +37,35 @@ function validateTelegramSecret(req: IncomingMessage): boolean {
|
||||
return incoming !== "" && incoming === expected;
|
||||
}
|
||||
|
||||
async function forwardTelegramConnectWebhook(rawBody: unknown) {
|
||||
const url = (process.env.TELEGRAM_CONNECT_WEBHOOK_FORWARD_URL || "").trim();
|
||||
if (!url) return;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": "application/json",
|
||||
};
|
||||
const secret = (process.env.TELEGRAM_WEBHOOK_SECRET || "").trim();
|
||||
if (secret) {
|
||||
headers["x-telegram-bot-api-secret-token"] = secret;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(rawBody ?? {}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
console.warn(`[omni_inbound] telegram connect forward failed: ${res.status} ${text.slice(0, 300)}`);
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn(`[omni_inbound] telegram connect forward error: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function startServer() {
|
||||
const port = Number(process.env.PORT || 8080);
|
||||
|
||||
@@ -62,12 +91,17 @@ export function startServer() {
|
||||
return;
|
||||
}
|
||||
|
||||
let body: unknown = {};
|
||||
let envelope: ReturnType<typeof parseTelegramBusinessUpdate> | null = null;
|
||||
|
||||
try {
|
||||
const body = await readJsonBody(req);
|
||||
const envelope = parseTelegramBusinessUpdate(body);
|
||||
body = await readJsonBody(req);
|
||||
envelope = parseTelegramBusinessUpdate(body);
|
||||
|
||||
await enqueueInboundEvent(envelope);
|
||||
|
||||
void forwardTelegramConnectWebhook(body);
|
||||
|
||||
writeJson(res, 200, {
|
||||
ok: true,
|
||||
queued: true,
|
||||
@@ -77,6 +111,8 @@ export function startServer() {
|
||||
});
|
||||
} catch (error) {
|
||||
if (isDuplicateJobError(error)) {
|
||||
void forwardTelegramConnectWebhook(body);
|
||||
|
||||
writeJson(res, 200, {
|
||||
ok: true,
|
||||
queued: false,
|
||||
|
||||
Reference in New Issue
Block a user