Update telegram_bot.py
Browse files- telegram_bot.py +13 -1
telegram_bot.py
CHANGED
|
@@ -1,16 +1,24 @@
|
|
| 1 |
import logging
|
|
|
|
| 2 |
import requests
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
| 5 |
|
|
|
|
| 6 |
# Set up logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Flask API URL
|
| 12 |
API_URL = "https://demaking-decision-helper-bot.hf.space/ask"
|
| 13 |
|
|
|
|
| 14 |
# Function to send user input to Flask API and return the response
|
| 15 |
def get_response_from_api(user_text):
|
| 16 |
try:
|
|
@@ -23,16 +31,19 @@ def get_response_from_api(user_text):
|
|
| 23 |
logging.error(f"Error connecting to API: {e}")
|
| 24 |
return "Error: Could not connect to the server"
|
| 25 |
|
|
|
|
| 26 |
# Function to handle incoming messages from users
|
| 27 |
def handle_message(update: Update, context: CallbackContext):
|
| 28 |
user_text = update.message.text
|
| 29 |
response = get_response_from_api(user_text)
|
| 30 |
update.message.reply_text(response)
|
| 31 |
|
|
|
|
| 32 |
# Function to handle /start command
|
| 33 |
def start(update: Update, context: CallbackContext):
|
| 34 |
update.message.reply_text("Hello! Tell me your Decision-Making issue in HE or EN, and I'll try to help you.")
|
| 35 |
|
|
|
|
| 36 |
# Initialize Telegram bot
|
| 37 |
def run_telegram_bot():
|
| 38 |
updater = Updater(TOKEN, use_context=True)
|
|
@@ -44,5 +55,6 @@ def run_telegram_bot():
|
|
| 44 |
updater.start_polling()
|
| 45 |
updater.idle()
|
| 46 |
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
run_telegram_bot()
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import os
|
| 3 |
import requests
|
| 4 |
from telegram import Update
|
| 5 |
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
| 6 |
|
| 7 |
+
|
| 8 |
# Set up logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
|
| 11 |
+
|
| 12 |
+
# Get the Telegram Bot Token from environment variables
|
| 13 |
+
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 14 |
+
if not TOKEN:
|
| 15 |
+
raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN in environment variables.")
|
| 16 |
+
|
| 17 |
|
| 18 |
# Flask API URL
|
| 19 |
API_URL = "https://demaking-decision-helper-bot.hf.space/ask"
|
| 20 |
|
| 21 |
+
|
| 22 |
# Function to send user input to Flask API and return the response
|
| 23 |
def get_response_from_api(user_text):
|
| 24 |
try:
|
|
|
|
| 31 |
logging.error(f"Error connecting to API: {e}")
|
| 32 |
return "Error: Could not connect to the server"
|
| 33 |
|
| 34 |
+
|
| 35 |
# Function to handle incoming messages from users
|
| 36 |
def handle_message(update: Update, context: CallbackContext):
|
| 37 |
user_text = update.message.text
|
| 38 |
response = get_response_from_api(user_text)
|
| 39 |
update.message.reply_text(response)
|
| 40 |
|
| 41 |
+
|
| 42 |
# Function to handle /start command
|
| 43 |
def start(update: Update, context: CallbackContext):
|
| 44 |
update.message.reply_text("Hello! Tell me your Decision-Making issue in HE or EN, and I'll try to help you.")
|
| 45 |
|
| 46 |
+
|
| 47 |
# Initialize Telegram bot
|
| 48 |
def run_telegram_bot():
|
| 49 |
updater = Updater(TOKEN, use_context=True)
|
|
|
|
| 55 |
updater.start_polling()
|
| 56 |
updater.idle()
|
| 57 |
|
| 58 |
+
|
| 59 |
if __name__ == "__main__":
|
| 60 |
run_telegram_bot()
|