diff --git a/CHANGELOG.md b/CHANGELOG.md index 274b4c0..b0545ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +# 0.16.8 + +- Update to Bot API 9.0 + # 0.16.7 - Update to Bot API 8.2 diff --git a/lib/telegram/bot/client/api_methods.txt b/lib/telegram/bot/client/api_methods.txt index bad7272..aab059c 100644 --- a/lib/telegram/bot/client/api_methods.txt +++ b/lib/telegram/bot/client/api_methods.txt @@ -1,5 +1,5 @@ # Generated with bin/fetch-telegram-methods -# Bot API 8.2 +# Bot API 9.0 getUpdates setWebhook @@ -102,6 +102,30 @@ editMessageReplyMarkup stopPoll deleteMessage deleteMessages +getAvailableGifts +sendGift +giftPremiumSubscription +verifyUser +verifyChat +removeUserVerification +removeChatVerification +readBusinessMessage +deleteBusinessMessages +setBusinessAccountName +setBusinessAccountUsername +setBusinessAccountBio +setBusinessAccountProfilePhoto +removeBusinessAccountProfilePhoto +setBusinessAccountGiftSettings +getBusinessAccountStarBalance +transferBusinessAccountStars +getBusinessAccountGifts +convertGiftToStars +upgradeGift +transferGift +postStory +editStory +deleteStory sendSticker getStickerSet @@ -119,12 +143,6 @@ setStickerSetTitle setStickerSetThumbnail setCustomEmojiStickerSetThumbnail deleteStickerSet -getAvailableGifts -sendGift -verifyUser -verifyChat -removeUserVerification -removeChatVerification answerInlineQuery answerWebAppQuery diff --git a/lib/telegram/bot/client/request_body_formatter.rb b/lib/telegram/bot/client/request_body_formatter.rb index bcae116..a4f20b4 100644 --- a/lib/telegram/bot/client/request_body_formatter.rb +++ b/lib/telegram/bot/client/request_body_formatter.rb @@ -13,22 +13,25 @@ module RequestBodyFormatter def format(body, action) body = body.dup - 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) - end + handlers = { + 'sendMediaGroup' => -> { extract_files_from_array!(body, :media) }, + 'editMessageMedia' => -> { extract_and_merge!(body, :media) }, + 'postStory' => -> { extract_and_merge!(body, :content) }, + } + handlers[action.to_s]&.call + + body.transform_values! { |v| v.is_a?(Hash) || v.is_a?(Array) ? v.to_json : v } end private + def extract_and_merge!(body, field) + replace_field(body, field) do |value| + files = {} + extract_files_from_hash(value, files).tap { body.merge!(files) } + end + end + # 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) } diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index 628c2c8..340f0fd 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'logger' require 'abstract_controller' require 'active_support/core_ext/string/inflections' require 'active_support/callbacks' diff --git a/lib/telegram/bot/updates_controller/payload_types.txt b/lib/telegram/bot/updates_controller/payload_types.txt index b52f5cc..2c68883 100644 --- a/lib/telegram/bot/updates_controller/payload_types.txt +++ b/lib/telegram/bot/updates_controller/payload_types.txt @@ -1,5 +1,5 @@ # Generated with bin/fetch-telegram-methods -# Bot API 8.2 +# Bot API 9.0 message edited_message channel_post diff --git a/lib/telegram/bot/version.rb b/lib/telegram/bot/version.rb index 06d8d9e..576c9da 100644 --- a/lib/telegram/bot/version.rb +++ b/lib/telegram/bot/version.rb @@ -2,7 +2,7 @@ module Telegram module Bot - VERSION = '0.16.7' + VERSION = '0.16.8' 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 35f5134..e72d39a 100644 --- a/spec/telegram/bot/client/request_body_formatter_spec.rb +++ b/spec/telegram/bot/client/request_body_formatter_spec.rb @@ -90,5 +90,35 @@ it { should eq input } end end + + context 'with postStory action' do + let(:action) { :postStory } + let(:input) { {content: {a: file, b: 123}, x: 789} } + let(:file) { File.new(__FILE__) } + + it 'extracts files to the top-level' do + should eq( + content: {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( + 'content' => {a: 'attach://_file0', b: 123}.to_json, + 'x' => 789, + '_file0' => file, + ) + end + end + + context 'without content' do + let(:input) { {a: 1, b: '2', c: nil} } + it { should eq input } + end + end end end