簡體   English   中英

如何通過帶有文件路徑的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM