File size: 1,834 Bytes
b9cc0a0
ac55002
b9cc0a0
 
 
 
ac55002
b9cc0a0
 
 
ac55002
 
 
 
 
 
b9cc0a0
 
48f7491
b9cc0a0
ac55002
b9cc0a0
 
 
 
 
 
 
 
 
 
 
 
ac55002
b9cc0a0
 
 
 
 
 
ac55002
b9cc0a0
 
 
 
ac55002
b9cc0a0
 
 
 
 
 
 
 
 
 
 
ac55002
b9cc0a0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import logging
import os
import requests
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext


# Set up logging
logging.basicConfig(level=logging.INFO)


# Get the Telegram Bot Token from environment variables
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
if not TOKEN:
    raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN in environment variables.")
    

# Flask API URL
API_URL = "https://demaking-decision-helper-bot.hf.space/ask"


# Function to send user input to Flask API and return the response
def get_response_from_api(user_text):
    try:
        response = requests.post(API_URL, json={"text": user_text})
        if response.status_code == 200:
            return response.json().get("response", "Error: No response received")
        else:
            return "Error: API request failed"
    except Exception as e:
        logging.error(f"Error connecting to API: {e}")
        return "Error: Could not connect to the server"


# Function to handle incoming messages from users
def handle_message(update: Update, context: CallbackContext):
    user_text = update.message.text
    response = get_response_from_api(user_text)
    update.message.reply_text(response)


# Function to handle /start command
def start(update: Update, context: CallbackContext):
    update.message.reply_text("Hello! Tell me your Decision-Making issue in HE or EN, and I'll try to help you.")


# Initialize Telegram bot
def run_telegram_bot():
    updater = Updater(TOKEN, use_context=True)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))

    updater.start_polling()
    updater.idle()


if __name__ == "__main__":
    run_telegram_bot()