我试过这个
elif command == 'bold':
telegram_bot.sendMessage (chat_id, str("*bold*"), reply_markup=markup)
但它正在回复*bold*
而不是粗体
我试过这个
elif command == 'bold':
telegram_bot.sendMessage (chat_id, str("*bold*"), reply_markup=markup)
但它正在回复*bold*
而不是粗体
您需要提供一个parse_mode
参数 (parse_mode="Markdown")。
否则您将看不到任何降价样式。
sendMessage(chat_id, "*this text is bold*", parse_mode= 'Markdown')
看
https://telepot.readthedocs.io/en/latest/reference.html#telepot.Bot.sendMessage
我对 Markdown parse_mode 也有同样的问题。我自己编写了 send_message,没有使用 Telepot 的 sendMessage 方法。在这种情况下,更容易理解如何处理这个问题:
url = 'https://api.telegram.org/bot<token>'
def send_message(chat_id, text='empty line', parse_mode = 'Markdown'):
URL = url + 'sendMessage'
answer = {'chat_id': chat_id, 'text': text, 'parse_mode': 'Markdown'}
r = requests.post(URL, json=answer)
return r.json()
if (text == '/bold'):
send_message(chat_id, 'Here comes the'+'*'+'bold'+'*'+'text!')
另一方面,您可以使用 curl 发送粗体文本:
if (text == '/bold'):
URL = url + 'sendMessage?chat_id='+chat_id+'&text=*Here comes the bold text*&parse_mode=Markdown'
answer = {'chat_id': chat_id, 'text': text}
r = requests.post(URL, json=answer)
摘自 -->如何在 python 电报机器人中写粗体
你应该使用:
bot.send_message(chat_id=chat_id, text="*bold* Example message",
parse_mode=telegram.ParseMode.MARKDOWN)
或者:
bot.send_message(chat_id=chat_id, text='<b>Example message</b>',
parse_mode=telegram.ParseMode.HTML)