yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.13k stars 1.49k forks source link

sendMediaGroup method is not working sometimes #1148

Closed Okashichan closed 4 months ago

Okashichan commented 8 months ago

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

Send all images via URL.

Actual Behavior

Most of the time, it returns a 429 error. But it worked flawlessly for a year.

I send the images in chunks (9 images per chunk) with a set interval. As there are limits for both group and private chat, I have set the interval to 65 seconds for groups and 3 seconds for private chat. However, the behavior is the same in both scenarios. Here is an example of a chunk:

[
  {
    type: 'photo',
    media: 'https://p16-sign-va.tiktokcdn.com/tos-maliva-i-photomode-us/4bf476b2fe2b429189980b02f26b5dde~tplv-photomode-image.jpeg?from=photomode.FEED&x-expires=1699779600&x-signature=1XQFOUEbjQyka3o2pxyjU8zDcxw%3D'
  },
  {
    type: 'photo',
    media: 'https://p16-sign-va.tiktokcdn.com/tos-maliva-i-photomode-us/43eaec02992a4e399b7336bc1330a206~tplv-photomode-image.jpeg?from=photomode.FEED&x-expires=1699779600&x-signature=p9PtBtRh2ir5OoSU2REp6RqCjk0%3D'
  }
]

Example

let interval = chatType === 'private' ? 3000 : 65000

sendMediaGroupOptions = {
    reply_to_message_id: userMsgId,
    disable_notification: true,
    allow_sending_without_reply: true
}

data.images.forEach((el, index) => {
    setTimeout(() => {
        console.log(`       part #${index + 1}; size=${el.length}; timeout=${interval * index}`)
        bot.sendMediaGroup(chatId, el, sendMediaGroupOptions).catch((err) => {
            console.log(err.code)
            console.log(err.response?.body)
        })
    }, interval * index)
})
yagop commented 8 months ago

Maybe a rate limit in Telegram servers.

Did you try to send them asyncronously. Something like:


for (const image of data.images) {
   ... 
   await bot.sendMediaGroup(chatId, el, sendMediaGroupOptions);
}
kul-sudo commented 6 months ago

I have the same issue.

Okashichan commented 6 months ago

@kul-sudo, try using the method suggested by @yagop. I ended up with a different approach by the time he replied, so try it and let us know if it works for you.

kul-sudo commented 6 months ago

@kul-sudo, try using the method suggested by @yagop. I ended up with a different approach by the time he replied, so try it and let us know if it works for you.

What's great is that it actually works. Thanks for the suggestion.