This commit is contained in:
@@ -4,7 +4,7 @@ import { MyContext } from "../types/myContext";
|
||||
import { env } from "../config/env";
|
||||
import { CouchDBStorageAdapter } from "./storageAdapters/CouchDBStorageAdapter";
|
||||
import { loadUserMiddleware } from "./middleware/loadUser";
|
||||
import { replayToQrCodeMessageMiddleware } from "./middleware/replayToQrCodeMessageMiddleware";
|
||||
import { forwardQrCodeMessageMiddleware } from "./middleware/forwardQrCodeMessageMiddleware";
|
||||
import { tgBotSessionsDb } from "../dbs";
|
||||
import { startCommand } from "./commands/start";
|
||||
import { helpCommand } from "./commands/help";
|
||||
@@ -44,7 +44,7 @@ export const createBot = ({
|
||||
bot.use(loadUserMiddleware);
|
||||
|
||||
// Middleware для обработки ответов на сообщения
|
||||
bot.use(replayToQrCodeMessageMiddleware);
|
||||
bot.use(forwardQrCodeMessageMiddleware);
|
||||
|
||||
// Подключаем conversations для многошаговых сценариев
|
||||
bot.use(conversations());
|
||||
|
||||
+9
-14
@@ -5,6 +5,8 @@ import { secondsToISOString } from "../../utils/date";
|
||||
import { env } from "../../config/env";
|
||||
import { escapeHtml } from "../../utils/escapeHtml";
|
||||
|
||||
const qrCodeMessageRegexp = /#qr(?<qr_code_uri>\S+)_c(?<chat>\S+)/;
|
||||
|
||||
/**
|
||||
* Middleware for handling replies to messages containing QR code identifiers.
|
||||
*
|
||||
@@ -17,22 +19,18 @@ import { escapeHtml } from "../../utils/escapeHtml";
|
||||
* @param ctx - The context object containing the message and other request details.
|
||||
* @param next - The next middleware function in the stack to be executed.
|
||||
*/
|
||||
export const replayToQrCodeMessageMiddleware = async (
|
||||
export const forwardQrCodeMessageMiddleware = async (
|
||||
ctx: MyContext,
|
||||
next: () => Promise<void>,
|
||||
) => {
|
||||
if (!ctx.has(":text")) {
|
||||
return next();
|
||||
}
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
console.log("Это не ответное сообщение.");
|
||||
return next();
|
||||
}
|
||||
|
||||
// Получаем текст сообщения, на которое пришёл ответ
|
||||
const originalMessage = ctx.msg.reply_to_message.text;
|
||||
const m =
|
||||
ctx.msg.reply_to_message?.text?.match(qrCodeMessageRegexp) ||
|
||||
ctx.msg.text.match(qrCodeMessageRegexp);
|
||||
|
||||
const m = originalMessage?.match(/#qr(?<qr_code_uri>\S+)_c(?<chat>\S+)/);
|
||||
if (!m?.groups) {
|
||||
console.log("m.groups is empty", m, m?.groups);
|
||||
await ctx.reply(
|
||||
@@ -42,11 +40,6 @@ export const replayToQrCodeMessageMiddleware = async (
|
||||
}
|
||||
|
||||
const { qr_code_uri, chat } = m.groups;
|
||||
console.log(qr_code_uri, chat);
|
||||
|
||||
// Логируем исходное сообщение и ответ
|
||||
console.log("Ответ на сообщение: ", originalMessage);
|
||||
console.log("Ответное сообщение: ", ctx.msg.text);
|
||||
|
||||
const qrCodeDoc = await qrDb.get(`qr_code:${qr_code_uri}`).catch((error) => {
|
||||
if (error.name === "not_found") return;
|
||||
@@ -90,12 +83,14 @@ export const replayToQrCodeMessageMiddleware = async (
|
||||
created_at,
|
||||
};
|
||||
|
||||
await ctx.replyWithChatAction("typing");
|
||||
await messagesDb.put(message);
|
||||
|
||||
const hashTag = `#qr${qr_code_uri}_c${chat}`;
|
||||
const manageMessageUrl = `${env.APP_BASE_URL}/manage/qr/${qr_code_uri}/chat/${chat}#message-${encodeURIComponent(created_at)}`;
|
||||
const text = `<i>переслано сообщение в <a href="${escapeHtml(manageMessageUrl)}">диалог QR-кода <b>«${escapeHtml(qrCodeDoc.name)}»</b></a> ${escapeHtml(hashTag)}</i>`;
|
||||
const text = `<i><a href="${escapeHtml(manageMessageUrl)}">диалог QR-кода <b>«${escapeHtml(qrCodeDoc.name)}»</b></a> ${escapeHtml(hashTag)}</i>`;
|
||||
|
||||
// await ctx.react("⚡");
|
||||
await ctx.reply(text, {
|
||||
parse_mode: "HTML",
|
||||
link_preview_options: { is_disabled: true },
|
||||
Reference in New Issue
Block a user