|
| 1 | +from dotenv import load_dotenv |
| 2 | +import os |
| 3 | +from telegram import Update |
| 4 | +import replicate |
| 5 | +from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler, filters |
| 6 | + |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | + |
| 10 | +def getUrl(prompt): |
| 11 | + a = replicate.run( |
| 12 | + "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", |
| 13 | + input={"prompt": prompt} |
| 14 | + ) |
| 15 | + print(a) |
| 16 | + return a[0] |
| 17 | + |
| 18 | + |
| 19 | +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
| 20 | + await update.message.reply_text( |
| 21 | + f"Hello {update.effective_user.first_name} I am your personal AI Image Generator \n Use /help to know all commands.") |
| 22 | + |
| 23 | + |
| 24 | +async def help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
| 25 | + await update.message.reply_text( |
| 26 | + '''Here are a list of all commands:- |
| 27 | + /start - Start a conversation |
| 28 | + /help - Get a list of commands |
| 29 | + /imagine <prompt> - Get an AI generated image''' |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 34 | + await update.message.reply_text(""" |
| 35 | + Sorry, I did not understand that command. |
| 36 | + Type \" /help \" to see all possible commands""") |
| 37 | + |
| 38 | + |
| 39 | +async def imagine(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 40 | + arg = str(" ".join(context.args)) |
| 41 | + url = getUrl(arg) |
| 42 | + await update.message.reply_photo(url) |
| 43 | + |
| 44 | + |
| 45 | +app = ApplicationBuilder().token(os.environ.get('TELEGRAM_BOT_TOKEN')).build() |
| 46 | +app.add_handler(CommandHandler("start", start)) |
| 47 | +app.add_handler(CommandHandler("help", help)) |
| 48 | +app.add_handler(CommandHandler("imagine", imagine)) |
| 49 | +app.add_handler(MessageHandler(filters.COMMAND, handle_message)) |
| 50 | +app.run_polling() |
0 commit comments