电报漫游器未发送带有“ <”,“>”符号的消息

时间:2020-08-26 15:40:11

标签: python html telegram

response=requests.post("https://api.telegram.org/<botToken>/sendmessage?chat_id=12345&parse_mode=HTML&text={}".format(" &gt;")) 
print(response.text) 

>消息未通过移动设备发送,并且response.text打印:

{"ok":false,"error_code":400,"description":"Bad Request: message must be non-empty"} 

我已遵循电报官方api https://core.telegram.org/bots/api#html-style-

All <, > and & symbols that are not a part of a tag or an HTML entity must be replaced with the corresponding HTML entities (< with &lt;, > with &gt; and & with &amp;).

1 个答案:

答案 0 :(得分:1)

使用python urllib.parse.quote_plus(string)转换文本,以便特殊字符不会干扰网址;

import requests
from urllib.parse import quote_plus

response=requests.post("https://api.telegram.org/bot<TOKEN>/sendmessage?chat_id=12345&parse_mode=HTML&text={}".format(quote_plus(" &gt;")))
print(response.text)