Update chat events/transcription flow and container startup fixes
This commit is contained in:
@@ -1,92 +1,43 @@
|
||||
import { Queue, Worker, JobsOptions } from "bullmq";
|
||||
import { getRedis } from "../utils/redis";
|
||||
import type { JobsOptions } from "bullmq";
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { telegramBotApi } from "../utils/telegram";
|
||||
|
||||
export const TELEGRAM_SEND_QUEUE_NAME = "telegram:send";
|
||||
import { telegramApiBase, requireTelegramBotToken } from "../utils/telegram";
|
||||
import { enqueueOutboundDelivery, startOutboundDeliveryWorker } from "./outboundDelivery";
|
||||
|
||||
type TelegramSendJob = {
|
||||
omniMessageId: string;
|
||||
};
|
||||
|
||||
export function telegramSendQueue() {
|
||||
return new Queue<TelegramSendJob>(TELEGRAM_SEND_QUEUE_NAME, {
|
||||
connection: getRedis(),
|
||||
defaultJobOptions: {
|
||||
removeOnComplete: { count: 1000 },
|
||||
removeOnFail: { count: 5000 },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function enqueueTelegramSend(input: TelegramSendJob, opts?: JobsOptions) {
|
||||
const q = telegramSendQueue();
|
||||
return q.add("send", input, {
|
||||
jobId: input.omniMessageId, // idempotency
|
||||
attempts: 10,
|
||||
backoff: { type: "exponential", delay: 1000 },
|
||||
...opts,
|
||||
const msg = await prisma.omniMessage.findUnique({
|
||||
where: { id: input.omniMessageId },
|
||||
include: { thread: true },
|
||||
});
|
||||
}
|
||||
if (!msg) throw new Error(`omni message not found: ${input.omniMessageId}`);
|
||||
if (msg.channel !== "TELEGRAM" || msg.direction !== "OUT") {
|
||||
throw new Error(`Invalid omni message for telegram send: ${msg.id}`);
|
||||
}
|
||||
|
||||
export function startTelegramSendWorker() {
|
||||
return new Worker<TelegramSendJob>(
|
||||
TELEGRAM_SEND_QUEUE_NAME,
|
||||
async (job) => {
|
||||
const msg = await prisma.omniMessage.findUnique({
|
||||
where: { id: job.data.omniMessageId },
|
||||
include: { thread: true },
|
||||
});
|
||||
if (!msg) return;
|
||||
const token = requireTelegramBotToken();
|
||||
const endpoint = `${telegramApiBase()}/bot${token}/sendMessage`;
|
||||
const payload = {
|
||||
chat_id: msg.thread.externalChatId,
|
||||
text: msg.text,
|
||||
...(msg.thread.businessConnectionId ? { business_connection_id: msg.thread.businessConnectionId } : {}),
|
||||
};
|
||||
|
||||
// Idempotency: if we already sent it, don't send twice.
|
||||
if (msg.status === "SENT" && msg.providerMessageId) return;
|
||||
|
||||
if (msg.channel !== "TELEGRAM" || msg.direction !== "OUT") {
|
||||
throw new Error(`Invalid omni message for telegram send: ${msg.id}`);
|
||||
}
|
||||
|
||||
const thread = msg.thread;
|
||||
const chatId = thread.externalChatId;
|
||||
const businessConnectionId = thread.businessConnectionId || undefined;
|
||||
|
||||
try {
|
||||
const result = await telegramBotApi<any>("sendMessage", {
|
||||
chat_id: chatId,
|
||||
text: msg.text,
|
||||
...(businessConnectionId ? { business_connection_id: businessConnectionId } : {}),
|
||||
});
|
||||
|
||||
const providerMessageId = result?.message_id != null ? String(result.message_id) : null;
|
||||
await prisma.omniMessage.update({
|
||||
where: { id: msg.id },
|
||||
data: {
|
||||
status: "SENT",
|
||||
providerMessageId: providerMessageId,
|
||||
rawJson: result,
|
||||
},
|
||||
});
|
||||
} catch (e: any) {
|
||||
const isLastAttempt =
|
||||
typeof job.opts.attempts === "number" && job.attemptsMade + 1 >= job.opts.attempts;
|
||||
|
||||
if (isLastAttempt) {
|
||||
await prisma.omniMessage.update({
|
||||
where: { id: msg.id },
|
||||
data: {
|
||||
status: "FAILED",
|
||||
rawJson: {
|
||||
error: String(e?.message || e),
|
||||
attemptsMade: job.attemptsMade + 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
return enqueueOutboundDelivery(
|
||||
{
|
||||
omniMessageId: msg.id,
|
||||
endpoint,
|
||||
method: "POST",
|
||||
payload,
|
||||
provider: "telegram_business",
|
||||
channel: "TELEGRAM",
|
||||
},
|
||||
{ connection: getRedis() },
|
||||
opts,
|
||||
);
|
||||
}
|
||||
|
||||
export function startTelegramSendWorker() {
|
||||
return startOutboundDeliveryWorker();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user