Skip to content

fix(weixin_oc): allow CDN uploads to use upload_full_url when provided #7066

Merged
Soulter merged 3 commits intoAstrBotDevs:masterfrom
Astral-Yang:dev_weixin_cdn
Mar 28, 2026
Merged

fix(weixin_oc): allow CDN uploads to use upload_full_url when provided #7066
Soulter merged 3 commits intoAstrBotDevs:masterfrom
Astral-Yang:dev_weixin_cdn

Conversation

@Astral-Yang
Copy link
Copy Markdown
Contributor

@Astral-Yang Astral-Yang commented Mar 28, 2026

适配微信开放平台getuploadurl接口返回的新格式,该接口现在可能返回upload_full_url字段。 优先使用upload_full_url作为上传地址,以保持上传功能正常。

当前会出现返回upload_param为空的问题导致发送失败

[2026-03-28 13:59:17.551] [Plug] [DBUG] [douyin.handler:42]: 🎵 估算抖音视频大小: 1.96 MB
[2026-03-28 13:59:19.006] [Plug] [DBUG] [douyin.handler:341]: 📥 抖音视频下载成功: size=1.96MB, 耗时=1.80s
[2026-03-28 13:59:19.007] [Plug] [DBUG] [douyin.handler:431]: 🚀 抖音普通消息准备发送: 媒体数=1
[2026-03-28 13:59:19.419] [Core] [DBUG] [weixin_oc.weixin_oc_adapter:254]: weixin_oc(WechatBot): getuploadurl response user=o9cq80x32v2pw3Twb2uv8MoTmPEM@im.wechat media_type=2 raw_size=2052244 raw_md5=7e6f5be86d3bbfc4d51371c04cbf85e8 filekey=daa18c484b6d4eabaa613d520c0b7489 file=4a9cc64331ef525d_2fede061.mp4 upload_param_len=0
[2026-03-28 13:59:19.421] [Core] [ERRO] [v4.22.1] [weixin_oc.weixin_oc_adapter:524]: weixin_oc(WechatBot): prepare media failed: getuploadurl returned empty upload_param

Modifications / 改动点

  • weixin_oc_adapter.py 增加upload_full_url参数校验

  • weixin_oc_client.py 增加upload_full_url参数赋值给cdn_url

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

# @tencent-weixin/openclaw-weixin 2.11 官方插件逻辑
  const { buf, uploadFullUrl, uploadParam, filekey, cdnBaseUrl, label, aeskey } = params;
  const ciphertext = encryptAesEcb(buf, aeskey);
  const trimmedFull = uploadFullUrl?.trim();
  let cdnUrl: string;
  if (trimmedFull) {
    cdnUrl = trimmedFull;
  } else if (uploadParam) {
    cdnUrl = buildCdnUploadUrl({ cdnBaseUrl, uploadParam, filekey });
  } else {
    throw new Error(`${label}: CDN upload URL missing (need upload_full_url or upload_param)`);
  }

Screenshot_20260328_125456_WeChat


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Allow CDN uploads to use upload_full_url when provided and fall back to constructing the URL from upload_param, avoiding failures when upload_param is missing in the new API response.

适配微信开放平台`getuploadurl`接口返回的新格式,该接口现在可能返回`upload_full_url`字段。
优先使用`upload_full_url`作为上传地址,以保持上传功能正常。
@auto-assign auto-assign bot requested review from Fridemn and advent259141 March 28, 2026 04:56
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 28, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In upload_to_cdn, the parameter order (upload_param before upload_full_url) is opposite to the new priority logic; consider reordering the arguments or using keyword-only parameters at the call site to make it harder to accidentally swap them in future changes.
  • You currently normalize and trim upload_param/upload_full_url both in the adapter and again in the client; consider consolidating this normalization in one place (adapter or client) to avoid duplicated logic and potential divergence later.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `upload_to_cdn`, the parameter order (`upload_param` before `upload_full_url`) is opposite to the new priority logic; consider reordering the arguments or using keyword-only parameters at the call site to make it harder to accidentally swap them in future changes.
- You currently normalize and trim `upload_param`/`upload_full_url` both in the adapter and again in the client; consider consolidating this normalization in one place (adapter or client) to avoid duplicated logic and potential divergence later.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 28, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for upload_full_url in the WeChat OC adapter and client, enabling direct CDN uploads. The upload_to_cdn method now handles both full URLs and parameter-based URL construction. A suggestion was made to use the walrus operator to simplify the URL cleaning and validation logic in the client.

Comment thread astrbot/core/platform/sources/weixin_oc/weixin_oc_client.py Outdated
- 在适配器中,将通用错误信息具体化为“CDN上传URL缺失”
- 在客户端中,移除冗余的参数处理逻辑,使参数验证更清晰
@Astral-Yang
Copy link
Copy Markdown
Contributor Author

