我在写代码的时候遇到了一个问题。机器人的本质是将争吵的文本发送到电报频道。问题是被问到的文本看起来像这样:
async def send_article(chat_id, id_topic, title, preview_text, photo_url):
response_text = f"<b>{title}</b>\n\n<i>{preview_text}</i>\n\n<a href='http://example.com/-t{id_topic}.0.html'>Read ALL</a>"
它像这样出现在机器人本身中:
第一行 - 粗体
第二行 - 斜体
第三行-html超链接
处理程序发送到通道的代码:
@dp.callback_query_handler(lambda call: call.data.startswith('pub_'))
async def publish_article(call: types.CallbackQuery):
id_topic = call.data[4:]
message = call.message
chat_id = CHANNEL_ID
if message.photo:
response_text = message.caption
photo_url = message.photo[-1].file_id
else:
response_text = message.text
photo_url = None
if photo_url:
await bot.send_photo(chat_id=chat_id, photo=photo_url, caption=response_text, parse_mode=ParseMode.HTML)
else:
await bot.send_message(chat_id=chat_id, text=response_text, parse_mode=ParseMode.HTML)
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("✅Опубликовано", callback_data="published"))
await bot.edit_message_reply_markup(chat_id=message.chat.id, message_id=message.message_id, reply_markup=markup)
await call.answer()
如何保存已发送消息中的文本格式?