From 9e396cabc4b9eab04c4e863b63dfa284dfd0fafb Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Fri, 24 Nov 2017 10:57:44 +0300 Subject: [PATCH] New methods from bot api v3.5 Collect all api helper-methods in Client::ApiHelper module. Add bin/fetch-telegram-methods to update methods list from website. --- CHANGELOG.md | 6 ++ README.md | 2 + bin/fetch-telegram-methods | 41 ++++++++++++++ lib/telegram/bot/client.rb | 73 ++----------------------- lib/telegram/bot/client/api_helper.rb | 31 +++++++++++ lib/telegram/bot/client/api_methods.txt | 68 +++++++++++++++++++++++ lib/telegram/bot/initializers.rb | 2 + lib/telegram/bot/version.rb | 2 +- 8 files changed, 155 insertions(+), 70 deletions(-) create mode 100755 bin/fetch-telegram-methods create mode 100644 lib/telegram/bot/client/api_helper.rb create mode 100644 lib/telegram/bot/client/api_methods.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 22dddea..83513fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Unreleased +# 0.12.2 + +- New methods from Bot API v3.5 +- Collect all api helper-methods in Client::ApiHelper module. +- Add `bin/fetch-telegram-methods` to update API methods list from website. + # 0.12.1 - Fix `set_webhook` rake task for async bots with self-issued certificates. diff --git a/README.md b/README.md index afcdb76..fd0f5cd 100644 --- a/README.md +++ b/README.md @@ -532,6 +532,8 @@ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). +Use `bin/fetch-telegram-methods` to update API methods list from Telegram website. + ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/telegram-bot-rb/telegram-bot. diff --git a/bin/fetch-telegram-methods b/bin/fetch-telegram-methods new file mode 100755 index 0000000..0092284 --- /dev/null +++ b/bin/fetch-telegram-methods @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby +# +# 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'.freeze + +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/ + +# Filter method names. +method_list = headers. + map { |g| g.reject { |x| x.match?(NOT_METHOD_REGEXP) } }. + reject(&:empty?) + +api_version = doc.text.match(/^(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}" diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index 4220625..83f31a9 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -1,11 +1,9 @@ require 'json' require 'httpclient' -require 'active_support/core_ext/string/inflections' -require 'active_support/core_ext/hash/keys' module Telegram module Bot - class Client # rubocop:disable ClassLength + class Client URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze autoload :TypedResponse, 'telegram/bot/client/typed_response' @@ -14,6 +12,9 @@ module Telegram prepend Botan::ClientHelpers include DebugClient + require 'telegram/bot/client/api_helper' + include ApiHelper + class << self def by_id(id) Telegram.bots[id] @@ -62,72 +63,6 @@ module Telegram raise Error, "#{res.reason}: #{err_msg}" end - # Splited to the sections similar to API docs. - %w[ - deleteWebhook - getUpdates - getWebhookInfo - setWebhook - - answerCallbackQuery - deleteChatPhoto - exportChatInviteLink - forwardMessage - getChat - getChatAdministrators - getChatMember - getChatMembersCount - getFile - getMe - getUserProfilePhotos - kickChatMember - leaveChat - pinChatMessage - promoteChatMember - restrictChatMember - sendAudio - sendChatAction - sendContact - sendDocument - sendLocation - sendMessage - sendPhoto - sendVenue - sendVideo - sendVideoNote - sendVoice - setChatDescription - setChatPhoto - setChatTitle - unbanChatMember - unpinChatMessage - - deleteMessage - editMessageCaption - editMessageReplyMarkup - editMessageText - - sendSticker - getStickerSet - uploadStickerFile - createNewStickerSet - addStickerToSet - setStickerPositionInSet - deleteStickerFromSet - - answerInlineQuery - - sendInvoice - answerShippingQuery - answerPreCheckoutQuery - - getGameHighScores - sendGame - setGameScore - ].each do |method| - define_method(method.underscore) { |*args| request(method, *args) } - end - # Endpoint for low-level request. For easy host highjacking & instrumentation. # Params are not used directly but kept for instrumentation purpose. # You probably don't want to use this method directly. diff --git a/lib/telegram/bot/client/api_helper.rb b/lib/telegram/bot/client/api_helper.rb new file mode 100644 index 0000000..ee33d8b --- /dev/null +++ b/lib/telegram/bot/client/api_helper.rb @@ -0,0 +1,31 @@ +require 'active_support/core_ext/string/inflections' + +module Telegram + module Bot + class Client + module ApiHelper + METHODS_LIST_FILE = File.expand_path('../api_methods.txt', __FILE__) + + class << self + def methods_list(file = METHODS_LIST_FILE) + File.read(file).lines. + map(&:strip). + reject { |x| x.empty? || x.start_with?('#') } + end + + # Defines method with underscored name to post to specific endpoint: + # + # define_method :getMe + # # defines #get_me + def define_helpers(*list) + list.map(&:to_s).each do |method| + define_method(method.underscore) { |*args| request(method, *args) } + end + end + end + + define_helpers(*methods_list) + end + end + end +end diff --git a/lib/telegram/bot/client/api_methods.txt b/lib/telegram/bot/client/api_methods.txt new file mode 100644 index 0000000..09ae353 --- /dev/null +++ b/lib/telegram/bot/client/api_methods.txt @@ -0,0 +1,68 @@ +# Generated with bin/fetch-telegram-methods +# Bot API 3.5. + +getUpdates +setWebhook +deleteWebhook +getWebhookInfo + +getMe +sendMessage +forwardMessage +sendPhoto +sendAudio +sendDocument +sendVideo +sendVoice +sendVideoNote +sendMediaGroup +sendLocation +editMessageLiveLocation +stopMessageLiveLocation +sendVenue +sendContact +sendChatAction +getUserProfilePhotos +getFile +kickChatMember +unbanChatMember +restrictChatMember +promoteChatMember +exportChatInviteLink +setChatPhoto +deleteChatPhoto +setChatTitle +setChatDescription +pinChatMessage +unpinChatMessage +leaveChat +getChat +getChatAdministrators +getChatMembersCount +getChatMember +setChatStickerSet +deleteChatStickerSet +answerCallbackQuery + +editMessageText +editMessageCaption +editMessageReplyMarkup +deleteMessage + +sendSticker +getStickerSet +uploadStickerFile +createNewStickerSet +addStickerToSet +setStickerPositionInSet +deleteStickerFromSet + +answerInlineQuery + +sendInvoice +answerShippingQuery +answerPreCheckoutQuery + +sendGame +setGameScore +getGameHighScores diff --git a/lib/telegram/bot/initializers.rb b/lib/telegram/bot/initializers.rb index b0f9c71..3dfb08e 100644 --- a/lib/telegram/bot/initializers.rb +++ b/lib/telegram/bot/initializers.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/keys' + module Telegram module Bot module Initializers diff --git a/lib/telegram/bot/version.rb b/lib/telegram/bot/version.rb index e994a91..ffb1a9f 100644 --- a/lib/telegram/bot/version.rb +++ b/lib/telegram/bot/version.rb @@ -1,6 +1,6 @@ module Telegram module Bot - VERSION = '0.12.1'.freeze + VERSION = '0.12.3'.freeze def self.gem_version Gem::Version.new VERSION