如何在 telepot Telegram 机器人中加粗文本?
How can I bold text in telepot Telegram bot?
我试过了
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
抽象自 -->
你应该使用:
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)
我在使用 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)
我试过了
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
抽象自 -->
你应该使用:
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)
我在使用 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)