read tg_bot_updates from db
Deploy app frontend / app frontend (push) Successful in 2m34s
Deploy db-migrations / db-migrations (push) Successful in 1m12s
Deploy service message-delivery-method-web-push / service message-delivery-method-web-push (push) Successful in 1m38s
Deploy service tg-bot / service tg-bot (push) Failing after 2m11s

This commit is contained in:
2024-12-15 03:14:39 +02:00
parent c075c5f708
commit 0ac5e9ce62
31 changed files with 211 additions and 331 deletions
@@ -0,0 +1,2 @@
export * from "./updates/saveUpdate";
export type * from "./types";
@@ -0,0 +1,28 @@
import type { BaseDocument } from "@hereconnect/types";
export interface TgBotUpdateWebhookDesignDocument extends BaseDocument {
_id: "_design/tg_bot_webhook";
type: "_design/tg_bot_webhook";
updated_at: BaseDocument["created_at"];
updates: {
saveUpdate: string;
};
constants: {
telegram_secret_token: string;
telegram_webhook_url?: string;
};
}
export interface TgBotUpdateDocument extends BaseDocument {
/**
* Тип документа для идентификации (фиксированное значение "tg_bot_update")
*/
type: "tg_bot_update";
/**
* Данные обновления Telegram
*/
data: unknown;
}
@@ -0,0 +1,55 @@
import {
TgBotUpdateWebhookDesignDocument,
TgBotUpdateDocument,
} from "../types";
export type { TgBotUpdateWebhookDesignDocument };
type CouchRequest = {
body: string; // JSON-строка
headers: Record<string, string>; // Заголовки запроса
method: string; // HTTP-метод (обычно POST для update handlers)
query: Record<string, string>; // Параметры запроса (например, ?key=value)
userCtx: {
name: string | null; // Имя пользователя, если используется аутентификация
roles: string[]; // Роли пользователя
};
};
type CouchResponse = [
TgBotUpdateDocument | null,
{ code: number; body: string } | string,
];
type UpdateFunction = (
doc: TgBotUpdateDocument | null,
req: CouchRequest,
) => CouchResponse;
export const saveUpdate: UpdateFunction = function (
this: TgBotUpdateWebhookDesignDocument,
doc: TgBotUpdateDocument | null,
req: CouchRequest,
): CouchResponse {
const token = req.headers["X-Telegram-Bot-Api-Secret-Token"];
if (token !== this.constants.telegram_secret_token) {
return [null, { code: 403, body: "Forbidden: Invalid token" }];
}
const data = JSON.parse(req.body);
if (doc) {
return [null, { code: 304, body: "Update already exists" }];
}
doc = {
_id: "tg_bot_update:" + data.update_id.toString(),
type: "tg_bot_update",
data: data,
created_at: new Date().toISOString(),
};
return [doc, "Message saved"];
};
export const saveUpdateString = saveUpdate.toString();