1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-08-28 15:26:18 +03:00
Files
telegram-bot/bin/fetch-telegram-methods
2025-01-14 22:52:59 -05:00

65 строки
1.8 KiB
Ruby
Исполняемый файл

#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Fetch list of methods from Telegram docs.
# Use it to update client.rb.
require 'net/http'
require 'nokogiri'
DOCS_URL = 'https://core.telegram.org/bots/api'
page_html = Net::HTTP.get(URI(DOCS_URL))
doc = Nokogiri::HTML(page_html)
# Select `h4`s, but use `h3`s to group them.
headers = doc.css('h3, h4').
chunk_while { |_, x| x.name == 'h4' }.
map { |g| g.select { |x| x.name == 'h4' } }.
map { |g| g.map(&:text) }
# Method starts with lowercase and does not have spaces.
NOT_METHOD_REGEXP = /(\A[^a-z])|\s/.freeze
# Filter method names.
method_list = headers.
map { |g| g.grep_v(NOT_METHOD_REGEXP) }.
reject(&:empty?)
api_version = doc.text.match(/^(?:Introducing )?(Bot API ([\d\.]+))\.?$/)
result = ['# Generated with bin/fetch-telegram-methods']
result << "# #{api_version[1]}" if api_version
result << ''
result << method_list.map { |g| g.join("\n") }.join("\n\n")
result << ''
result_txt = result.join("\n")
puts result_txt
API_METHODS_FILE = File.expand_path('../lib/telegram/bot/client/api_methods.txt', __dir__).freeze
File.write(API_METHODS_FILE, result_txt)
puts '', "Updated #{API_METHODS_FILE}"
puts '', 'Payload types:'
update_tbody = doc.css('tbody').
find { |tbody| tbody.css('td').any? { |td| td.text == 'update_id' } }
payload_types = update_tbody.css('td:first-child').map(&:text).reject { |x| x == 'update_id' }
result = ['# Generated with bin/fetch-telegram-methods']
result << "# #{api_version[1]}" if api_version
result << payload_types.join("\n")
result << ''
result_txt = result.join("\n")
puts result_txt
PAYLOAD_TYPES_FILE = File.expand_path(
'../lib/telegram/bot/updates_controller/payload_types.txt',
__dir__,
).freeze
File.write(PAYLOAD_TYPES_FILE, result_txt)
puts '', "Updated #{PAYLOAD_TYPES_FILE}"