Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· OpenAIAi2 WildChat-4.8M (AI2)

I have this cloudflare workers code which sends video to my telegram bot. I want to make…

  1. User

    I have this cloudflare workers code which sends video to my telegram bot. I want to make this function instantly return response while the video is being sent in the background.

    "addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) })

    async function handleRequest(request) { const formData = await request.formData() const videoUrl = formData.get('video_url') const chatId = formData.get('chat_id')

    // Return a success response to the Python request immediately event.waitUntil(sendVideoToTelegram(chatId, videoUrl))

    return new Response('Video sending process initiated', { status: 200 }) }

    async function sendVideoToTelegram(chatId, videoUrl) { // Download the video from the provided URL const videoResponse = await fetch(videoUrl) const videoBuffer = await videoResponse.arrayBuffer()

    // Send the video to the Telegram bot using the Telegram API endpoint const telegramApiUrl = https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendVideo const formDataTelegram = new FormData() formDataTelegram.append('chat_id', chatId) formDataTelegram.append('video', new Blob([videoBuffer]), 'video.mp4')

    await fetch(telegramApiUrl, { method: 'POST', body: formDataTelegram }) } "

  2. Assistant

    In the code snippet you provided, it looks like you were on the right track with using event.waitUntil to handle the video upload in the background. However, the event object isn't available within the handleRequest function by default, since it's a parameter for the fetch event listener but isn't passed as an argument to handleRequest.

    To fix the issue, you should pass the event object to the handleRequest function so that it can access the waitUntil method. Adjust your code like this:

    addEventListener('fetch', event => {
      event.respondWith(handleRequest(event))
    })
    
    async function handleRequest(event) {
      const request = event.request;
      const formData = await request.formData()
      const videoUrl = formData.get('video_url')
      const chatId = formData.get('chat_id')
    
      // Use waitUntil on the event to execute the sendVideoToTelegram function
      event.waitUntil(sendVideoToTelegram(chatId, videoUrl))
    
      // Return a success response to the Python request immediately
      return new Response('Video sending process initiated', { status: 200 })
    }
    
    async function sendVideoToTelegram(chatId, videoUrl) {
      // ... Your existing sendVideoToTelegram code ...
    }
    

    In this updated code, we're passing the event received from the 'fetch' listener to the handleRequest function, so inside that function, it can now correctly use event.waitUntil to run the sendVideoToTelegram method in the background after sending the immediate success response back to the caller.

    Make sure you keep your <YOUR_BOT_TOKEN> securely placed in your code (not shared publicly) and replace it in the telegramApiUrl with your actual bot token provided by Telegram.