我正在开发一个 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’的配置文件”)为粗体。但是,当我在文本周围使用 * 符号时,它似乎不起作用。如何正确设置此文本的格式,使其在机器人发送的消息中加粗?
任何帮助将不胜感激!
我希望答案是粗体的: 网站简介'--':
这是简单的解决方案!
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)
谢谢!