如何在 Telegram 机器人的输出中将特定文本设为粗体?

问题描述 投票:0回答:1

我正在开发一个 Telegram 机器人,在提供网站的个人资料上下文时,我想在响应中将文本的某些部分加粗。这是我的代码片段,用于检索和格式化网站配置文件:

def get_profile_context(site_name):
    """Find the profile context based on the site name, case-insensitive."""
    site_name = site_name.lower()
    site_info = site_data[site_data['site_name'].str.lower() == site_name]

    if not site_info.empty:
        row = site_info.iloc[0]
        context = (
            f"- Enodeb ID: *{row['enodeb_id']}*\n"
            f"- Site ID: {row['site_id']}\n"
            f"- MC: {row['mc']}\n"
            f"- TAC: {row['tac']}\n"
            f"- Region: {row['region']}\n"
            f"- Regency: {row['regency']}\n"
            f"- Antenna Height: {row['antenna_height']}\n"
            f"- Azimuth: {row['azimuth']}\n"
        )
        print(context)
        global context_global
        context_global = context
        return context
    else:
        return "Site name not found."

async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query
    await query.answer()

    if query.data == 'profile_site':
        profile_context = get_profile_context(status["site_name"])
        await query.edit_message_text(f"Profile of site '{status['site_name']}':\n{profile_context}")
        keyboard = [
            [InlineKeyboardButton("Back to Menu", callback_data='back_to_menu'),
            InlineKeyboardButton("Back to Start", callback_data='start')]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        await query.message.reply_text("What would you like to do next?", reply_markup=reply_markup)

在输出中,我希望站点名称(例如“站点‘site_name’的配置文件”)为粗体。但是,当我在文本周围使用 * 符号时,它似乎不起作用。如何正确设置此文本的格式,使其在机器人发送的消息中加粗?

任何帮助将不胜感激!

我希望答案是粗体的: 网站简介'--':

  • Enodeb ID:0000
  • 站点 ID:000000
  • MC:MC
  • TAC:00
  • 地区:中北部
  • 摄政:城市
  • 天线高度:61
  • 方位角:3_0
python artificial-intelligence telegram-bot chatbot python-telegram-bot
1个回答
0
投票

这是简单的解决方案!

make_bold = "STRING: \033[1mBOLD\033[0m"
print(make_bold)

像这样更改您的代码:

context = (
    f"- Enodeb ID: \033[1m{row['enodeb_id']}\033[0m\n"
    f"- Site ID: {row['site_id']}\n"
    f"- MC: {row['mc']}\n"
    f"- TAC: {row['tac']}\n"
    f"- Region: {row['region']}\n"
    f"- Regency: {row['regency']}\n"
    f"- Antenna Height: {row['antenna_height']}\n"
    f"- Azimuth: {row['azimuth']}\n"
)
print(context)

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.