Skip to content

Bug: Telegram适配器在getFile时产生ERR_INVALID_URL错误 #1500

@pasical

Description

@pasical

Describe the bug

Image

不严谨分析,问题出现在Satori类的HTTP事件时尝试直接将传入的路径作为完整URL处理

Steps to reproduce

使用@myrtus/forward、forward-hime这类转发插件时,同步telegram聊天中的图片到其他平台时出现上述报错。已知网络无问题,同时在其他平台发送的图片同步到telegram时正常,即接收正常,发送不正常。

Expected behavior

使用大模型分析@satorijs/adapter-telegram/lib/index.cjs的代码后给出如下解决方案,经过修改后已正常使用。但由于我能力不足以确认其是否存在潜在问题,故不发pr

问题分析

错误发生在 http/file 处理中,当尝试使用路径 /photos/file_23.jpg 创建 URL 对象时失败:

const url = new URL(_url);  // 尝试解析 "/photos/file_23.jpg" - 失败

解决方案

不修改核心代码,只需调整 TelegramBot 类中的 $getFile 方法,确保传递完整 URL:

// 修改 TelegramBot 的 $getFile 方法
async $getFile(filePath) {
  if (this.local) {
    return await this.ctx.http.file((0, import_url.pathToFileURL)(filePath).href);
  } else {
    // 构建完整 URL,而不是仅传递相对路径
    const fullUrl = `${this.file.config.endpoint}${filePath.startsWith('/') ? filePath : '/' + filePath}`;
    // 使用完整 URL 调用 ctx.http.file
    return await this.ctx.http.file(fullUrl);
  }
}

这样修改后,传递给 http/file 的将是一个完整的 URL,而不是相对路径。

Screenshots

Image

Versions

  • OS: Mac OS Sequoia 15.4.1
  • Platform: Telegram
  • Koishi version: 4.18.7

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions