pundit
pundit

Reputation: 187

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 :

{"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 -

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;).

Upvotes: 1

Views: 2655

Answers (1)

0stone0
0stone0

Reputation: 43983

Convert your text using the python urllib.parse.quote_plus(string) so the special characters won't interfere with the 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)

Upvotes: 3

Related Questions