繁体   English   中英

获得<response [200]>在 Python 上向 Telegram 发送包含加号“+”的字符串时</response>

[英]Getting <Response [200]> on Python when sending to Telegram a string containing the plus sign "+"

从 Python 向 Telegram 频道发送消息非常简单,只需创建一个 Telegram 机器人,保存其令牌 ID 并将机器人作为管理员添加到频道,然后 Python 代码为

import requests
TOKEN = "..." # bot token ID to access the HTTP API
CHANNEL_ID = "..." # for example @durov
MSG = "..."
url = "https://api.telegram.org/bot" + TOKEN + "/sendMessage?chat_id=" + CHANNEL_ID + "&text=" + MSG
requests.post(url)

但是,当使用例如MSG = "test+8"时,发送到 Telegram 频道的消息是test 8 (使用空格而不是加号)并且在 Python 上我得到以下 output

<Response [200]>

我尝试用减号替换加号并且有效,但是加号有什么问题? 它是一个特殊的字符吗?

这与 URL 编码有关 - 您可以通过自己对MSG进行 urlencode 来规避“missrepersented”+:

import urllib.parse
MSG = 'message + 42'
enc = urllib.parse.quote(MSG)
print(enc)

Output:

message%20%2B%204

并发送此编码消息。

有关详细信息,请参阅URL 中允许的 fe 字符

根据RFC 3986字符+属于保留字符

 sub-delims = "," / "$" / "&" / "'" / "(" / ")" / "*" / "+" / ";" / ";" / "="

并且在 uri 的查询部分中使用时需要被引用。

暂无
暂无

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

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