Create telegram_bot.py
Browse files- telegram_bot.py +40 -0
telegram_bot.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
from telegram import Update, Bot
|
| 4 |
+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
| 5 |
+
|
| 6 |
+
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 7 |
+
MOLBOT_URL = os.getenv("MOLBOT_URL") # উদাহরণ: https://suj2026-molbot.hf.space/
|
| 8 |
+
|
| 9 |
+
bot = Bot(token=TELEGRAM_TOKEN)
|
| 10 |
+
|
| 11 |
+
def start(update: Update, context: CallbackContext):
|
| 12 |
+
update.message.reply_text("Hi! I'm your Molbot AI 🤖")
|
| 13 |
+
|
| 14 |
+
def handle_message(update: Update, context: CallbackContext):
|
| 15 |
+
user_msg = update.message.text
|
| 16 |
+
try:
|
| 17 |
+
response = requests.post(
|
| 18 |
+
MOLBOT_URL,
|
| 19 |
+
headers={"Content-Type": "application/json"},
|
| 20 |
+
json={"message": user_msg}
|
| 21 |
+
).json()
|
| 22 |
+
|
| 23 |
+
reply_text = response.get("choices", [{}])[0].get("message", {}).get("content", "Sorry, couldn't reply.")
|
| 24 |
+
update.message.reply_text(reply_text)
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
update.message.reply_text(f"Error: {e}")
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
updater = Updater(TELEGRAM_TOKEN)
|
| 31 |
+
dp = updater.dispatcher
|
| 32 |
+
|
| 33 |
+
dp.add_handler(CommandHandler("start", start))
|
| 34 |
+
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))
|
| 35 |
+
|
| 36 |
+
updater.start_polling()
|
| 37 |
+
updater.idle()
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|