3

我尝试使用 Cloudflare Workers 向 Telegram 发送图像,但我的 Telegram 帐户中没有收到图像。我的代码也可以在 Chrome 开发者控制台中正常工作,所以 Cloudflare Workers 有问题。你可以帮帮我吗?

function b64toBlob(dataURI) {
    var byteString = atob(dataURI.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);

    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], { type: 'image/jpeg' });
}

const telegram_token = "<my_bot_api_token>";
const telegram_url_img = "https://api.telegram.org/bot" + telegram_token + "/sendPhoto";
const base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEsAAAAhCAIAAAAeQ8GBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAGESURBVFhH7ZY90kUwFIZvaQm3tATLUCqVSqUdKC1BaQlKS7IEpe+dL+9kjJ84JOZi8lQ3nHOSJ8cNn/HteMPn4w2fjzd8Pt7wF3RdF4bhZw/E1HXNnG3uaPj9fikhAJ5N0zBzDVtDyX7vLmIKIpkmJggCJq9hayjcb/MiFMvN4o01hmHIsoxxxkhbQ86wR57nTNhmtlmSFIZeZyh5orDZjN5m1r00TSVZgAluDWG1+8fTSPoApt2DHq8KYI5DQ5zOLLlAKLMKSxzpnoJpDg0N3WPEKVjieBGmOTQsyxLlkiTRO60mAGp4Dpa4gyHo+56//uEMbzKcgRedmqOqKl46jqoAOBbDtEsNi6JQc0je6VuoCoBjMXp/OV7D1hB/SD0NziHJp/ASlQ44FoMHB1nmY9zWEOg2KnAa8YYYZh43lOCgKNo4/UScgq7Cf3Y4LWH0bQ0V8IzjmCs9C2s5xWVRQzOFsJBTLimqads2iiIufw+b7z4D1xreAW/4fLzh83m74Tj+AcvdXhfpeUapAAAAAElFTkSuQmCC"

const blob = b64toBlob(base64);
var fd = new FormData();
fd.append('photo', blob)
fd.append('chat_id', <my_chat_id>)
const telegram_init = {
      method: "POST",
      body: fd
}
4

1 回答 1

2

不幸的是,BlobCloudflare Workers 目前不支持。FormData受支持,您可以使用ArrayBuffers 填充它,但没有Blob您将无法指定Content-Typeon individual FormData,这可能是此 API 的要求。

于 2019-09-30T15:52:51.180 回答