@@ -193,6 +193,47 @@ async def search(update, context, title, robot):
193
193
result = re .sub (r"," , ',' , result )
194
194
await context .bot .edit_message_text (chat_id = update .message .chat_id , message_id = messageid , text = escape (result ), parse_mode = 'MarkdownV2' , disable_web_page_preview = True )
195
195
196
+ async def image (update , context ):
197
+ print ("\033 [32m" , update .effective_user .username , update .effective_user .id , update .message .text , "\033 [0m" )
198
+ if (len (context .args ) == 0 ):
199
+ message = (
200
+ f"格式错误哦~,示例:\n \n "
201
+ f"`/pic 一只可爱长毛金渐层在趴在路由器上`\n \n "
202
+ f"👆点击上方命令复制格式\n \n "
203
+ )
204
+ await context .bot .send_message (chat_id = update .effective_chat .id , text = escape (message ), parse_mode = 'MarkdownV2' , disable_web_page_preview = True )
205
+ return
206
+ message = ' ' .join (context .args )
207
+ result = ""
208
+ robot = config .dallbot
209
+ text = message
210
+ message = await context .bot .send_message (
211
+ chat_id = update .message .chat_id ,
212
+ text = "生成中💭" ,
213
+ parse_mode = 'MarkdownV2' ,
214
+ reply_to_message_id = update .message .message_id ,
215
+ )
216
+ messageid = message .message_id
217
+
218
+ try :
219
+ for data in robot .dall_e_3 (text ):
220
+ result = data
221
+ await context .bot .delete_message (chat_id = update .message .chat_id , message_id = messageid )
222
+ await context .bot .send_photo (chat_id = update .message .chat_id , photo = result , reply_to_message_id = update .message .message_id )
223
+ except Exception as e :
224
+ print ('\033 [31m' )
225
+ print ("response_msg" , result )
226
+ print ("error" , e )
227
+ traceback .print_exc ()
228
+ print ('\033 [0m' )
229
+ if "You exceeded your current quota, please check your plan and billing details." in str (e ):
230
+ print ("OpenAI api 已过期!" )
231
+ await context .bot .delete_message (chat_id = update .message .chat_id , message_id = messageid )
232
+ messageid = ''
233
+ config .API = ''
234
+ result += f"`出错啦!{ e } `"
235
+ print (result )
236
+
196
237
import time
197
238
import threading
198
239
async def delete_message (update , context , messageid , delay = 10 ):
@@ -512,6 +553,7 @@ def setup(token):
512
553
513
554
run_async (application .bot .set_my_commands ([
514
555
BotCommand ('info' , 'basic information' ),
556
+ BotCommand ('pic' , 'Generate image' ),
515
557
BotCommand ('search' , 'search Google or duckduckgo' ),
516
558
BotCommand ('en2zh' , 'translate to Chinese' ),
517
559
BotCommand ('zh2en' , 'translate to English' ),
@@ -521,6 +563,7 @@ def setup(token):
521
563
]))
522
564
523
565
application .add_handler (CommandHandler ("start" , start ))
566
+ application .add_handler (CommandHandler ("pic" , image ))
524
567
application .add_handler (CommandHandler ("search" , lambda update , context : search (update , context , title = f"`🤖️ { config .GPT_ENGINE } `\n \n " , robot = config .ChatGPTbot )))
525
568
application .add_handler (CallbackQueryHandler (button_press ))
526
569
application .add_handler (CommandHandler ("reset" , reset_chat ))
0 commit comments