简体   繁体   English

电报机器人不发送带有“<”、“>”符号的消息

[英]Telegram bot not sending message with “<” , “>” symbols

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

The message > doesn't get sent on mobile and response.text prints :消息>不会在移动设备上发送并且response.text打印:

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

I have followed the telegram official api https://core.telegram.org/bots/api#html-style -我已经关注了电报官方 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;).

Convert your text using the python urllib.parse.quote_plus(string) so the special characters won't interfere with the url;使用 python urllib.parse.quote_plus(string)转换文本,这样特殊字符就不会干扰 url;

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM