atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 218 forks source link

send_media_group returned error: "Bad Request: media to send are not specified" #184

Closed anton-kachan-dev closed 3 years ago

anton-kachan-dev commented 6 years ago

I'm using latest version of gem 0.8.6.1. This error when I try to set 2 photos in my channel. I Use this code:

 Telegram::Bot::Client.run(TOKEN) do |bot|
  media = [Telegram::Bot::Types::InputMediaPhoto.new(media: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSMzFLAE9nbpaqjU9CYVUfcuI_m4U83TYsG3_-T1t3s2q1dzTSO0w"),
           Telegram::Bot::Types::InputMediaPhoto.new(media: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSMzFLAE9nbpaqjU9CYVUfcuI_m4U83TYsG3_-T1t3s2q1dzTSO0w")]
  bot.api.send_media_group(chat_id: CHAT_ID, media: media )
end

And I get this error: telegram-bot-ruby-0.8.6.1/lib/telegram/bot/api.rb:76:in call': Telegram API has returned the error. (ok: "false", error_code: "400", description: "Bad Request: media to send are not specified") (Telegram::Bot::Exceptions::ResponseError) from /Users/antonkachan/.rvm/gems/ruby-2.4.2/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/api.rb:60:inmethod_missing'

anton-kachan-dev commented 6 years ago

Probably InputMediaPhoto and InputMediaVideo types are missed in '/lib/telegram/bot/api.rb' file, in the INLINE_QUERY_RESULT_TYPES constant. When I added there Telegram::Bot::Types::InputMediaPhoto my code has worked. Resolve this problem please

brauliobo commented 3 years ago

OMG this is still happening, is this gem abandoned @atipugin? do you want help in maintaining?

brauliobo commented 3 years ago

@KachanAnton350504 this issue also happens if using Faraday::UploadIO and isn't fixed by @krelly's patch

brauliobo commented 3 years ago

using Firaday::UploadIO you have to use attach:// as in https://github.com/php-telegram-bot/core/issues/811, then @krelly patch works

atipugin commented 3 years ago

OMG this is still happening, is this gem abandoned @atipugin? do you want help in maintaining?

@brauliobo Gem is maintained as you can see by releases and changelog. PR #215 wasn't merged because it adds InputMedia* types to list of InlineQueryResult* which is not correct.

Anyway i've added some changes to master, following code should work now:

bot.api.send_media_group(
  chat_id: message.chat.id,
  media: [
    Telegram::Bot::Types::InputMediaPhoto.new(media: 'attach://file1'),
    Telegram::Bot::Types::InputMediaPhoto.new(media: 'attach://file2')
  ],
  file1: Faraday::UploadIO.new('/path/to/file1.png', 'image/png'),
  file2: Faraday::UploadIO.new('/path/to/file2.png', 'image/png')
)

Please check if everything's ok.

antondgx commented 2 years ago

The below code don't work! Your code also doesnt work. Possible to help me?


photos = [
  Telegram::Bot::Types::InputMediaPhoto.new(media: "https://example1.png"),
  Telegram::Bot::Types::InputMediaPhoto.new(media: "https://example2.png")
 ]

   Telegram::Bot::Client.run(@token) do |bot|
   bot.api.send_media_group(chat_id: message.chat.id, media: photos)
end
dkirilenko commented 2 years ago

@antondgx Found the solution, can you try this one:

photos = [
  Telegram::Bot::Types::InputMediaPhoto.new(
    type: 'photo',
    media: 'https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80',
    caption: 'MyCaption',
    parse_mode: ''
  ),

  Telegram::Bot::Types::InputMediaPhoto.new(
    type: 'photo',
    media: 'https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80',
    caption: 'MyCaption',
    parse_mode: ''
  )
]

Telegram::Bot::Client.run('token') do |bot|
  bot.api.send_media_group(
    chat_id: 'chat_id',
    media: photos,
    caption: 'MyCaption'
  )
end

It works too for me (you can try to open the file or try the code above instead of sending a request as in my case):

Telegram::Bot::Client.run('token') do |bot|
  bot.api.send_media_group(
    chat_id: 'chat_id',
    media: [
      Telegram::Bot::Types::InputMediaPhoto.new(media: 'attach://file1', caption: '123', parse_mode: ''),
      Telegram::Bot::Types::InputMediaPhoto.new(media: 'attach://file2', caption: '123', parse_mode: ''),
      Telegram::Bot::Types::InputMediaPhoto.new(media: 'attach://file3', caption: '123', parse_mode: '')
    ],
    file1: Faraday::FilePart.new(StringIO.new(send_request("/api/v1/cameras/1/snapshot", :get)), 'image/jpeg'),
    file2: Faraday::FilePart.new(StringIO.new(send_request("/api/v1/cameras/2/snapshot", :get)), 'image/jpeg'),
    file3: Faraday::FilePart.new(StringIO.new(send_request("/api/v1/cameras/3/snapshot", :get)), 'image/jpeg'),
    caption: 'My capture'
  )
end

Actually had a problem, just added caption: '123', parse_mode: '' to the Telegram::Bot::Types::InputMediaPhoto and it started work