go-telegram / bot

Telegram Bot API Go framework
MIT License
502 stars 46 forks source link

SendMediaGroup does not work when SendMediaGroupParams is an array of mixed InputMediaDocument consisting of both images and videos #80

Open QuynhVir opened 2 months ago

QuynhVir commented 2 months ago

I am migrating my Telegram bot written in telegraf.js to Go. This is my old working code using telegraf.js:

const payload = response.data.items[0].carousel_media.map((item) => {
  return {
    type: "document",
    media: {
      url:
        item.media_type === 1
          ? item.image_versions2.candidates[0].url
          : item.video_versions[0].url,
      filename: `${item.id}.${item.media_type === 1 ? "jpg" : "mp4"}`,
    },
  };
});

await ctx.replyWithMediaGroup(payload);

Case 1 [WORKING]:

    image := &models.InputMediaDocument{
        Media: <IMAGE_URL>,
    }
    params := &bot.SendMediaGroupParams{
        ChatID: update.Message.Chat.ID,
        Media: []models.InputMedia{
            image,
        },
    }

Case 2 [WORKING]:

    image1 := &models.InputMediaDocument{
        Media: <IMAGE_URL>,
    }
    image2 := &models.InputMediaDocument{
        Media: <IMAGE_URL>,
    }
    params := &bot.SendMediaGroupParams{
        ChatID: update.Message.Chat.ID,
        Media: []models.InputMedia{
            image1,
            image2,
        },
    }

Case 3 [NOT EXPECTED]:

    video := &models.InputMediaDocument{
        Media: <VIDEO_URL>,
    }
    params := &bot.SendMediaGroupParams{
        ChatID: update.Message.Chat.ID,
        Media: []models.InputMedia{
            video,
        },
    }

In this case, it works but not the way I want, the bot responds as a video, not a document, while I want the bot to send the document, so I can download the original file.

Case 4 [NOT WORKING]:

    image := &models.InputMediaDocument{
        Media: <IMAGE_URL>,
    }
    video := &models.InputMediaDocument{
        Media: <VIDEO_URL>,
    }
    params := &bot.SendMediaGroupParams{
        ChatID: update.Message.Chat.ID,
        Media: []models.InputMedia{
            image,
                        video,
        },
    }

In this case, I received the error Bad Request: failed to send message #1 with the error message "MEDIA_INVALID"

QuynhVir commented 2 months ago

I just read the source code of telegraf.js and turned on the debugger to find out, it turns out they post the file using multipart/form-data. So there's nothing wrong with this library, it's just that telegraf.js makes it easier by accepting the URL as well and handling the rest automatically.

You should warn users about this. Thank you for your hard work on this project.