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