Title: input_file = InputFile(f, read_file_handle=False) · Issue #4821 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: input_file = InputFile(f, read_file_handle=False) · Issue #4821 · python-telegram-bot/python-telegram-bot
X Title: input_file = InputFile(f, read_file_handle=False) · Issue #4821 · python-telegram-bot/python-telegram-bot
Description: Issue I am facing with open(file_path, 'rb') as f: input_file = InputFile(f, read_file_handle=False) msg = await update.message.reply_document(input_file, caption='本地文件直传', write_timeout=300, connect_timeout=30) #msg = await update.messa...
Open Graph Description: Issue I am facing with open(file_path, 'rb') as f: input_file = InputFile(f, read_file_handle=False) msg = await update.message.reply_document(input_file, caption='本地文件直传', write_timeout=300, conne...
X Description: Issue I am facing with open(file_path, 'rb') as f: input_file = InputFile(f, read_file_handle=False) msg = await update.message.reply_document(input_file, caption='本地文件直传', write_ti...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/4821
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"input_file = InputFile(f, read_file_handle=False)","articleBody":"### Issue I am facing\n\n\n```\n with open(file_path, 'rb') as f:\n input_file = InputFile(f, read_file_handle=False)\n msg = await update.message.reply_document(input_file, caption='本地文件直传', write_timeout=300, connect_timeout=30)\n #msg = await update.message.reply_document(f, caption='本地文件直传')\n new_file_id = msg.document.file_id\n```\n```\n request = HTTPXRequest(\n connect_timeout=60.0, # Connection timeout\n read_timeout=1810.0, # Should be \u003e TDLIB_UPLOAD_FILE_TIMEOUT\n write_timeout=1810.0, # Should be \u003e TDLIB_UPLOAD_FILE_TIMEOUT\n pool_timeout=60.0, # Pool timeout\n media_write_timeout=1810.0\n )\n builder = ApplicationBuilder().token(TOKEN).request(request)\n```\nCan someone help me? If I add .request(request), it will report this error\n\nBut file can be sent successfully.\n\n.request(request) If there is no such thing, it will prompt a timeout, but the file can still be sent successfully\n\nI have used my own API, (https://github.com/aiogram/telegram-bot-api) and there is no problem sending small files, but there is a problem with large files\n\n### Traceback to the issue\n\n```python\nCan not load invalid JSON data: \"\"\nTraceback (most recent call last):\n File \"C:\\Users\\123\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\telegram\\request\\_baserequest.py\", \nline 380, in parse_json_payload\n return json.loads(decoded_s)\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\json\\__init__.py\", line 346, in loads\n return _default_decoder.decode(s)\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\json\\decoder.py\", line 337, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\json\\decoder.py\", line 355, in raw_decode\n raise JSONDecodeError(\"Expecting value\", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\nTraceback (most recent call last):\n File \"c:\\Users\\123\\Desktop\\1\\mytgbot\\docker_txttg\\new_tg_bot.py\", line 536, in \u003cmodule\u003e\n main()\n File \"c:\\Users\\123\\Desktop\\1\\mytgbot\\docker_txttg\\new_tg_bot.py\", line 533, in main\n app.run_polling()\n File \"C:\\Users\\123\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\telegram\\ext\\_application.py\", line 832, in run_polling\n return self.__run(\n File \"C:\\Users\\123\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\telegram\\ext\\_application.py\", line 1061, in __run\n loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\base_events.py\", line 634, in run_until_complete\n self.run_forever()\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\windows_events.py\", line 321, in run_forever\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\base_events.py\", line 601, in run_forever\n self._run_once()\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\base_events.py\", line 1869, in _run_once\n event_list = self._selector.select(timeout)\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\windows_events.py\", line 439, in select\n self._poll(timeout)\n File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\lib\\asyncio\\windows_events.py\", line 788, in _poll\n status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)\nKeyboardInterrupt\n```\n\n### Related part of your code\n\n```python\nasync def on_start(update: Update, context: ContextTypes.DEFAULT_TYPE):\n args = context.args\n if not args and update.message:\n # 兼容 /start 无参数\n await update.message.reply_text('欢迎使用本bot!')\n return\n # 支持 deep link\n if update.message:\n start_param = update.message.text.split(' ', 1)[1] if ' ' in update.message.text else ''\n elif update.callback_query:\n start_param = update.callback_query.data.split(' ', 1)[1] if ' ' in update.callback_query.data else ''\n else:\n start_param = ''\n \n if start_param.startswith('book_'):\n # 检查用户权限\n user_id = update.effective_user.id\n \n # 只解析 file_id\n try:\n parts = start_param.split('_')\n file_id = int(parts[1])\n except Exception:\n await update.message.reply_text('参数错误。')\n return\n with SessionLocal() as session:\n file = session.query(File).filter_by(file_id=file_id).first()\n if not file:\n await update.message.reply_text('文件不存在。')\n return\n tg_file_id, file_path = file.tg_file_id, file.file_path\n try:\n if tg_file_id and (tg_file_id.startswith('BQAC') or tg_file_id.startswith('CAAC') or tg_file_id.startswith('HDAA')):\n await update.message.reply_document(tg_file_id, caption=f'文件tg_file_id: {tg_file_id}')\n elif tg_file_id and tg_file_id.startswith('BAAC'):\n await update.message.reply_video(tg_file_id, caption=f'文件tg_file_id: {tg_file_id}')\n elif tg_file_id and tg_file_id.startswith('AgAC'):\n await update.message.reply_photo(tg_file_id, caption=f'文件tg_file_id: {tg_file_id}')\n elif tg_file_id is None or tg_file_id == '':\n ext = os.path.splitext(file_path)[1].lower()\n if ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']:\n with open(file_path, 'rb') as f:\n msg = await update.message.reply_photo(f, caption='本地图片直传')\n new_file_id = msg.photo[-1].file_id if msg.photo else None\n elif ext in ['.mp4', '.mov', '.avi', '.mkv', '.webm']:\n with open(file_path, 'rb') as f:\n \n msg = await update.message.reply_video(f, caption='本地视频直传')\n new_file_id = msg.video.file_id\n elif os.path.exists(file_path):\n with open(file_path, 'rb') as f:\n input_file = InputFile(f, read_file_handle=False)\n msg = await update.message.reply_document(input_file, caption='本地文件直传', write_timeout=300, connect_timeout=30)\n #msg = await update.message.reply_document(f, caption='本地文件直传')\n new_file_id = msg.document.file_id\n else:\n await update.message.reply_text('文件丢失。')\n return\n # 关键:本地直传后写入tg_file_id\n if new_file_id:\n with SessionLocal() as session:\n file = session.query(File).filter_by(file_id=file_id).first()\n if file:\n file.tg_file_id = new_file_id\n session.commit()\n elif os.path.exists(file_path):\n ext = os.path.splitext(file_path)[1].lower()\n if ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']:\n with open(file_path, 'rb') as f:\n await update.message.reply_photo(f, caption=f'文件tg_file_id: {tg_file_id}')\n elif ext in ['.mp4', '.mov', '.avi', '.mkv', '.webm']:\n with open(file_path, 'rb') as f:\n await update.message.reply_video(f, caption=f'文件tg_file_id: {tg_file_id}')\n else:\n with open(file_path, 'rb') as f:\n await update.message.reply_document(f, caption=f'文件tg_file_id: {tg_file_id}')\n else:\n await update.message.reply_text('文件丢失。')\n except Exception as e:\n await update.message.reply_text(f'发送失败: {e}')\n\ndef main():\n upgrade_users_table() # 启动时自动升级users表结构\n base_url = os.getenv('TELEGRAM_API_URL')\n request = HTTPXRequest(\n connect_timeout=60.0, # Connection timeout\n read_timeout=1810.0, # Should be \u003e TDLIB_UPLOAD_FILE_TIMEOUT\n write_timeout=1810.0, # Should be \u003e TDLIB_UPLOAD_FILE_TIMEOUT\n pool_timeout=60.0, # Pool timeout\n media_write_timeout=1810.0\n )\n builder = ApplicationBuilder().token(TOKEN).request(request)\n if base_url:\n builder = builder.base_url(f\"{base_url}/bot\").base_file_url(f\"{base_url}/file/bot\")\n app = builder.build()\n\n # 用 post_init 钩子自动注入 bot 用户名\n async def set_username(app):\n me = await app.bot.get_me()\n set_bot_username(me.username)\n app.post_init = set_username\n app.add_handler(CommandHandler('start', on_start))\n\n\n app.run_polling()\n\nif __name__ == '__main__':\n main()\n```\n\n### Operating System\n\nwin10\n\n### Version of Python, python-telegram-bot \u0026 dependencies\n\n```shell\npython-telegram-bot 22.1\nBot API 9.0\nPython 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]\n```","author":{"url":"https://github.com/aiastia","@type":"Person","name":"aiastia"},"datePublished":"2025-06-09T06:19:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/4821/python-telegram-bot/issues/4821"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:790af697-fc74-65a0-e123-a4f94c8bb947 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D83A:3FD786:2504E2:317A95:6A5CB972 |
| html-safe-nonce | 75b7be869e74d645c6120871573387625d9558d7704f9f893920c072925b75cb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODNBOjNGRDc4NjoyNTA0RTI6MzE3QTk1OjZBNUNCOTcyIiwidmlzaXRvcl9pZCI6IjU3NjkxNDkzNzU0MjQ4MDUyMzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | aac3d3b5a0ebeefad8a49d98ffb1a6a81c51aee02be6f5b35ffc09a7cdc60541 |
| hovercard-subject-tag | issue:3129279717 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/4821/issue_layout |
| twitter:image | https://opengraph.githubassets.com/33965fbf5de63c53d6f00d88daeef01a3446f043e27f01e7d1f885a1887abf51/python-telegram-bot/python-telegram-bot/issues/4821 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/33965fbf5de63c53d6f00d88daeef01a3446f043e27f01e7d1f885a1887abf51/python-telegram-bot/python-telegram-bot/issues/4821 |
| og:image:alt | Issue I am facing with open(file_path, 'rb') as f: input_file = InputFile(f, read_file_handle=False) msg = await update.message.reply_document(input_file, caption='本地文件直传', write_timeout=300, conne... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aiastia |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width