Mohammed Foud commited on
Commit ·
9c3af50
1
Parent(s): 4857c79
all
Browse files- src/bots/botManager.ts +36 -12
- src/bots/index.ts +26 -15
src/bots/botManager.ts
CHANGED
|
@@ -75,7 +75,15 @@ export const initializeBot = async (botToken: string, botData?: BotData) => {
|
|
| 75 |
setupCallbackHandlers(bot);
|
| 76 |
|
| 77 |
// Set bot commands
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
return {
|
| 81 |
success: true,
|
|
@@ -88,6 +96,26 @@ export const initializeBot = async (botToken: string, botData?: BotData) => {
|
|
| 88 |
}
|
| 89 |
};
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
export const stopBot = async (botId: string) => {
|
| 92 |
try {
|
| 93 |
// Get bot token from database
|
|
@@ -98,6 +126,7 @@ export const stopBot = async (botId: string) => {
|
|
| 98 |
.single();
|
| 99 |
|
| 100 |
if (error || !botData) {
|
|
|
|
| 101 |
return { success: false, message: "Bot not found in database" };
|
| 102 |
}
|
| 103 |
|
|
@@ -109,22 +138,17 @@ export const stopBot = async (botId: string) => {
|
|
| 109 |
telegrafBots.delete(botToken);
|
| 110 |
|
| 111 |
// Update database
|
| 112 |
-
await
|
| 113 |
-
.from('bots')
|
| 114 |
-
.update({
|
| 115 |
-
is_active: false,
|
| 116 |
-
state: {
|
| 117 |
-
status: 'stopped',
|
| 118 |
-
stoppedAt: new Date().toISOString()
|
| 119 |
-
}
|
| 120 |
-
})
|
| 121 |
-
.eq('id', botId);
|
| 122 |
-
|
| 123 |
return { success: true, message: "Bot stopped successfully" };
|
| 124 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
return { success: false, message: "Bot instance not found" };
|
| 126 |
} catch (error: any) {
|
| 127 |
logger.error(`Error stopping bot: ${error.message}`);
|
|
|
|
| 128 |
return { success: false, message: `Failed to stop bot: ${error.message}` };
|
| 129 |
}
|
| 130 |
};
|
|
|
|
| 75 |
setupCallbackHandlers(bot);
|
| 76 |
|
| 77 |
// Set bot commands
|
| 78 |
+
try {
|
| 79 |
+
await bot.telegram.setMyCommands(BotCommands, {
|
| 80 |
+
scope: { type: 'default' }
|
| 81 |
+
});
|
| 82 |
+
logger.info('Bot commands set successfully');
|
| 83 |
+
} catch (error: any) {
|
| 84 |
+
logger.error(`Failed to set bot commands: ${error.message}`);
|
| 85 |
+
// Continue initialization even if setting commands fails
|
| 86 |
+
}
|
| 87 |
|
| 88 |
return {
|
| 89 |
success: true,
|
|
|
|
| 96 |
}
|
| 97 |
};
|
| 98 |
|
| 99 |
+
// Utility function to update bot status in database
|
| 100 |
+
const updateBotStatus = async (botId: string, isActive: boolean, error?: string) => {
|
| 101 |
+
try {
|
| 102 |
+
await supabase
|
| 103 |
+
.from('bots')
|
| 104 |
+
.update({
|
| 105 |
+
is_active: isActive,
|
| 106 |
+
last_activity: new Date().toISOString(),
|
| 107 |
+
state: {
|
| 108 |
+
status: isActive ? 'running' : 'error',
|
| 109 |
+
startedAt: isActive ? new Date().toISOString() : undefined,
|
| 110 |
+
error: error
|
| 111 |
+
}
|
| 112 |
+
})
|
| 113 |
+
.eq('id', botId);
|
| 114 |
+
} catch (error: any) {
|
| 115 |
+
logger.error(`Error updating bot status: ${error.message}`);
|
| 116 |
+
}
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
export const stopBot = async (botId: string) => {
|
| 120 |
try {
|
| 121 |
// Get bot token from database
|
|
|
|
| 126 |
.single();
|
| 127 |
|
| 128 |
if (error || !botData) {
|
| 129 |
+
await updateBotStatus(botId, false, "Bot not found in database");
|
| 130 |
return { success: false, message: "Bot not found in database" };
|
| 131 |
}
|
| 132 |
|
|
|
|
| 138 |
telegrafBots.delete(botToken);
|
| 139 |
|
| 140 |
// Update database
|
| 141 |
+
await updateBotStatus(botId, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return { success: true, message: "Bot stopped successfully" };
|
| 143 |
}
|
| 144 |
+
|
| 145 |
+
// Bot instance not found
|
| 146 |
+
await updateBotStatus(botId, false, "Bot instance not found");
|
| 147 |
+
|
| 148 |
return { success: false, message: "Bot instance not found" };
|
| 149 |
} catch (error: any) {
|
| 150 |
logger.error(`Error stopping bot: ${error.message}`);
|
| 151 |
+
await updateBotStatus(botId, false, error.message);
|
| 152 |
return { success: false, message: `Failed to stop bot: ${error.message}` };
|
| 153 |
}
|
| 154 |
};
|
src/bots/index.ts
CHANGED
|
@@ -6,6 +6,26 @@ import { setupPaymentWebhookHandlers } from './handlers/paymentWebhookHandlers';
|
|
| 6 |
|
| 7 |
const logger = createLogger('BotManager');
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
export const handleAddTelegrafBot = async (botId: string) => {
|
| 10 |
if (!botId) {
|
| 11 |
return { status: 400, data: { error: "Bot ID is required" } };
|
|
@@ -50,22 +70,9 @@ export const handleAddTelegrafBot = async (botId: string) => {
|
|
| 50 |
telegrafBots.set(botData.bot_token, result.bot);
|
| 51 |
|
| 52 |
try {
|
| 53 |
-
//
|
| 54 |
-
|
| 55 |
|
| 56 |
-
// Update last_activity and is_active in database
|
| 57 |
-
await supabase
|
| 58 |
-
.from('bots')
|
| 59 |
-
.update({
|
| 60 |
-
is_active: true,
|
| 61 |
-
last_activity: new Date().toISOString(),
|
| 62 |
-
state: {
|
| 63 |
-
status: 'running',
|
| 64 |
-
startedAt: new Date().toISOString()
|
| 65 |
-
}
|
| 66 |
-
})
|
| 67 |
-
.eq('id', botId);
|
| 68 |
-
|
| 69 |
logger.info(`Bot ${botData.name} (${botId}) launched successfully`);
|
| 70 |
|
| 71 |
await result.bot.launch();
|
|
@@ -84,18 +91,22 @@ export const handleAddTelegrafBot = async (botId: string) => {
|
|
| 84 |
|
| 85 |
} catch (error: any) {
|
| 86 |
logger.error(`Error launching bot: ${error.message}`);
|
|
|
|
| 87 |
return { status: 500, data: { error: `Error launching bot: ${error.message}` } };
|
| 88 |
}
|
| 89 |
} else {
|
| 90 |
logger.error(`Error initializing bot: ${result.message}`);
|
|
|
|
| 91 |
return { status: 500, data: { error: result.message } };
|
| 92 |
}
|
| 93 |
} catch (error: any) {
|
| 94 |
logger.error(`Bot initialization error: ${error.message}`);
|
|
|
|
| 95 |
return { status: 500, data: { error: `Bot initialization error: ${error.message}` } };
|
| 96 |
}
|
| 97 |
} catch (error: any) {
|
| 98 |
logger.error(`Unexpected error: ${error.message}`);
|
|
|
|
| 99 |
return { status: 500, data: { error: `Unexpected error: ${error.message}` } };
|
| 100 |
}
|
| 101 |
};
|
|
|
|
| 6 |
|
| 7 |
const logger = createLogger('BotManager');
|
| 8 |
|
| 9 |
+
// Utility function to update bot status in database
|
| 10 |
+
const updateBotStatus = async (botId: string, isActive: boolean, error?: string) => {
|
| 11 |
+
try {
|
| 12 |
+
await supabase
|
| 13 |
+
.from('bots')
|
| 14 |
+
.update({
|
| 15 |
+
is_active: isActive,
|
| 16 |
+
last_activity: new Date().toISOString(),
|
| 17 |
+
state: {
|
| 18 |
+
status: isActive ? 'running' : 'error',
|
| 19 |
+
startedAt: isActive ? new Date().toISOString() : undefined,
|
| 20 |
+
error: error
|
| 21 |
+
}
|
| 22 |
+
})
|
| 23 |
+
.eq('id', botId);
|
| 24 |
+
} catch (error: any) {
|
| 25 |
+
logger.error(`Error updating bot status: ${error.message}`);
|
| 26 |
+
}
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
export const handleAddTelegrafBot = async (botId: string) => {
|
| 30 |
if (!botId) {
|
| 31 |
return { status: 400, data: { error: "Bot ID is required" } };
|
|
|
|
| 70 |
telegrafBots.set(botData.bot_token, result.bot);
|
| 71 |
|
| 72 |
try {
|
| 73 |
+
// Update bot status to active
|
| 74 |
+
await updateBotStatus(botId, true);
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
logger.info(`Bot ${botData.name} (${botId}) launched successfully`);
|
| 77 |
|
| 78 |
await result.bot.launch();
|
|
|
|
| 91 |
|
| 92 |
} catch (error: any) {
|
| 93 |
logger.error(`Error launching bot: ${error.message}`);
|
| 94 |
+
await updateBotStatus(botId, false, error.message);
|
| 95 |
return { status: 500, data: { error: `Error launching bot: ${error.message}` } };
|
| 96 |
}
|
| 97 |
} else {
|
| 98 |
logger.error(`Error initializing bot: ${result.message}`);
|
| 99 |
+
await updateBotStatus(botId, false, result.message);
|
| 100 |
return { status: 500, data: { error: result.message } };
|
| 101 |
}
|
| 102 |
} catch (error: any) {
|
| 103 |
logger.error(`Bot initialization error: ${error.message}`);
|
| 104 |
+
await updateBotStatus(botId, false, error.message);
|
| 105 |
return { status: 500, data: { error: `Bot initialization error: ${error.message}` } };
|
| 106 |
}
|
| 107 |
} catch (error: any) {
|
| 108 |
logger.error(`Unexpected error: ${error.message}`);
|
| 109 |
+
await updateBotStatus(botId, false, error.message);
|
| 110 |
return { status: 500, data: { error: `Unexpected error: ${error.message}` } };
|
| 111 |
}
|
| 112 |
};
|