зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-08-28 15:26:18 +03:00
Merge pull request #198 from telegram-bot-rb/nested_files
Add support for editMessageMedia similar to sendMediaGroup
Этот коммит содержится в:
@@ -1,5 +1,9 @@
|
||||
# Unreleased
|
||||
|
||||
# 0.15.7
|
||||
|
||||
- Add support for editMessageMedia similar to sendMediaGroup.
|
||||
|
||||
# 0.15.6
|
||||
|
||||
- Rails 7.0 support.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Ссылка в новой задаче
Block a user