molbot / telegram_bot.py
Sobuj2026's picture
Create telegram_bot.py
9681664 verified
import requests
import os
from telegram import Update, Bot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
MOLBOT_URL = os.getenv("MOLBOT_URL") # উদাহরণ: https://suj2026-molbot.hf.space/
bot = Bot(token=TELEGRAM_TOKEN)
def start(update: Update, context: CallbackContext):
update.message.reply_text("Hi! I'm your Molbot AI 🤖")
def handle_message(update: Update, context: CallbackContext):
user_msg = update.message.text
try:
response = requests.post(
MOLBOT_URL,
headers={"Content-Type": "application/json"},
json={"message": user_msg}
).json()
reply_text = response.get("choices", [{}])[0].get("message", {}).get("content", "Sorry, couldn't reply.")
update.message.reply_text(reply_text)
except Exception as e:
update.message.reply_text(f"Error: {e}")
def main():
updater = Updater(TELEGRAM_TOKEN)
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__":
main()