Update bot.py
Browse files
bot.py
CHANGED
@@ -41,15 +41,14 @@ if not WEBHOOK_DOMAIN:
|
|
41 |
# Define a unique webhook path using the bot token
|
42 |
# WEBHOOK_PATH = f"/{TOKEN}"
|
43 |
# Construct the full webhook URL (must be HTTPS as required by Telegram)
|
44 |
-
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}"
|
45 |
-
TELEGRAM_WEBHOOK = f"https://api.telegram.org/bot{TOKEN}/setwebhook?url=https://{WEBHOOK_URL}"
|
46 |
-
|
47 |
|
48 |
# -------------------------
|
49 |
# API URL of the FastAPI server (running on Hugging Face)
|
50 |
# -------------------------
|
51 |
API_URL = "https://demaking-decision-helper-bot.hf.space/generate_response"
|
52 |
|
|
|
53 |
|
54 |
# -------------------------
|
55 |
# Function to fetch response from FastAPI (unchanged)
|
@@ -102,30 +101,58 @@ async def handle_message(update: Update, context: CallbackContext):
|
|
102 |
# return "!", 200
|
103 |
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
# -------------------------
|
106 |
-
#
|
107 |
# -------------------------
|
108 |
async def set_webhook():
|
109 |
-
bot = Bot(token=TOKEN)
|
110 |
-
#bot.delete_webhook()
|
111 |
-
# This call will set the webhook to the given URL.
|
112 |
-
PATH = WEBHOOK_URL + TOKEN
|
113 |
try:
|
114 |
-
await bot.set_webhook(url=
|
115 |
logger.info(f"Webhook set successfully to: {WEBHOOK_URL}")
|
116 |
-
print("
|
117 |
except Exception as e:
|
118 |
-
logger.error(f"Failed to set webhook
|
119 |
-
print(f"error setting bot webhook. Error: {e}")
|
120 |
-
|
121 |
|
122 |
|
123 |
-
|
124 |
# -------------------------
|
125 |
# Main function to run the bot using Webhook mode
|
126 |
# -------------------------
|
127 |
async def main():
|
|
|
128 |
await set_webhook()
|
|
|
129 |
# Build the Application with the Telegram Bot Token
|
130 |
application = Application.builder().token(TOKEN).build()
|
131 |
|
@@ -135,17 +162,16 @@ async def main():
|
|
135 |
|
136 |
# Log: starting the bot in webhook mode.
|
137 |
logger.info("Starting bot in webhook mode...")
|
138 |
-
|
139 |
|
140 |
|
141 |
# Run the application using webhook mode.
|
142 |
-
# The bot will listen on all interfaces (0.0.0.0) at the specified port
|
143 |
-
# Make sure that the port you choose is allowed by your hosting environment.
|
144 |
await application.run_webhook(
|
145 |
-
listen="0.0.0.0",
|
146 |
-
port=443,
|
147 |
# url_path=TELEGRAM_WEBHOOK, # The webhook path; here, we use the bot token
|
148 |
-
|
149 |
)
|
150 |
|
151 |
|
@@ -160,4 +186,12 @@ if __name__ == "__main__":
|
|
160 |
# Instead of asyncio.run(), which may try to close an already running loop,
|
161 |
# get the current loop and run main() until complete.
|
162 |
loop = asyncio.get_event_loop()
|
163 |
-
loop.run_until_complete(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Define a unique webhook path using the bot token
|
42 |
# WEBHOOK_PATH = f"/{TOKEN}"
|
43 |
# Construct the full webhook URL (must be HTTPS as required by Telegram)
|
44 |
+
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}"
|
|
|
|
|
45 |
|
46 |
# -------------------------
|
47 |
# API URL of the FastAPI server (running on Hugging Face)
|
48 |
# -------------------------
|
49 |
API_URL = "https://demaking-decision-helper-bot.hf.space/generate_response"
|
50 |
|
51 |
+
bot = Bot(token=TOKEN)
|
52 |
|
53 |
# -------------------------
|
54 |
# Function to fetch response from FastAPI (unchanged)
|
|
|
101 |
# return "!", 200
|
102 |
|
103 |
|
104 |
+
# # -------------------------
|
105 |
+
# # Set Telegram webhook
|
106 |
+
# # -------------------------
|
107 |
+
# async def set_webhook():
|
108 |
+
# #bot = Bot(token=TOKEN)
|
109 |
+
# #bot.delete_webhook()
|
110 |
+
# # This call will set the webhook to the given URL.
|
111 |
+
# PATH = WEBHOOK_URL + TOKEN
|
112 |
+
# try:
|
113 |
+
# await bot.set_webhook(url=PATH)
|
114 |
+
# logger.info(f"Webhook set successfully to: {WEBHOOK_URL}")
|
115 |
+
# print("bot webhook success")
|
116 |
+
# except Exception as e:
|
117 |
+
# logger.error(f"Failed to set webhook manually. Error: {e}")
|
118 |
+
# print(f"error setting bot webhook. Error: {e}")
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
# -------------------------
|
123 |
+
# Delete Telegram webhook
|
124 |
+
# -------------------------
|
125 |
+
async def delete_webhook():
|
126 |
+
try:
|
127 |
+
await bot.delete_webhook(drop_pending_updates=True)
|
128 |
+
logger.info("Webhook deleted successfully")
|
129 |
+
print("deleted webhook successfully")
|
130 |
+
except Exception as e:
|
131 |
+
logger.error(f"Failed to delete webhook manually. Error: {e}")
|
132 |
+
print(f"error deleting bot webhook. Error: {e}")
|
133 |
+
|
134 |
+
|
135 |
# -------------------------
|
136 |
+
# Set Telegram webhook
|
137 |
# -------------------------
|
138 |
async def set_webhook():
|
139 |
+
# bot = Bot(token=TOKEN)
|
|
|
|
|
|
|
140 |
try:
|
141 |
+
await bot.set_webhook(url=WEBHOOK_URL) # This call will set the webhook to the given URL.
|
142 |
logger.info(f"Webhook set successfully to: {WEBHOOK_URL}")
|
143 |
+
print("webhook set succesfully")
|
144 |
except Exception as e:
|
145 |
+
logger.error(f"Failed to set webhook. Error: {e}")
|
146 |
+
print(f"error in setting bot webhook. Error: {e}")
|
|
|
147 |
|
148 |
|
|
|
149 |
# -------------------------
|
150 |
# Main function to run the bot using Webhook mode
|
151 |
# -------------------------
|
152 |
async def main():
|
153 |
+
await delete_webhook()
|
154 |
await set_webhook()
|
155 |
+
|
156 |
# Build the Application with the Telegram Bot Token
|
157 |
application = Application.builder().token(TOKEN).build()
|
158 |
|
|
|
162 |
|
163 |
# Log: starting the bot in webhook mode.
|
164 |
logger.info("Starting bot in webhook mode...")
|
165 |
+
print("Starting bot in webhook mode...")
|
166 |
|
167 |
|
168 |
# Run the application using webhook mode.
|
169 |
+
# The bot will listen on all interfaces (0.0.0.0) at the specified port.
|
|
|
170 |
await application.run_webhook(
|
171 |
+
listen="0.0.0.0", # Listen on all available interfaces
|
172 |
+
port=443, # Port to listen on
|
173 |
# url_path=TELEGRAM_WEBHOOK, # The webhook path; here, we use the bot token
|
174 |
+
webhook_url=WEBHOOK_URL # The webhook URL that Telegram will use to send updates
|
175 |
)
|
176 |
|
177 |
|
|
|
186 |
# Instead of asyncio.run(), which may try to close an already running loop,
|
187 |
# get the current loop and run main() until complete.
|
188 |
loop = asyncio.get_event_loop()
|
189 |
+
# loop.run_until_complete(main())
|
190 |
+
|
191 |
+
try:
|
192 |
+
loop.run_until_complete(main())
|
193 |
+
except Exception as e:
|
194 |
+
logger.error(f"Error in main loop: {e}")
|
195 |
+
print(f"Error in main loop: {e}")
|
196 |
+
# finally:
|
197 |
+
# await bot.shutdown()
|