【问题标题】:Telegram bot not sending message with "<" , ">" symbols电报机器人不发送带有“<”、“>”符号的消息
【发布时间】:2020-12-15 10:56:33
【问题描述】:
response=requests.post("https://api.telegram.org/<botToken>/sendmessage?chat_id=12345&parse_mode=HTML&text={}".format(" &gt;")) 
print(response.text) 

&gt; 的消息不会在移动设备上发送,response.text 会打印出来:

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

我已经关注了telegram官方apihttps://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;).

【问题讨论】:

    标签: python html telegram


    【解决方案1】:

    使用 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)
    

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 1970-01-01
      • 2017-09-01
      • 2016-10-18
      • 2016-03-11
      • 1970-01-01
      • 2019-12-31
      • 2017-03-28
      • 2019-10-30
      相关资源
      最近更新 更多