Spaces:
Runtime error
Runtime error
Dmitry Beresnev commited on
Commit ·
7debc6c
1
Parent(s): 6c0eb4d
fix bot for local env
Browse files- src/telegram_bot.py +20 -23
src/telegram_bot.py
CHANGED
|
@@ -57,30 +57,27 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 57 |
|
| 58 |
async def run_crew(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 59 |
await update.message.reply_text("Fetching latest financial news...")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
await update.message.reply_text(chunk, parse_mode='HTML')
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
await update.message.reply_text(formatted_news, parse_mode='HTML')
|
| 80 |
-
|
| 81 |
-
#except Exception as e:
|
| 82 |
-
# logger.error(f"Error in run_crew: {e}")
|
| 83 |
-
# await update.message.reply_text(f"Sorry, there was an error fetching news: {str(e)}")
|
| 84 |
|
| 85 |
|
| 86 |
# Add handlers to the global application
|
|
|
|
| 57 |
|
| 58 |
async def run_crew(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 59 |
await update.message.reply_text("Fetching latest financial news...")
|
| 60 |
+
try:
|
| 61 |
+
feed = fetch_comp_financial_news()
|
| 62 |
+
logger.info(f"Processed: {len(feed)} news items")
|
| 63 |
+
formatted_news = format_news_for_telegram(feed)
|
| 64 |
+
# Split message if too long (Telegram limit is 4096 characters)
|
| 65 |
+
if len(formatted_news) > 4000:
|
| 66 |
+
# Split by news items, not by character count
|
| 67 |
+
items = formatted_news.split('\n\n')
|
| 68 |
+
chunk = ""
|
| 69 |
+
for item in items:
|
| 70 |
+
if len(chunk) + len(item) + 2 > 4000:
|
| 71 |
+
await update.message.reply_text(chunk, parse_mode='HTML')
|
| 72 |
+
chunk = ""
|
| 73 |
+
chunk += item + "\n\n"
|
| 74 |
+
if chunk:
|
| 75 |
await update.message.reply_text(chunk, parse_mode='HTML')
|
| 76 |
+
else:
|
| 77 |
+
await update.message.reply_text(formatted_news, parse_mode='HTML')
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Error in run_crew: {e}")
|
| 80 |
+
await update.message.reply_text(f"Sorry, there was an error fetching news: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
# Add handlers to the global application
|