this is my current code : 

"
async function fetchAndSendMedia(number) {
    try {
        const response = await fetch(`https://react-tweet.vercel.app/api/tweet/${number}`);
        const jsonData = await response.json();

        if (jsonData.data === null) {
            console.log("Protected content");
            return new Response("Protected content", { status: 200 });
        }

        const allVideoUrls = new Set();
        const allImageUrls = new Set();

        // Collect all unique video and image URLs
        await collectMediaUrls(jsonData.data, allVideoUrls, allImageUrls, 'mediaDetails');

        // If quoted tweets exist, do the same
        if (jsonData.data.quoted_tweet) {
            await collectMediaUrls(jsonData.data.quoted_tweet, allVideoUrls, allImageUrls, 'mediaDetails');
        }

        // Process videos and images
        const sendVideoPromises = Array.from(allVideoUrls).map(url => sendVideo(url));
        const sendImagePromises = Array.from(allImageUrls).map(url => sendImageToTelegram(url));

        // Wait for both video and image processing to complete
        await Promise.all([...sendVideoPromises, ...sendImagePromises]);

        return new Response("Media processing completed", { status: 200 });
    } catch (error) {
        console.error(error);
        return new Response("Error processing media", { status: 500 });
    }
}

async function modifyImageUrl(inputUrl) {
    try {
        const modifiedUrl = inputUrl.replace('.jpg', '?format=jpg&name=large');
        return modifiedUrl;
    } catch (error) {
        // Handle any errors
        console.error('An error occurred:', error);
        return null;
    }
}

async function collectMediaUrls(data, videoUrls, imageUrls, mediaDetailsKey) {
    if (data[mediaDetailsKey] && data[mediaDetailsKey].length > 0) {
        for (const mediaDetail of data[mediaDetailsKey]) {
            if (mediaDetail.video_info && mediaDetail.video_info.variants) {
                // Find the highest resolution video that is less than 50MB in size
                const sortedVariants = mediaDetail.video_info.variants
                    .filter(variant => variant.content_type === "video/mp4")
                    .sort((a, b) => {
                        // Sort in descending order of bitrate; assuming higher bitrate is higher resolution
                        return b.bitrate - a.bitrate;
                    });

                for (const variant of sortedVariants) {
                    const fileSize = await getMp4FileSize(variant.url);
                    const fileSizeInMB = fileSize / (1024 * 1024);
                    // Select the highest resolution variant that is less than 50MB
                    if (fileSizeInMB < 50) {
                        videoUrls.add(variant.url);
                        break; // We've found the optimal variant, so no need to check others
                    }
                }
            } else if (mediaDetail.type === "photo" && mediaDetail.media_url_https) {
                imageUrls.add(mediaDetail.media_url_https);
            }
        }
    }
}

async function sendVideo(url) {
    try {
        const fileSize = await getMp4FileSize(url);
        const fileSizeInMB = fileSize / (1024 * 1024);
        if (fileSizeInMB < 50) {
            await sendVideoToTelegram(url);
        } else {
            console.log(`Video file size is too large: ${fileSizeInMB} MB for URL: ${url}`);
        }
    } catch (error) {
        console.error(`Failed to send video ${url}: ${error}`);
    }
}

async function sendImageToTelegram(url) {
    try {
        const largeJpgUrl = await modifyImageUrl(url);
        await sendPhoto(largeJpgUrl);
        console.log(`Image sent: ${url}`);
    } catch (error) {
        console.error(`Failed to send image ${url}: ${error}`);
    }
}

async function sendVideoToTelegram(url) {
    const botToken = "mybottoken";
    const chatId = '5225794753';
    let tgurl = `https://api.telegram.org/bot${botToken}/sendVideo?chat_id=${chatId}&video=${url}`;

    // Send the request to the Telegram API to send the video
    const response = await fetch(tgurl, { method: 'POST' });
    const data = await response.json();

    // Check the response and log accordingly
    if (data.ok) {
        console.log(`Video sent: ${url}`);
    } else {
        throw new Error(`Failed to send video: ${url}`);
    }
}

async function sendPhoto(url) {
    const botToken = "mybottoken";
    const chatId = '5225794753';
    let tgurl = `https://api.telegram.org/bot${botToken}/sendPhoto?chat_id=${chatId}&photo=${url}`;

    // Send the request to the Telegram API to send the photo
    const response = await fetch(tgurl, { method: 'POST' });
    const data = await response.json();

    // Check the response and log accordingly
    if (!data.ok) {
        throw new Error(`Failed to send photo: ${url}`);
    }
}

async function getMp4FileSize(url) {
    // Implement the MP4 file size check logic. Perform a HEAD request to the video URL and check the Content-Length header.
    // If the Content-Length header is not available, or the request fails, return 0.
    const response = await fetch(url, { method: 'HEAD' });
    const length = response.headers.get('content-length');

    if (length) {
        return parseInt(length, 10);
    } else {
        return 0;
    }
}"

this code is working perfectly but sometimes the highest resolution available video is not sent. in that case, I want to try sending second highest resolution video available. how to do that? Please make changes in my current code to do that functionality. Also show me full code of the functions without suggesting me to take it from my code. Thanks