透過Telegram API發送圖片 (本機上傳)

import requests
import os

telegram_id = XXXXX
bot_token = ""

def sendPhoto(image_path):
    data = {"chat_id": telegram_id}
    url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
    try:
        with open(image_path, "rb") as image_file:
            ret = requests.post(url, data=data, files={"photo": image_file})
            result = ret.json()
            if 'ok' in result:
                if result['ok']:
                    file_id = result['result']['photo'][0]['file_id']
    except Exception as e:
        raise e

if __name__ == '__main__':
    image_path = 'a.jpg'
    sendPhoto(image_path)