@gemini-code-assist review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for using a full upload URL in the WeChat OC CDN upload process, allowing the system to bypass manual URL construction when upload_full_url is provided. The review identified a critical bug where the parameter order in the upload_to_cdn call in the adapter does not match the updated function signature in the client, which would cause logic errors. Additionally, there is redundant validation logic and inconsistent exception handling between the adapter and the client that should be consolidated to improve code maintainability.

Comment on lines +603 to +604
upload_param,
upload_full_url,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

此处的 upload_paramupload_full_url 参数顺序与 WeixinOCClient.upload_to_cdn 中的函数定义不符。这会导致构建CDN上传URL时逻辑错误。请交换它们的顺序以匹配函数签名。

Suggested change
upload_param,
upload_full_url,
upload_full_url,
upload_param,

Comment thread astrbot/core/platform/sources/weixin_oc/weixin_oc_client.py
@Astral-Yang
Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The upload_to_cdn parameter order is inconsistent between weixin_oc_client.py and weixin_oc_adapter.py: the method now expects (upload_full_url, upload_param, ...) but the adapter is calling it as (upload_param, upload_full_url, ...), which will swap the values at runtime and should be corrected.
  • Since upload_full_url and upload_param are mutually alternative inputs, consider making both keyword-only or giving them explicit names at call sites to avoid similar ordering issues in future refactors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `upload_to_cdn` parameter order is inconsistent between `weixin_oc_client.py` and `weixin_oc_adapter.py`: the method now expects `(upload_full_url, upload_param, ...)` but the adapter is calling it as `(upload_param, upload_full_url, ...)`, which will swap the values at runtime and should be corrected.
- Since `upload_full_url` and `upload_param` are mutually alternative inputs, consider making both keyword-only or giving them explicit names at call sites to avoid similar ordering issues in future refactors.

## Individual Comments

### Comment 1
<location path="astrbot/core/platform/sources/weixin_oc/weixin_oc_adapter.py" line_range="602-607" />
<code_context>
+                "CDN upload URL missing (need upload_full_url or upload_param)"
+            )

         encrypted_query_param = await self.client.upload_to_cdn(
             upload_param,
+            upload_full_url,
             file_key,
             aes_key_hex,
             media_path,
</code_context>
<issue_to_address>
**issue (bug_risk):** Arguments to `upload_to_cdn` appear swapped relative to the updated function signature.

The updated signature is `(self, upload_full_url, upload_param, file_key, aes_key_hex, media_path)`, but this call passes `(upload_param, upload_full_url, ...)`, so the URL and param are reversed. This will select the wrong URL and can raise a `ValueError` even for valid URLs. Please swap the first two arguments to match the new signature.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/platform/sources/weixin_oc/weixin_oc_adapter.py
移除对weixin_oc_adapter中upload_param和upload_full_url同时为空的检查,因为逻辑上已由底层方法保证。
调整upload_to_cdn方法的参数顺序以匹配其内部实现,确保正确传递。
@Astral-Yang
Copy link
Copy Markdown
Contributor Author

@gemini-code-assist review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the WeChat Official Account (weixin_oc) platform source to support a full upload URL (upload_full_url) in addition to the existing parameter-based URL construction. Changes include extracting the full URL in the adapter and updating the CDN upload client to prioritize the full URL if available. A review comment suggests adding a validation check in the adapter to ensure at least one upload method is provided, which would improve error reporting by catching missing parameters earlier in the process.

Comment thread astrbot/core/platform/sources/weixin_oc/weixin_oc_adapter.py
@Astral-Yang
Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Changing upload_to_cdn to take a new leading positional argument (upload_full_url) risks subtle breakage for any existing callers; consider making it a keyword-only or optional argument with a default to preserve the previous call signature.
  • Since upload_full_url is now preferred over upload_param, it may be helpful to include the chosen URL (or at least which path was taken) in the debug log to aid in diagnosing CDN upload issues with mixed response formats.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Changing `upload_to_cdn` to take a new leading positional argument (`upload_full_url`) risks subtle breakage for any existing callers; consider making it a keyword-only or optional argument with a default to preserve the previous call signature.
- Since `upload_full_url` is now preferred over `upload_param`, it may be helpful to include the chosen URL (or at least which path was taken) in the debug log to aid in diagnosing CDN upload issues with mixed response formats.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 28, 2026
@Soulter Soulter changed the title fix(weixin_oc): 处理微信开放平台CDN上传URL的新格式 fix(weixin_oc): allow CDN uploads to use upload_full_url when provided Mar 28, 2026
@Soulter Soulter merged commit fcfd6a9 into AstrBotDevs:master Mar 28, 2026
7 checks passed
@Soulter Soulter mentioned this pull request Mar 28, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants