Dmitry Beresnev commited on
Commit
fe216f3
·
1 Parent(s): 58b0b2a

fix tg bot

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. src/telegram_bot.py +25 -2
requirements.txt CHANGED
@@ -111,4 +111,5 @@ isort>=5.10.0
111
  # - bcrypt (not essential for basic functionality)
112
 
113
  finnhub-python>=2.4.0
114
- python-telegram-bot>=20.0
 
 
111
  # - bcrypt (not essential for basic functionality)
112
 
113
  finnhub-python>=2.4.0
114
+ python-telegram-bot>=20.0
115
+ httpx==0.25.0
src/telegram_bot.py CHANGED
@@ -4,14 +4,22 @@ import sys
4
  from typing import Any
5
 
6
  from telegram import Update
7
- from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
8
  from dotenv import load_dotenv
9
  from src.financial_news_requester import fetch_comp_financial_news
10
 
11
 
 
 
 
 
12
  load_dotenv()
13
 
14
  TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
 
 
 
 
15
 
16
 
17
  def format_news_for_telegram(news_json: list[dict[str, Any]]) -> str:
@@ -38,9 +46,24 @@ async def run_crew(update: Update, context: ContextTypes.DEFAULT_TYPE):
38
  except Exception as e:
39
  await update.message.reply_text(f"Error: {e}")
40
 
41
- if __name__ == "__main__":
 
42
  logging.basicConfig(level=logging.INFO)
43
  app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
44
  app.add_handler(CommandHandler("start", start))
45
  app.add_handler(CommandHandler("run", run_crew))
46
  app.run_polling()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from typing import Any
5
 
6
  from telegram import Update
7
+ from telegram.ext import Application, ApplicationBuilder, CommandHandler, ContextTypes
8
  from dotenv import load_dotenv
9
  from src.financial_news_requester import fetch_comp_financial_news
10
 
11
 
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
  load_dotenv()
17
 
18
  TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
19
+ if not TELEGRAM_TOKEN:
20
+ logger.error("TELEGRAM_TOKEN not found! Please add it in Space Settings > Repository secrets")
21
+ raise ValueError("TELEGRAM_TOKEN not found in environment variables")
22
+ SPACE_ID = os.environ.get('SPACE_ID', 'ResearchEngineering/news_sentiment_analyzer')
23
 
24
 
25
  def format_news_for_telegram(news_json: list[dict[str, Any]]) -> str:
 
46
  except Exception as e:
47
  await update.message.reply_text(f"Error: {e}")
48
 
49
+
50
+ def func():
51
  logging.basicConfig(level=logging.INFO)
52
  app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
53
  app.add_handler(CommandHandler("start", start))
54
  app.add_handler(CommandHandler("run", run_crew))
55
  app.run_polling()
56
+
57
+
58
+ if __name__ == "__main__":
59
+ application = Application.builder().token(TELEGRAM_TOKEN).build()
60
+ application.add_handler(CommandHandler("start", start))
61
+ PORT = int(os.environ.get('PORT', 8000))
62
+
63
+ # Run webhook (better for HF Spaces)
64
+ application.run_webhook(
65
+ listen="0.0.0.0",
66
+ port=PORT,
67
+ url_path=TELEGRAM_TOKEN,
68
+ webhook_url=f"https://{os.environ.get('SPACE_ID', SPACE_ID)}.hf.space/{TELEGRAM_TOKEN}"
69
+ )