Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to API 8.0 #314

Merged
merged 15 commits into from
Nov 24, 2024
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.1.0

### Added

- [Bot API 8.0](https://core.telegram.org/bots/api#november-17-2024)

## 2.0.0

### Added
Expand Down
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.4'
gem 'vcr', '~> 6.0'

gem 'rubocop', '~> 1.54.1'
gem 'rubocop', '~> 1.66.1'
gem 'rubocop-performance', '~> 1.18'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec', '~> 2.22.0'
gem 'rubocop-rspec', '~> 3.1.0'

group :development do
gem 'openapi3_parser', '~> 0.9.2'
end
45 changes: 0 additions & 45 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,3 @@ end
RSpec::Core::RakeTask.new(:spec)

task default: :spec

desc 'Dump type definitions from docs to YAML'
task :dump_type_attributes do
require_relative 'lib/telegram/bot'
require 'nokogiri'
require 'open-uri'
require 'yaml'

# Preload every type we have
Zeitwerk::Loader.eager_load_all
types = Telegram::Bot::Types::Base.descendants.map { |c| c.name.split('::').last }

# Fetch and parse docs
doc = Nokogiri::HTML(URI.open('https://core.telegram.org/bots/api').read)

next_type_element_names = %w[table h4]

result = types.to_h do |type|
# This is very hacky but working way to find table containing attributes for
# given type. Basic idea is to find heading with type and then iterate until
# we find table with attributes or next heading (because sometimes type
# doesn't have any attributes).
element = doc.at_xpath(%{//h4[text() = "#{type}"]})
loop do
element = element.next_element
break if next_type_element_names.include?(element.name)
end

attributes = element.xpath('.//tbody//tr').map do |el|
cells = el.children.select { |c| c.name == 'td' }
{
'name' => cells[0].text,
'type' => cells[1].text,
'required' => !cells[2].text.start_with?('Optional.'),
'required_value' =>
cells[2].text.match(/^.+, (?:must be (?<found_type>\w+)|always “(?<found_type>\w+)”)$/)&.[](:found_type)
}.compact
end

[type, attributes]
end

# Write everything to fixture file
File.write "#{__dir__}/spec/support/type_attributes.yml", result.to_yaml
end
14 changes: 12 additions & 2 deletions lib/telegram/bot/api/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Api
'restrictChatMember' => Types::Bool,
'promoteChatMember' => Types::Bool,
'leaveChat' => Types::Bool,
'getChat' => Types::Chat,
'getChat' => Types::ChatFullInfo,
'getChatAdministrators' => Types::Array.of(Types::ChatMember),
'exportChatInviteLink' => Types::String,
'setChatPhoto' => Types::Bool,
Expand Down Expand Up @@ -107,7 +107,17 @@ class Api
'setMyDescription' => Types::Bool,
'getMyDescription' => Types::BotDescription,
'setMyShortDescription' => Types::Bool,
'getMyShortDescription' => Types::BotShortDescription
'getMyShortDescription' => Types::BotShortDescription,
'refundStarPayment' => Types::Bool,
'getStarTransactions' => Types::StarTransactions,
'sendPaidMedia' => Types::Message,
'createChatSubscriptionInviteLink' => Types::ChatInviteLink,
'editChatSubscriptionInviteLink' => Types::ChatInviteLink,
'getAvailableGifts' => Types::Gifts,
'sendGift' => Types::Bool,
'savePreparedInlineMessage' => Types::PreparedInlineMessage,
'editUserStarSubscription' => Types::Bool,
'setUserEmojiStatus' => Types::Bool
}.freeze
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/exceptions/response_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResponseError < Base
def initialize(response:)
@response = response

super "Telegram API has returned the error. (#{data.map { |k, v| %(#{k}: #{v.inspect}) }.join(', ')})"
super("Telegram API has returned the error. (#{data.map { |k, v| %(#{k}: #{v.inspect}) }.join(', ')})")
end

def error_code
Expand Down
16 changes: 16 additions & 0 deletions lib/telegram/bot/types/background_fill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
## Just for classes consistency
# rubocop:disable Naming/ConstantName
BackgroundFill = (
BackgroundFillSolid |
BackgroundFillGradient |
BackgroundFillFreeformGradient
)
# rubocop:enable Naming/ConstantName
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/background_fill_freeform_gradient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundFillFreeformGradient < Base
attribute :type, Types::String.constrained(eql: 'freeform_gradient').default('freeform_gradient')
attribute :colors, Types::Array.of(Types::Integer)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/telegram/bot/types/background_fill_gradient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundFillGradient < Base
attribute :type, Types::String.constrained(eql: 'gradient').default('gradient')
attribute :top_color, Types::Integer
attribute :bottom_color, Types::Integer
attribute :rotation_angle, Types::Integer
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/background_fill_solid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundFillSolid < Base
attribute :type, Types::String.constrained(eql: 'solid').default('solid')
attribute :color, Types::Integer
end
end
end
end
17 changes: 17 additions & 0 deletions lib/telegram/bot/types/background_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
## Just for classes consistency
# rubocop:disable Naming/ConstantName
BackgroundType = (
BackgroundTypeFill |
BackgroundTypeWallpaper |
BackgroundTypePattern |
BackgroundTypeChatTheme
)
# rubocop:enable Naming/ConstantName
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/background_type_chat_theme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundTypeChatTheme < Base
attribute :type, Types::String.constrained(eql: 'chat_theme').default('chat_theme')
attribute :theme_name, Types::String
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/background_type_fill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundTypeFill < Base
attribute :type, Types::String.constrained(eql: 'fill').default('fill')
attribute :fill, BackgroundFill
attribute :dark_theme_dimming, Types::Integer
end
end
end
end
16 changes: 16 additions & 0 deletions lib/telegram/bot/types/background_type_pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundTypePattern < Base
attribute :type, Types::String.constrained(eql: 'pattern').default('pattern')
attribute :document, Document
attribute :fill, BackgroundFill
attribute :intensity, Types::Integer
attribute? :is_inverted, Types::True
attribute? :is_moving, Types::True
end
end
end
end
15 changes: 15 additions & 0 deletions lib/telegram/bot/types/background_type_wallpaper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BackgroundTypeWallpaper < Base
attribute :type, Types::String.constrained(eql: 'wallpaper').default('wallpaper')
attribute :document, Document
attribute :dark_theme_dimming, Types::Integer
attribute? :is_blurred, Types::True
attribute? :is_moving, Types::True
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/birthdate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class Birthdate < Base
attribute :day, Types::Integer
attribute :month, Types::Integer
attribute? :year, Types::Integer
end
end
end
end
4 changes: 2 additions & 2 deletions lib/telegram/bot/types/bot_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Telegram
module Bot
module Types
class BotCommand < Base
attribute :command, Types::String
attribute :description, Types::String
attribute :command, Types::String.constrained(min_size: 1, max_size: 32)
attribute :description, Types::String.constrained(min_size: 1, max_size: 256)
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/telegram/bot/types/bot_command_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
## Just for classes consistency
# rubocop:disable Naming/ConstantName
BotCommandScope = (
BotCommandScopeDefault |
BotCommandScopeAllPrivateChats |
BotCommandScopeAllGroupChats |
BotCommandScopeAllChatAdministrators |
BotCommandScopeChat |
BotCommandScopeChatAdministrators |
BotCommandScopeChatMember
)
# rubocop:enable Naming/ConstantName
end
end
end
16 changes: 16 additions & 0 deletions lib/telegram/bot/types/business_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessConnection < Base
attribute :id, Types::String
attribute :user, User
attribute :user_chat_id, Types::Integer
attribute :date, Types::Integer
attribute :can_reply, Types::Bool
attribute :is_enabled, Types::Bool
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/business_intro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessIntro < Base
attribute? :title, Types::String
attribute? :message, Types::String
attribute? :sticker, Sticker
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/business_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessLocation < Base
attribute :address, Types::String
attribute? :location, Location
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/business_messages_deleted.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessMessagesDeleted < Base
attribute :business_connection_id, Types::String
attribute :chat, Chat
attribute :message_ids, Types::Array.of(Types::Integer)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/business_opening_hours.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessOpeningHours < Base
attribute :time_zone_name, Types::String
attribute :opening_hours, Types::Array.of(BusinessOpeningHoursInterval)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/business_opening_hours_interval.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Telegram
module Bot
module Types
class BusinessOpeningHoursInterval < Base
attribute :opening_minute, Types::Integer
attribute :closing_minute, Types::Integer
end
end
end
end
Loading
Loading