diff --git a/CHANGELOG.md b/CHANGELOG.md index b3f86a1..66f622a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +# 0.15.7 + +- Add support for editMessageMedia similar to sendMediaGroup. + # 0.15.6 - Rails 7.0 support. diff --git a/lib/telegram/bot/client/request_body_formatter.rb b/lib/telegram/bot/client/request_body_formatter.rb index 6c55c35..385e1a1 100644 --- a/lib/telegram/bot/client/request_body_formatter.rb +++ b/lib/telegram/bot/client/request_body_formatter.rb @@ -11,8 +11,14 @@ module Telegram def format(body, action) body = body.dup - if action.to_s == 'sendMediaGroup' - body = extract_files_from_array!(body, :media) + case action.to_s + when 'sendMediaGroup' + extract_files_from_array!(body, :media) + when 'editMessageMedia' + replace_field(body, :media) do |value| + files = {} + extract_files_from_hash(value, files).tap { body.merge!(files) } + end end body.each do |key, val| body[key] = val.to_json if val.is_a?(Hash) || val.is_a?(Array) @@ -21,12 +27,19 @@ module Telegram private - def extract_files_from_array!(body, field_name) - field_name = [field_name.to_sym, field_name.to_s].find { |x| body.key?(x) } - return body unless field_name && body[field_name].is_a?(Array) - files = {} - body[field_name] = body[field_name].map { |x| extract_files_from_hash(x, files) } - body.merge!(files) + # Detects field by symbol or string name and replaces it with mapped value. + def replace_field(hash, field_name) + field_name = [field_name.to_sym, field_name.to_s].find { |x| hash.key?(x) } + hash[field_name] = yield hash[field_name] if field_name + end + + def extract_files_from_array!(hash, field_name) + replace_field(hash, field_name) do |value| + break value unless value.is_a?(Array) + files = {} + value.map { |x| extract_files_from_hash(x, files) }. + tap { hash.merge!(files) } + end end # Replace File objects with `attach` URIs. File objects are added into `files` hash. diff --git a/lib/telegram/bot/version.rb b/lib/telegram/bot/version.rb index 7e0433d..0b9597a 100644 --- a/lib/telegram/bot/version.rb +++ b/lib/telegram/bot/version.rb @@ -1,6 +1,6 @@ module Telegram module Bot - VERSION = '0.15.6'.freeze + VERSION = '0.15.7'.freeze def self.gem_version Gem::Version.new VERSION diff --git a/spec/telegram/bot/client/request_body_formatter_spec.rb b/spec/telegram/bot/client/request_body_formatter_spec.rb index 3a89811..35a1e6a 100644 --- a/spec/telegram/bot/client/request_body_formatter_spec.rb +++ b/spec/telegram/bot/client/request_body_formatter_spec.rb @@ -58,5 +58,35 @@ RSpec.describe Telegram::Bot::Client::RequestBodyFormatter do it { should eq input } end end + + context 'with editMessageMedia action' do + let(:action) { :editMessageMedia } + let(:input) { {media: {a: file, b: 123}, x: 789} } + let(:file) { File.new(__FILE__) } + + it 'extracts files to the top-level' do + should eq( + media: {a: 'attach://_file0', b: 123}.to_json, + x: 789, + '_file0' => file, + ) + end + + context 'and input has string keys' do + let(:input) { super().stringify_keys } + it 'extracts files to the top-level' do + should eq( + 'media' => {a: 'attach://_file0', b: 123}.to_json, + 'x' => 789, + '_file0' => file, + ) + end + end + + context 'without media' do + let(:input) { {a: 1, b: '2', c: nil} } + it { should eq input } + end + end end end