我想通过我的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中使用telegram 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)