Spaces:
Paused
Paused
Create telegram.py
Browse files- telegram.py +110 -0
telegram.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
import threading
|
| 4 |
+
import telebot
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from llm import generate_llm # Assuming these are your custom functions
|
| 7 |
+
from sd import generate_sd # Replace with actual imports as needed
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
def load_env():
|
| 12 |
+
load_dotenv()
|
| 13 |
+
global TELEGRAM_BOT_TOKEN
|
| 14 |
+
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 15 |
+
if not TELEGRAM_BOT_TOKEN:
|
| 16 |
+
raise ValueError("TELEGRAM_BOT_TOKEN environment variable is not set")
|
| 17 |
+
|
| 18 |
+
load_env()
|
| 19 |
+
|
| 20 |
+
# Initialize the bot
|
| 21 |
+
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
|
| 22 |
+
|
| 23 |
+
# Configure logging
|
| 24 |
+
LOG_FILE = "bot.log"
|
| 25 |
+
logging.basicConfig(
|
| 26 |
+
level=logging.INFO,
|
| 27 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 28 |
+
handlers=[
|
| 29 |
+
logging.FileHandler(LOG_FILE),
|
| 30 |
+
logging.StreamHandler()
|
| 31 |
+
]
|
| 32 |
+
)
|
| 33 |
+
logger = logging.getLogger(__name__)
|
| 34 |
+
|
| 35 |
+
# Configure SOCKS5 Proxy (replace with your proxy details)
|
| 36 |
+
PROXY_HOST = '23.19.244.109'
|
| 37 |
+
PROXY_PORT = 1080
|
| 38 |
+
|
| 39 |
+
telebot.apihelper.proxy = {'https': f'socks5://{PROXY_HOST}:{PROXY_PORT}'}
|
| 40 |
+
|
| 41 |
+
def response_text(chat_id, prompt):
|
| 42 |
+
"""
|
| 43 |
+
Generate a response using the LLM and send it to the user.
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
msg = generate_llm(prompt) # Example function call to generate a response
|
| 47 |
+
bot.send_message(chat_id, msg)
|
| 48 |
+
logger.info(f"Chat ID: {chat_id} - Sent Message: {msg}")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Chat ID: {chat_id} - Error in response_text: {e}")
|
| 51 |
+
bot.send_message(chat_id, "There was an error processing your request.")
|
| 52 |
+
|
| 53 |
+
@bot.message_handler(commands=['imagine'])
|
| 54 |
+
def handle_imagine_command(message):
|
| 55 |
+
"""
|
| 56 |
+
Handle the /imagine command by generating an image from the provided prompt.
|
| 57 |
+
"""
|
| 58 |
+
prompt = message.text.replace('/imagine', '').strip()
|
| 59 |
+
if not prompt:
|
| 60 |
+
bot.reply_to(message, "Please provide a prompt after /imagine.")
|
| 61 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Please provide a prompt after /imagine.")
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
generating_message = bot.send_message(message.chat.id, "Generating...")
|
| 65 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Generating...")
|
| 66 |
+
|
| 67 |
+
def handle_image_generation():
|
| 68 |
+
try:
|
| 69 |
+
# Example function call to generate image data
|
| 70 |
+
image_data, image_path = generate_sd(prompt)
|
| 71 |
+
bot.delete_message(chat_id=message.chat.id, message_id=generating_message.message_id)
|
| 72 |
+
|
| 73 |
+
if image_data:
|
| 74 |
+
bot.send_photo(message.chat.id, image_data)
|
| 75 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Image {image_path}")
|
| 76 |
+
else:
|
| 77 |
+
bot.reply_to(message, "Failed to generate image. Please try again later.")
|
| 78 |
+
logger.error(f"Chat ID: {message.chat.id} - Failed to generate image.")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Chat ID: {message.chat.id} - Error in handle_image_generation: {e}")
|
| 81 |
+
bot.reply_to(message, "There was an error generating the image. Please try again later.")
|
| 82 |
+
|
| 83 |
+
threading.Thread(target=handle_image_generation).start()
|
| 84 |
+
|
| 85 |
+
@bot.message_handler(func=lambda message: True)
|
| 86 |
+
def handle_message(message):
|
| 87 |
+
"""
|
| 88 |
+
Handle all text messages by generating a response and sending it.
|
| 89 |
+
"""
|
| 90 |
+
chat_id = message.chat.id
|
| 91 |
+
user_message = message.text
|
| 92 |
+
logger.info(f"Chat ID: {chat_id} - Received Message: {user_message}")
|
| 93 |
+
|
| 94 |
+
threading.Thread(target=response_text, args=(chat_id, user_message)).start()
|
| 95 |
+
|
| 96 |
+
def start_telegram_bot():
|
| 97 |
+
"""
|
| 98 |
+
Start the Telegram bot.
|
| 99 |
+
"""
|
| 100 |
+
while True:
|
| 101 |
+
try:
|
| 102 |
+
logger.info("Starting bot")
|
| 103 |
+
bot.polling(none_stop=True)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
logger.critical(f"Critical error: {e}")
|
| 106 |
+
time.sleep(5) # Wait for a few seconds before restarting
|
| 107 |
+
|
| 108 |
+
# Ensure the bot starts when this script is executed directly
|
| 109 |
+
if __name__ == '__main__':
|
| 110 |
+
start_telegram_bot()
|