import requests def send_file_to_telegram(bot_token, chat_id, file_path, caption=None): url = f"https://api.telegram.org/bot{bot_token}/sendDocument" with open(file_path, "rb") as f: files = {"document": f} data = {"chat_id": chat_id} if caption: data["caption"] = caption response = requests.post(url, data=data, files=files) return response.json() if __name__ == '__main__': TXT_SAVE_PATH = "output.txt" result_txt = 'test' with open(TXT_SAVE_PATH, "w", encoding="utf-8") as f: f.write(result_txt) tg_result = send_file_to_telegram("5149834858:AAEnQcjS4fH9iUPLGUI3LvFqHo0oWDA6XCU", "1759334982", TXT_SAVE_PATH, caption="自动上传的txt文件")