mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-23 23:32:05 +00:00
new set template
This commit is contained in:
@@ -71,9 +71,12 @@ async def url_media(update: Update, context: CustomContext) -> None:
|
|||||||
url = tweet.url
|
url = tweet.url
|
||||||
if context.chat_data.edit_before_forward:
|
if context.chat_data.edit_before_forward:
|
||||||
message_reply = await update.effective_message.reply_text(
|
message_reply = await update.effective_message.reply_text(
|
||||||
"Reply to edit message. [URL]",
|
"Reply to edit message.",
|
||||||
reply_markup=InlineKeyboardMarkup.from_button(
|
reply_markup=InlineKeyboardMarkup.from_column(
|
||||||
InlineKeyboardButton("↩️ Confirm", callback_data="forward")
|
[InlineKeyboardButton("↩️ Confirm", callback_data="forward")] + [
|
||||||
|
InlineKeyboardButton(name, callback_data=f"template|{name}") for name in
|
||||||
|
context.chat_data.template.keys()
|
||||||
|
]
|
||||||
),
|
),
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=update.message.message_id,
|
||||||
)
|
)
|
||||||
@@ -103,23 +106,12 @@ async def forward_message(
|
|||||||
async def edit_message(update: Update, context: CustomContext) -> None:
|
async def edit_message(update: Update, context: CustomContext) -> None:
|
||||||
if (reply_id := update.message.reply_to_message.id) not in context.chat_data.edit_message:
|
if (reply_id := update.message.reply_to_message.id) not in context.chat_data.edit_message:
|
||||||
return
|
return
|
||||||
template = context.chat_data.template
|
|
||||||
message_url = '<a href="{0}">{1}</a>'
|
|
||||||
_edit_message = context.chat_data.edit_message[reply_id]
|
_edit_message = context.chat_data.edit_message[reply_id]
|
||||||
if template:
|
new_text = '<a href="{0}">{1}</a>'.format(
|
||||||
update_text = template.replace("[]", message_url.format(
|
|
||||||
_edit_message.url,
|
_edit_message.url,
|
||||||
html.escape(update.message.text)
|
html.escape(update.message.text)
|
||||||
))
|
)
|
||||||
else:
|
update_text = ori_text.replace("[]", new_text) if "[]" in (ori_text := _edit_message.forward[0].text) else new_text
|
||||||
update_text = html.escape(update.message.text)
|
|
||||||
match = regex.message_url.search(update_text)
|
|
||||||
if match:
|
|
||||||
match = match.span()
|
|
||||||
update_text = update_text[:match[0]] + message_url.format(
|
|
||||||
_edit_message.url,
|
|
||||||
update_text[match[0] + 1:match[1] - 1]
|
|
||||||
) + update_text[match[1]:]
|
|
||||||
await _edit_message.forward[0].edit_caption(update_text)
|
await _edit_message.forward[0].edit_caption(update_text)
|
||||||
|
|
||||||
|
|
||||||
@@ -131,6 +123,15 @@ async def query_forward_message(update: Update, context: CustomContext) -> None:
|
|||||||
del _edit_message
|
del _edit_message
|
||||||
|
|
||||||
|
|
||||||
|
async def query_template(update: Update, context: CustomContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
await query.answer()
|
||||||
|
name = query.data.split("|")[1]
|
||||||
|
await context.chat_data.edit_message[query.message.message_id].forward[0].edit_caption(
|
||||||
|
context.chat_data.template[name]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@send_action(ChatAction.TYPING)
|
@send_action(ChatAction.TYPING)
|
||||||
async def cmd_set_forward_channel(update: Update, context: CustomContext) -> None:
|
async def cmd_set_forward_channel(update: Update, context: CustomContext) -> None:
|
||||||
if not context.args:
|
if not context.args:
|
||||||
@@ -191,10 +192,10 @@ async def cmd_set_template(update: Update, context: CustomContext) -> None:
|
|||||||
if not reply:
|
if not reply:
|
||||||
await update.effective_message.reply_text("Please reply to a message to set as template.")
|
await update.effective_message.reply_text("Please reply to a message to set as template.")
|
||||||
return
|
return
|
||||||
if '[]' not in reply.text_html:
|
if '[]' not in (template := reply.text_html):
|
||||||
await update.effective_message.reply_text("Please reply to a message with [] to set as template.")
|
await update.effective_message.reply_text("Please reply to a message with [] to set as template.")
|
||||||
return
|
return
|
||||||
context.chat_data.template = reply.text_html
|
context.chat_data.template[''.join(context.args)] = template
|
||||||
await update.effective_message.reply_text("Template set.")
|
await update.effective_message.reply_text("Template set.")
|
||||||
|
|
||||||
|
|
||||||
@@ -253,6 +254,7 @@ def main():
|
|||||||
CommandHandler("set_template", cmd_set_template),
|
CommandHandler("set_template", cmd_set_template),
|
||||||
MessageHandler(~filters.COMMAND & filters.ChatType.PRIVATE, edit_message),
|
MessageHandler(~filters.COMMAND & filters.ChatType.PRIVATE, edit_message),
|
||||||
CallbackQueryHandler(query_forward_message, pattern="forward"),
|
CallbackQueryHandler(query_forward_message, pattern="forward"),
|
||||||
|
CallbackQueryHandler(query_template, pattern=r"^template\|"),
|
||||||
CommandHandler("bot_dict", cmd_user_dict, filters=user_filter),
|
CommandHandler("bot_dict", cmd_user_dict, filters=user_filter),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ class ChatData:
|
|||||||
self.forward_channel_id: Optional[int] = None
|
self.forward_channel_id: Optional[int] = None
|
||||||
self.edit_before_forward: bool = False
|
self.edit_before_forward: bool = False
|
||||||
self.edit_message: dict[int, EditMessage] = {}
|
self.edit_message: dict[int, EditMessage] = {}
|
||||||
self.template: Optional[str] = None
|
self.template: dict[str, str] = {}
|
||||||
|
|
||||||
|
|
||||||
class CustomContext(CallbackContext[ExtBot, dict, ChatData, dict]):
|
class CustomContext(CallbackContext[ExtBot, dict, ChatData, dict]):
|
||||||
|
|||||||
Reference in New Issue
Block a user