Send an image to Telegram with Cloudflare Workers

Hello,
I trying to send an image to Telegram with Cloudflare Workers, but I don’t receive the image in my Telegram account. Also my code correctly work in Chrome developer console, so a problem is in Cloudflare Workers. Could you help me?

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
}

var telegram_response =  await fetch(telegram_url_img, telegram_init)

Hey @swan013 – is this your whole script? You’ll need to add an event listener (see the “Writing Code” section of our Quick Start) and make a request to your Workers script for your code to execute.

1 Like