如何通过Telegram Bot发送带有文件路径的照片



我正试图通过Telegram机器人发送照片,但遇到了一个错误。我的电脑上有照片的文件路径。也许我没有正确输入文件路径。我得到的错误是:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape`. 

它指的是路径名之前的内容。这是我的代码:

import requests
import json
bot_token = 'XXXXXX'
chat_id = "-100YYYYYY"
file = "C:UsersnameOneDriveDesktopCapture.PNG"
message = ('https://api.telegram.org/bot'+ bot_token + '/sendPhoto?chat_id=' 
+ chat_id + 'photo=' + file)
send = requests.get(message)

以下是如何在python中使用telegram sendPhoto端点上传文件。

import requests
import json
bot_token = 'BOT TOKEN'
chat_id = "CHAT ID"
file = r"C:UsersnameOneDriveDesktopCapture.PNG"
files = {
'photo': open(file, 'rb')
}
message = ('https://api.telegram.org/bot'+ bot_token + '/sendPhoto?chat_id=' 
+ chat_id)
send = requests.post(message, files = files)

作为回答,我使用了上面的代码,但图像质量很差,我无法清楚地理解图像内容。图片是我的笔记本电脑屏幕截图,它有1080p分辨率的

最新更新