使用Cloudflare Workers将图像发送到Telegram

时间:2019-09-28 20:52:54

标签: javascript telegram cloudflare cloudflare-workers

我试图通过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
}

1 个答案:

答案 0 :(得分:1)

很遗憾,Cloudflare Workers当前不支持Blob。支持FormData,您可以使用ArrayBuffer来填充它,但是如果没有Blob,您将无法在单个Content-Type上指定FormData,这可能是此API的要求。