Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/services/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from "../utils/logger";
import { InlineKeyboard, Bot } from "grammy";
import { ChannelModel, ChannelDoc } from "../models/Channel";
import { UserBotModel } from "../models/UserBot";
import { getOrCreateUserBot, forceStopBot } from "./userBotRegistry";
import { getActiveBotInstance } from "./userBotRegistry";
import { BotContext } from "../telegram/bot";
import { decrypt } from "../utils/crypto";

Expand Down Expand Up @@ -47,8 +47,12 @@ export async function publishPersonal(
"Publisher: creating API-only bot & checking permissions",
);

// Always use the polling bot instance for personal bots
const personalBot = await getOrCreateUserBot(userBotRecord.botId);
const personalBot = getActiveBotInstance(userBotRecord.botId);
if (!personalBot) {
throw new Error(
"Personal bot inactive. Start or re-add your personal bot first (use /mybot to verify status).",
);
}

const botMember = await personalBot.api.getChatMember(
chatId,
Expand Down
8 changes: 8 additions & 0 deletions src/services/userBotRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ function initial(): SessionData {
return {};
}

export function getActiveBotInstance(botId: number): Bot<BotContext> | undefined {
const meta = activeBots.get(botId);
if (meta && meta.isRunning && meta.bot.isRunning()) {
return meta.bot;
}
return undefined;
}

export async function getOrCreateUserBot(botId: number) {
// Global lock to prevent any concurrent bot creation
while (globalCreationLock) {
Expand Down