1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-08-28 15:26:18 +03:00
Collect all api helper-methods in Client::ApiHelper module.
Add bin/fetch-telegram-methods to update methods list from website.
Этот коммит содержится в:
Max Melentiev
2017-11-24 10:57:44 +03:00
родитель 81c18034a5
Коммит 9e396cabc4
8 изменённых файлов: 155 добавлений и 70 удалений

Просмотреть файл

@@ -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.

Просмотреть файл

@@ -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.

41
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}"

Просмотреть файл

@@ -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.

31
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

68
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

Просмотреть файл

@@ -1,3 +1,5 @@
require 'active_support/core_ext/hash/keys'
module Telegram
module Bot
module Initializers

Просмотреть файл

@@ -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