import os import re import markdown from dotenv import load_dotenv from flowise import Flowise, PredictionData from telegram import Update from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters global flowiseClient global chatflowId async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Send a message when the command /start is issued.""" user = update.effective_user await update.message.reply_markdown_v2( fr"Hi {user.mention_markdown_v2()}\!", ) async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Handle a message.""" # Extract the session id from the message session_id = update.message.from_user.id # Create a prediction response = flowiseClient.create_prediction(PredictionData(question=update.message.text, chatflowId=chatflowId, streaming=False, chatId=session_id)) # Extract text from the response response_text = "" for item in response: response_text += item["text"] # Simple markdown to HTML conversion html = markdown.markdown(response_text) # Basic cleanup for Telegram HTML compatibility html = html.replace('', '').replace('', '') html = html.replace('', '').replace('', '') html = re.sub(r'

(.*?)

', r'\1\n\n', html, flags=re.DOTALL) # Handle lists - convert to simple bullet points html = re.sub(r'', '', html) html = re.sub(r'
  • (.*?)
  • ', r'• \1\n', html, flags=re.DOTALL) # Clean up any other unsupported tags that might be present html = re.sub(r'
      \s*', '', html) html = re.sub(r'\s*
    ', '', html) html = re.sub(r'(.*?)', r'\1\n', html, flags=re.DOTALL) # Clean up extra whitespace html = re.sub(r'\n\s*\n', '\n\n', html) html = html.strip() # Send the formatted message using HTML parse mode await update.message.reply_text(html.strip(), parse_mode='HTML') if __name__ == "__main__": # Load environment variables load_dotenv() # Setup the Flowise client FLOWISE_API_KEY = os.getenv("FLOWISE_API_KEY") FLOWISE_API_URL = os.getenv("FLOWISE_API_URL") chatflowId = os.getenv("FLOWISE_CHATFLOW_ID") if not FLOWISE_API_KEY: raise ValueError("FLOWISE_API_KEY not found in environment variables") if not FLOWISE_API_URL: raise ValueError("FLOWISE_API_URL not found in environment variables") if not chatflowId: raise ValueError("FLOWISE_CHATFLOW_ID not found in environment variables") flowiseClient = Flowise(base_url=FLOWISE_API_URL, api_key=FLOWISE_API_KEY) # Setup the Telegram bot TELEGRAM_API_KEY = os.getenv("TELEGRAM_API_KEY") if not TELEGRAM_API_KEY: raise ValueError("TELEGRAM_API_KEY not found in environment variables") application = Application.builder().token(TELEGRAM_API_KEY).build() application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.TEXT, handle_message)) # Run the bot application.run_polling()