11""" Create Buttons Through Bots """
22
33# IMPROVED BY code-rgb
4- # By @krishna_singhal
54
5+ import json
66import os
77import re
88
9- from html_telegraph_poster .upload_images import upload_image
109from pyrogram .errors import BadRequest , MessageEmpty , UserIsBot
1110from pyrogram .types import ReplyKeyboardRemove
1211
13- from userge import Config , Message , get_collection , userge
12+ from userge import Config , Message , userge
1413from userge .utils import get_file_id_and_ref
1514from userge .utils import parse_buttons as pb
1615
17- BUTTON_BASE = get_collection ("TEMP_BUTTON" )
1816BTN = r"\[([^\[]+?)\](\[buttonurl:(?:/{0,2})(.+?)(:same)?\])|\[([^\[]+?)\](\(buttonurl:(?:/{0,2})(.+?)(:same)?\))"
1917BTNX = re .compile (BTN )
18+ PATH = "./userge/xcache/inline_db.json"
19+ CHANNEL = userge .getCLogger (__name__ )
20+
21+
22+ class Inline_DB :
23+ def __init__ (self ):
24+ if not os .path .exists (PATH ):
25+ d = {}
26+ json .dump (d , open (PATH , "w" ))
27+ self .db = json .load (open (PATH ))
28+
29+ def save_msg (self , rnd_id : int , msg_content : str , media_valid : bool , media_id : int ):
30+ self .db [rnd_id ] = {
31+ "msg_content" : msg_content ,
32+ "media_valid" : media_valid ,
33+ "media_id" : media_id ,
34+ }
35+ self .save ()
36+
37+ def save (self ):
38+ with open (PATH , "w" ) as outfile :
39+ json .dump (self .db , outfile , indent = 4 )
40+
41+
42+ InlineDB = Inline_DB ()
2043
2144
2245@userge .on_cmd (
@@ -74,34 +97,41 @@ async def create_button(msg: Message):
7497 },
7598)
7699async def inline_buttons (message : Message ):
77- """ Create Buttons Through Inline Bots """
78- if Config .BOT_TOKEN is None :
79- await message .err (
80- "First Create a Inline Bot via @Botfather to Create Buttons..."
81- )
82- return
83- replied = message .reply_to_message
84- if not (replied and (replied .text or replied .caption )):
85- await message .err ("Reply a text Msg" )
86- return
87- await message .edit ("<code>Creating an inline button...</code>" )
88- if replied .caption :
89- text = replied .caption
90- text = check_brackets (text )
91- dls_loc = await down_image (message )
92- photo_url = str (upload_image (dls_loc ))
93- BUTTON_BASE .insert_one ({"msg_data" : text , "photo_url" : photo_url })
94- os .remove (dls_loc )
95- else :
96- text = replied .text
97- text = check_brackets (text )
98- BUTTON_BASE .insert_one ({"msg_data" : text })
100+ await message .edit ("<code>Creating an Inline Button...</code>" )
101+ reply = message .reply_to_message
102+ msg_content = None
103+ media_valid = False
104+ media_id = 0
105+ if reply :
106+ media_valid = bool (get_file_id_and_ref (reply )[0 ])
107+
108+ if message .input_str :
109+ msg_content = message .input_str
110+ if media_valid :
111+ media_id = (await reply .forward (Config .LOG_CHANNEL_ID )).message_id
112+
113+ elif reply :
114+ if media_valid :
115+ media_id = (await reply .forward (Config .LOG_CHANNEL_ID )).message_id
116+ msg_content = reply .caption .html if reply .caption else None
117+ elif reply .text :
118+ msg_content = reply .text .html
119+
120+ if not msg_content :
121+ return await message .err ("Content not found" , del_in = 5 )
122+
123+ rnd_id = userge .rnd_id ()
124+ msg_content = check_brackets (msg_content )
125+ InlineDB .save_msg (rnd_id , msg_content , media_valid , media_id )
126+
99127 bot = await userge .bot .get_me ()
100- x = await userge .get_inline_bot_results (bot .username , "buttonnn" )
128+
129+ x = await userge .get_inline_bot_results (bot .username , f"btn_{ rnd_id } " )
101130 await userge .send_inline_bot_result (
102- chat_id = message .chat .id , query_id = x .query_id , result_id = x .results [0 ].id
131+ chat_id = message .chat .id ,
132+ query_id = x .query_id ,
133+ result_id = x .results [0 ].id ,
103134 )
104- await BUTTON_BASE .drop ()
105135 await message .delete ()
106136
107137
@@ -120,16 +150,6 @@ def check_brackets(text):
120150 return text
121151
122152
123- async def down_image (message ):
124- message .reply_to_message
125- if not os .path .isdir (Config .DOWN_PATH ):
126- os .makedirs (Config .DOWN_PATH )
127- dls = await userge .download_media (
128- message = message .reply_to_message , file_name = Config .DOWN_PATH
129- )
130- return os .path .join (Config .DOWN_PATH , os .path .basename (dls ))
131-
132-
133153@userge .on_cmd (
134154 "noformat" ,
135155 about = {
0 commit comments