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

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

...

3 Коммитов

Автор 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
Max Melentiev
7d8a19d3c8 Pass action parameter to RequestBodyFormatter 2022-03-13 21:39:55 +00:00
7 изменённых файлов: 62 добавлений и 14 удалений

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

@@ -1,6 +1,10 @@
# Unreleased # Unreleased
# 0.15.5 # 0.15.7
- Add support for editMessageMedia similar to sendMediaGroup.
# 0.15.6
- Rails 7.0 support. - Rails 7.0 support.
- Fix sending File objects in nested objects in sendMediaGroup. - Fix sending File objects in nested objects in sendMediaGroup.

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

@@ -2,7 +2,7 @@
[![Gem Version](https://badge.fury.io/rb/telegram-bot.svg)](http://badge.fury.io/rb/telegram-bot) [![Gem Version](https://badge.fury.io/rb/telegram-bot.svg)](http://badge.fury.io/rb/telegram-bot)
[![Code Climate](https://codeclimate.com/github/telegram-bot-rb/telegram-bot/badges/gpa.svg)](https://codeclimate.com/github/telegram-bot-rb/telegram-bot) [![Code Climate](https://codeclimate.com/github/telegram-bot-rb/telegram-bot/badges/gpa.svg)](https://codeclimate.com/github/telegram-bot-rb/telegram-bot)
[![Build Status](https://travis-ci.com/telegram-bot-rb/telegram-bot.svg)](https://travis-ci.com/telegram-bot-rb/telegram-bot) [![Build Status](https://app.travis-ci.com/telegram-bot-rb/telegram-bot.svg)](https://app.travis-ci.com/github/telegram-bot-rb/telegram-bot)
Tools for developing Telegram bots. Best used with Rails, but can be used in Tools for developing Telegram bots. Best used with Rails, but can be used in
[standalone app](https://github.com/telegram-bot-rb/telegram-bot/wiki/Not-rails-application). [standalone app](https://github.com/telegram-bot-rb/telegram-bot/wiki/Not-rails-application).

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

@@ -37,7 +37,7 @@ module Telegram
end end
def prepare_async_args(action, body = {}) def prepare_async_args(action, body = {})
[action.to_s, Async.prepare_hash(RequestBodyFormatter.format(body))] [action.to_s, Async.prepare_hash(RequestBodyFormatter.format(body, action))]
end end
def error_for_response(response) def error_for_response(response)
@@ -63,7 +63,7 @@ module Telegram
end end
def request(action, body = {}) def request(action, body = {})
response = http_request("#{base_uri}#{action}", RequestBodyFormatter.format(body)) response = http_request("#{base_uri}#{action}", RequestBodyFormatter.format(body, action))
raise self.class.error_for_response(response) if response.status >= 300 raise self.class.error_for_response(response) if response.status >= 300
JSON.parse(response.body) JSON.parse(response.body)
end end

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

@@ -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.5'.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

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

@@ -129,7 +129,8 @@ RSpec.describe Telegram::Bot::Client do
let(:body) { body_json.to_json } let(:body) { body_json.to_json }
let(:body_json) { {'param' => 'val', 'description' => 'some description'} } let(:body_json) { {'param' => 'val', 'description' => 'some description'} }
before do before do
expect(described_class).to receive(:prepare_body).with(request_body) { prepared_body } expect(Telegram::Bot::Client::RequestBodyFormatter).
to receive(:format).with(request_body, action) { prepared_body }
expect(instance).to receive(:http_request).with(url, prepared_body) { response } expect(instance).to receive(:http_request).with(url, prepared_body) { response }
end end
around { |ex| Telegram::Bot::ClientStub.stub_all!(false) { ex.run } } around { |ex| Telegram::Bot::ClientStub.stub_all!(false) { ex.run } }