Update telegram_bot.py
Browse files- telegram_bot.py +8 -10
telegram_bot.py
CHANGED
|
@@ -2,7 +2,7 @@ import logging
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from telegram import Update
|
| 5 |
-
from telegram.ext import
|
| 6 |
|
| 7 |
|
| 8 |
# Set up logging
|
|
@@ -24,7 +24,7 @@ def get_response_from_api(user_text):
|
|
| 24 |
try:
|
| 25 |
response = requests.post(API_URL, json={"text": user_text})
|
| 26 |
if response.status_code == 200:
|
| 27 |
-
return response.json().get("response", "Error: No response received")
|
| 28 |
else:
|
| 29 |
return "Error: API request failed"
|
| 30 |
except Exception as e:
|
|
@@ -33,27 +33,25 @@ def get_response_from_api(user_text):
|
|
| 33 |
|
| 34 |
|
| 35 |
# Function to handle incoming messages from users
|
| 36 |
-
def handle_message(update: Update
|
| 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
|
| 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 |
-
|
| 50 |
-
dp = updater.dispatcher
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
updater.idle()
|
| 57 |
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from telegram import Update
|
| 5 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
| 6 |
|
| 7 |
|
| 8 |
# Set up logging
|
|
|
|
| 24 |
try:
|
| 25 |
response = requests.post(API_URL, json={"text": user_text})
|
| 26 |
if response.status_code == 200:
|
| 27 |
+
return response.json().get("response", "Error: No response received") # Gets the response or error if response not provided)
|
| 28 |
else:
|
| 29 |
return "Error: API request failed"
|
| 30 |
except Exception as e:
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
# Function to handle incoming messages from users
|
| 36 |
+
def handle_message(update: Update):
|
| 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):
|
| 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 |
+
app = Application.builder().token(TOKEN).build()
|
|
|
|
| 50 |
|
| 51 |
+
app.add_handler(CommandHandler("start", start))
|
| 52 |
+
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 53 |
|
| 54 |
+
app.run_polling()
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|