1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-03 17:55:52 +03:00

Сравнить коммиты

...

2 Коммитов

Автор SHA1 Сообщение Дата
Max Melentiev
b40e4b432d Merge pull request #198 from telegram-bot-rb/nested_files
Add support for editMessageMedia similar to sendMediaGroup
2023-01-14 13:18:38 +00:00
Max Melentiev
ef5ec81386 Add support for editMessageMedia similar to sendMediaGroup 2023-01-13 20:33:30 +00:00
4 изменённых файлов: 56 добавлений и 9 удалений

Просмотреть файл

@@ -1,5 +1,9 @@
# Unreleased # Unreleased
# 0.15.7
- Add support for editMessageMedia similar to sendMediaGroup.
# 0.15.6 # 0.15.6
- Rails 7.0 support. - Rails 7.0 support.

Просмотреть файл

@@ -11,8 +11,14 @@ module Telegram
def format(body, action) def format(body, action)
body = body.dup body = body.dup
if action.to_s == 'sendMediaGroup' case action.to_s
body = extract_files_from_array!(body, :media) 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 end
body.each do |key, val| body.each do |key, val|
body[key] = val.to_json if val.is_a?(Hash) || val.is_a?(Array) body[key] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
@@ -21,12 +27,19 @@ module Telegram
private private
def extract_files_from_array!(body, field_name) # Detects field by symbol or string name and replaces it with mapped value.
field_name = [field_name.to_sym, field_name.to_s].find { |x| body.key?(x) } def replace_field(hash, field_name)
return body unless field_name && body[field_name].is_a?(Array) field_name = [field_name.to_sym, field_name.to_s].find { |x| hash.key?(x) }
files = {} hash[field_name] = yield hash[field_name] if field_name
body[field_name] = body[field_name].map { |x| extract_files_from_hash(x, files) } end
body.merge!(files)
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 end
# Replace File objects with `attach` URIs. File objects are added into `files` hash. # Replace File objects with `attach` URIs. File objects are added into `files` hash.

Просмотреть файл

@@ -1,6 +1,6 @@
module Telegram module Telegram
module Bot module Bot
VERSION = '0.15.6'.freeze VERSION = '0.15.7'.freeze
def self.gem_version def self.gem_version
Gem::Version.new VERSION Gem::Version.new VERSION

Просмотреть файл

@@ -58,5 +58,35 @@ RSpec.describe Telegram::Bot::Client::RequestBodyFormatter do
it { should eq input } it { should eq input }
end end
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
end end