go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.58k stars 862 forks source link

Send Large files to Telegram Bot #646

Open AdamRussak opened 1 year ago

AdamRussak commented 1 year ago

I am trying to send a 1.5GB video and receiving an error:

 Bad Request: file is too big

didn't find any way to go around it.

my code:

package main

import (
    "log"
    "vsplit-bot/pkg/handler"

    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

const botToken = "SecretT9ken"

func main() {
    // Create a new bot instance
    bot, err := tgbotapi.NewBotAPI(botToken)
    if err != nil {
        log.Panic(err)
    }

    // Set up an update channel to receive incoming messages
    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60

    updates := bot.GetUpdatesChan(u)

    for update := range updates {
        // Check if the update contains a video message
        if update.Message != nil && update.Message.Video != nil {
            // Get the file ID of the video message
            fileID := update.Message.Video.FileID

            // Retrieve the video file using the GetFile method
            file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID}) // <-- point of error
            if err != nil {
                log.Println(err)
                continue
            }

            downloadLink := file.Link(botToken)
            handler.DownloadFile(downloadLink, update.Message.Video.FileName)
            // Handle the downloaded video file
            log.Println("Received video file:", downloadLink)
        }
    }
}

go.mod

module asda

go 1.19

require (
    github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
    github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
    github.com/technoweenie/multipartstreamer v1.0.1 // indirect
)
ilya-khadykin commented 1 year ago

Please remove your bot's token from your example, it is a best practice to store it in environment variable.

As for your question, It looks like there is a file size limit you can send with Bots - https://core.telegram.org/bots/faq#how-do-i-upload-a-large-file

AdamRussak commented 1 year ago

I know, you are right. But already revoked it.