Spaces:
Running
Running
Fix: Add try/catch to AI banner functions to prevent bot crash when models timeout
Browse files- bot/bot.js +34 -16
bot/bot.js
CHANGED
|
@@ -16,7 +16,7 @@ dns.setDefaultResultOrder('ipv4first');
|
|
| 16 |
function downloadImage(url) {
|
| 17 |
return new Promise((resolve, reject) => {
|
| 18 |
const protocol = url.startsWith('https') ? https : http;
|
| 19 |
-
const req = protocol.get(url, { timeout:
|
| 20 |
// Redirect bo'lsa โ kuzatib borish
|
| 21 |
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
| 22 |
return downloadImage(res.headers.location).then(resolve).catch(reject);
|
|
@@ -887,9 +887,9 @@ if (process.env.BOT_TOKEN && process.env.BOT_TOKEN !== 'YOUR_TELEGRAM_BOT_TOKEN'
|
|
| 887 |
const url1 = `https://image.pollinations.ai/prompt/${encoded}?width=1024&height=430&seed=${ts1}&nologo=true&model=gptimage`;
|
| 888 |
const url2 = `https://image.pollinations.ai/prompt/${encoded}?width=1024&height=430&seed=${ts2}&nologo=true&model=seedream5`;
|
| 889 |
|
| 890 |
-
await ctx.reply('โณ *2 ta super model* ishga tushmoqda...\n\n๐น GPT Image (OpenAI)\n๐น Seedream5 (Google)\n\n_Kuting, 20-60 soniya..._', { parse_mode: 'Markdown' });
|
| 891 |
-
|
| 892 |
try {
|
|
|
|
|
|
|
| 893 |
// Ikkalasini xavfsiz yuklab olish qotib qolmasligi uchun biroz kutamiz (stagger resquests)
|
| 894 |
let res1, res2;
|
| 895 |
try { res1 = await downloadImage(url1); } catch (err) { res1 = null; console.error("URL1 fail:", err); }
|
|
@@ -1183,29 +1183,47 @@ if (process.env.BOT_TOKEN && process.env.BOT_TOKEN !== 'YOUR_TELEGRAM_BOT_TOKEN'
|
|
| 1183 |
|
| 1184 |
// AI Banner โ Shablon bilan
|
| 1185 |
if (state.step === 'ai_template_prompt') {
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1191 |
return;
|
| 1192 |
}
|
| 1193 |
|
| 1194 |
// AI Banner โ Erkin yozish
|
| 1195 |
if (state.step === 'ai_free_prompt_input') {
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
| 1199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1200 |
return;
|
| 1201 |
}
|
| 1202 |
|
| 1203 |
// AI Banner (eski usul backup)
|
| 1204 |
if (state.step === 'ai_banner_prompt') {
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
|
| 1208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1209 |
return;
|
| 1210 |
}
|
| 1211 |
|
|
|
|
| 16 |
function downloadImage(url) {
|
| 17 |
return new Promise((resolve, reject) => {
|
| 18 |
const protocol = url.startsWith('https') ? https : http;
|
| 19 |
+
const req = protocol.get(url, { timeout: 45000 }, (res) => {
|
| 20 |
// Redirect bo'lsa โ kuzatib borish
|
| 21 |
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
| 22 |
return downloadImage(res.headers.location).then(resolve).catch(reject);
|
|
|
|
| 887 |
const url1 = `https://image.pollinations.ai/prompt/${encoded}?width=1024&height=430&seed=${ts1}&nologo=true&model=gptimage`;
|
| 888 |
const url2 = `https://image.pollinations.ai/prompt/${encoded}?width=1024&height=430&seed=${ts2}&nologo=true&model=seedream5`;
|
| 889 |
|
|
|
|
|
|
|
| 890 |
try {
|
| 891 |
+
await ctx.reply('โณ *2 ta super model* ishga tushmoqda...\n\n๐น GPT Image (OpenAI)\n๐น Seedream5 (Google)\n\n_Kuting, 20-60 soniya..._', { parse_mode: 'Markdown' });
|
| 892 |
+
|
| 893 |
// Ikkalasini xavfsiz yuklab olish qotib qolmasligi uchun biroz kutamiz (stagger resquests)
|
| 894 |
let res1, res2;
|
| 895 |
try { res1 = await downloadImage(url1); } catch (err) { res1 = null; console.error("URL1 fail:", err); }
|
|
|
|
| 1183 |
|
| 1184 |
// AI Banner โ Shablon bilan
|
| 1185 |
if (state.step === 'ai_template_prompt') {
|
| 1186 |
+
try {
|
| 1187 |
+
const userText = text.toLowerCase() === 'davom' ? '' : text;
|
| 1188 |
+
await ctx.reply('๐ง Gemini loyihangizni anglab, mukammal prompt tuzmoqda...');
|
| 1189 |
+
const finalPrompt = await enhancePromptWithGemini(userText, state.templateBase);
|
| 1190 |
+
appState[userId] = { step: 'ai_generating', lastPrompt: text, templateBase: state.templateBase, finalPrompt };
|
| 1191 |
+
await generateAIBannerVariants(ctx, finalPrompt, text || aiTemplates[state.templateKey]?.label || 'AI Banner');
|
| 1192 |
+
} catch (e) {
|
| 1193 |
+
console.error("ai_template_prompt error:", e);
|
| 1194 |
+
ctx.reply('โ Kechirasiz, tarmoqda uzilish yuz berdi. Iltimos qayta urining.', getMainMenu());
|
| 1195 |
+
delete appState[userId];
|
| 1196 |
+
}
|
| 1197 |
return;
|
| 1198 |
}
|
| 1199 |
|
| 1200 |
// AI Banner โ Erkin yozish
|
| 1201 |
if (state.step === 'ai_free_prompt_input') {
|
| 1202 |
+
try {
|
| 1203 |
+
await ctx.reply('๐ง Gemini sizning fikringiz asosida mukammal prompt tuzmoqda...');
|
| 1204 |
+
const finalPrompt = await enhancePromptWithGemini(text);
|
| 1205 |
+
appState[userId] = { step: 'ai_generating', lastPrompt: text, finalPrompt };
|
| 1206 |
+
await generateAIBannerVariants(ctx, finalPrompt, text);
|
| 1207 |
+
} catch (e) {
|
| 1208 |
+
console.error("ai_free_prompt_input error:", e);
|
| 1209 |
+
ctx.reply('โ Kechirasiz, tarmoqda xatolik yuz berdi. Iltimos qayta urining.', getMainMenu());
|
| 1210 |
+
delete appState[userId];
|
| 1211 |
+
}
|
| 1212 |
return;
|
| 1213 |
}
|
| 1214 |
|
| 1215 |
// AI Banner (eski usul backup)
|
| 1216 |
if (state.step === 'ai_banner_prompt') {
|
| 1217 |
+
try {
|
| 1218 |
+
await ctx.reply('๐ง Gemini orqali prompt tayyorlanmoqda...');
|
| 1219 |
+
const finalPrompt = await enhancePromptWithGemini(text);
|
| 1220 |
+
appState[userId] = { step: 'ai_generating', lastPrompt: text, finalPrompt };
|
| 1221 |
+
await generateAIBannerVariants(ctx, finalPrompt, text);
|
| 1222 |
+
} catch (e) {
|
| 1223 |
+
console.error("ai_banner_prompt error:", e);
|
| 1224 |
+
ctx.reply('โ Kechirasiz, tarmoqda xatolik yuz berdi. Iltimos qayta urining.', getMainMenu());
|
| 1225 |
+
delete appState[userId];
|
| 1226 |
+
}
|
| 1227 |
return;
|
| 1228 |
}
|
| 1229 |
|