如何设置输入按摩,通过电报频道的请求发送



我通过请求库将帖子发送到 teleram 频道,我需要添加 Enter 以获得更好的频道格式,我在行尾使用n但它不起作用,对此有什么想法吗

这是我的代码

import requests
def Telegram_channel (x):
url = "https://api.telegram.org/bot<token>/sendMessage"
data = {"chat_id":"-USER_id", "text":x}
r = requests.post(url, json=data)

x = ">>>> length of Tv packs banned in Database : n"
x = x,">>>> Torrent Link DB value ",torrent_link,'n'
Telegram_channel (x)

结果是:

>>>> length of Tv packs banned in Database  n>>>> Torrent Link DB value n

但它应该是这样的

>>>> length of Tv packs banned in Database 
>>>> Torrent Link DB value

您实际上是在创建一个tuple而不是str(textJSON参数应该是):

x = ">>>> length of Tv packs banned in Database : n"
x = x,">>>> Torrent Link DB value ","torrent_link_text_here",'n'
print(type(x))
print(x)

输出:

<class 'tuple'>
('>>>> length of Tv packs banned in Database : n', '>>>> Torrent Link DB value ', 'torrent_link_text_here', 'n')

请求库无法正确处理它以构造 HTTP 请求,因此您会丢失换行符。


为什么不使用字符串格式?

import requests
url = "https://api.telegram.org/bot<TOKEN>/sendMessage"
torrent_link = "https://example.com"
x = ">>>> length of Tv packs banned in Database: n>>>> Torrent Link DB value {}n".format(torrent_link)
data = {"chat_id": <YOUR_CHAT_ID>, "text": x}
r = requests.post(url, json=data)

聊天输出:

>>>> length of Tv packs banned in Database:  
>>>> Torrent Link DB value https://example.com

试试下面:

基本上,您需要在此 API中发送的参数,在查询参数中,实际上您是在正文中发送它们,因此请以查询字符串发送并享受编码。

网址 :https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHANNEL_NAME]&text=[MY_MESSAGE_TEXT]

方法:获取位置:

  • BOT_API_KEY是 BotFather 在您创建时生成的 API 密钥 您的机器人
  • MY_CHANNEL_NAME是频道的句柄(例如 @my_channel_name)
  • 您要发送的消息MY_MESSAGE_TEXT(网址编码)

最新更新