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

How to send photo via Telegram Bot with file path?

我正在尝试通过我的 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:\Users\name\OneDrive\Desktop\Capture.PNG"

message = ('https://api.telegram.org/bot'+ bot_token + '/sendPhoto?chat_id=' 
           + chat_id + 'photo=' + file)
send = requests.get(message)

以下是您应该如何使用 python 中的电报 sendPhoto 端点上传文件。

import requests
import json

bot_token = 'BOT TOKEN'
chat_id = "CHAT ID"
file = r"C:\Users\name\OneDrive\Desktop\Capture.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