Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 0.16.8

- Update to Bot API 9.0

# 0.16.7

- Update to Bot API 8.2
Expand Down
32 changes: 25 additions & 7 deletions lib/telegram/bot/client/api_methods.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated with bin/fetch-telegram-methods
# Bot API 8.2
# Bot API 9.0

getUpdates
setWebhook
Expand Down Expand Up @@ -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
Expand All @@ -119,12 +143,6 @@ setStickerSetTitle
setStickerSetThumbnail
setCustomEmojiStickerSetThumbnail
deleteStickerSet
getAvailableGifts
sendGift
verifyUser
verifyChat
removeUserVerification
removeChatVerification

answerInlineQuery
answerWebAppQuery
Expand Down
27 changes: 15 additions & 12 deletions lib/telegram/bot/client/request_body_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
1 change: 1 addition & 0 deletions lib/telegram/bot/updates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'logger'
require 'abstract_controller'
require 'active_support/core_ext/string/inflections'
require 'active_support/callbacks'
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/updates_controller/payload_types.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated with bin/fetch-telegram-methods
# Bot API 8.2
# Bot API 9.0
message
edited_message
channel_post
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Telegram
module Bot
VERSION = '0.16.7'
VERSION = '0.16.8'

def self.gem_version
Gem::Version.new VERSION
Expand Down
30 changes: 30 additions & 0 deletions spec/telegram/bot/client/request_body_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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