8

我试过这个

elif command == 'bold':
    telegram_bot.sendMessage (chat_id, str("*bold*"), reply_markup=markup)

但它正在回复*bold*而不是粗体

4

3 回答 3

11

您需要提供一个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

于 2018-09-04T10:38:00.953 回答
4

我对 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)
于 2019-09-04T01:45:57.390 回答
2

摘自 -->如何在 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)

更多信息:https ://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#message-formatting-bold-italic-code-

于 2018-09-04T10:52:31.893 回答