import os
from flask import Flask
from threading import Thread
from telegram import Update
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
ContextTypes,
)
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# --- Env vars ---
TELEGRAM_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
RENDER_EXTERNAL_URL = os.environ.get("RENDER_EXTERNAL_URL")
if not TELEGRAM_TOKEN or not RENDER_EXTERNAL_URL:
raise ValueError("Missing TELEGRAM_BOT_TOKEN or RENDER_EXTERNAL_URL")
# --- Optional: Flask for keeping alive or health check ---
app = Flask(__name__)
@app.route("/")
def home():
return "Bot is alive!"
def run_flask():
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
Thread(target=run_flask).start()
# --- Get 3 latest jobs ---
def get_latest_jobs():
options = uc.ChromeOptions()
options.headless = True
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = uc.Chrome(options=options)
jobs = []
try:
driver.get("https://tuitionterminal.com.bd/")
WebDriverWait(driver, 15).until(
EC.element_to_be_clickable((By.XPATH, "//*[contains(@class, 'd-md-block')]"))
).click()
WebDriverWait(driver, 15).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/job-board')]"))
).click()
WebDriverWait(driver, 15).until(
EC.presence_of_all_elements_located((By.XPATH, "//h6[contains(text(), 'Job Id')]"))
)
job_ids = driver.find_elements(By.XPATH, "//h6[contains(text(), 'Job Id')]")[:3]
times = driver.find_elements(By.XPATH, "//h6[i[contains(@class, 'bi-clock-fill')]]")[:3]
for job_id_elem, time_elem in zip(job_ids, times):
job_id_text = job_id_elem.text.strip()
job_id = job_id_text.split(":")[-1].strip()
time_posted = time_elem.text.strip()
job_url = f"https://tuitionterminal.com.bd/job-board/job-details/{job_id}"
jobs.append({
"job_id": job_id_text,
"time": time_posted,
"url": job_url
})
finally:
driver.quit()
return jobs
# --- Telegram command handler ---
async def latest_jobs(update: Update, context: ContextTypes.DEFAULT_TYPE):
jobs = get_latest_jobs()
if not jobs:
await update.message.reply_text("No jobs found.")
return
message = "🧑💼 *Latest 3 Job Postings:*\n\n"
for job in jobs:
message += f"🔹 *{job['job_id']}*\n🕒 _{job['time']}_\n🔗 [View Job]({job['url']})\n\n"
await update.message.reply_markdown(message)
# --- Main async function ---
async def main():
application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
application.add_handler(CommandHandler("latestjobs", latest_jobs))
# 🔧 IMPORTANT: initialize before anything else
await application.initialize()
# Set webhook
await application.bot.set_webhook(f"{RENDER_EXTERNAL_URL}/webhook")
# Start listening via webhook
await application.start()
await application.updater.start_webhook(
listen="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
url_path="webhook"
)
print("🚀 Bot is running with webhook!")
if __name__ == "__main__":
import asyncio
asyncio.run(main())
In the output log it shows : it shows * Running on all addresses (0.0.0.0)
Running on http://127.0.0.1:10000
Press CTRL+C to quit
127.0.0.1 - - [05/Apr/2025 19:39:35] "HEAD / HTTP/1.1" 200 -
Fetching updates got a asyncio.CancelledError. Ignoring as this task may onlybe closed via Application.stop.
==> Your service is live 🎉
127.0.0.1 - - [04/Apr/2025 19:39:42] "GET / HTTP/1.1" 200 -
But when i send /latestjobs to the telegram bot it does not respond anything