1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-03 17:55:52 +03:00
Этот коммит содержится в:
Max Melentiev
2016-05-12 14:20:51 +03:00
родитель 1f139a2b36
Коммит 9b9e55a449
9 изменённых файлов: 220 добавлений и 27 удалений

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

@@ -26,6 +26,7 @@ module Telegram
end
end
autoload :Botan, 'telegram/bot/botan'
autoload :Client, 'telegram/bot/client'
autoload :ClientStub, 'telegram/bot/client_stub'
autoload :Middleware, 'telegram/bot/middleware'

36
lib/telegram/bot/botan.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,36 @@
module Telegram
module Bot
class Botan
TRACK_URI = 'https://api.botan.io/track'.freeze
class Error < Bot::Error; end
include DebugClient
attr_reader :client, :token
def initialize(token)
@client = HTTPClient.new
@token = token
end
def track(event, uid, payload = {})
res = http_request(
:post,
TRACK_URI,
{token: token, name: event, uid: uid},
payload.to_json,
)
status = res.status
return JSON.parse(res.body) if 300 > status
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
err_msg = "#{res.reason}: #{result && result['info'] || '-'}"
raise Error, err_msg
end
def http_request(method, uri, query, body)
client.request(method, uri, query, body)
end
end
end
end

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

@@ -2,14 +2,16 @@ require 'json'
require 'httpclient'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/hash/keys'
require 'telegram/bot/debug_client'
module Telegram
module Bot
class Client
autoload :TypedResponse, 'telegram/bot/client/typed_response'
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
autoload :TypedResponse, 'telegram/bot/client/typed_response'
include DebugClient
class << self
# Accepts different options to initialize bot.
def wrap(input)
@@ -18,7 +20,7 @@ module Telegram
when Array then input.map(&method(__callee__))
when Hash then
input = input.stringify_keys
new input['token'], input['username']
new input['token'], input['username'], botan: input['botan']
when Symbol
Telegram.bots[input] or
raise "Bot #{input} not configured, check Telegram.bots_config."
@@ -41,31 +43,14 @@ module Telegram
end
end
attr_reader :client, :token, :username, :base_uri
attr_reader :client, :token, :username, :base_uri, :botan
def initialize(token, username = nil)
def initialize(token, username = nil, botan: nil)
@client = HTTPClient.new
@token = token
@username = username
@base_uri = format URL_TEMPLATE, token
end
def debug!(dev = STDOUT)
if block_given?
begin
old_dev = client.debug_dev
client.debug_dev = dev
yield
ensure
client.debug_dev = old_dev
end
else
client.debug_dev = dev
end
end
def debug_off!
client.debug_dev = nil
@botan = Botan.new(botan) if botan
end
def request(action, body = {}) # rubocop:disable PerceivedComplexity

23
lib/telegram/bot/debug_client.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,23 @@
module Telegram
module Bot
module DebugClient
def debug!(dev = STDOUT)
if block_given?
begin
old_dev = client.debug_dev
client.debug_dev = dev
yield
ensure
client.debug_dev = old_dev
end
else
client.debug_dev = dev
end
end
def debug_off!
client.debug_dev = nil
end
end
end
end

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

@@ -57,6 +57,7 @@ module Telegram
require 'telegram/bot/updates_controller/log_subscriber'
require 'telegram/bot/updates_controller/instrumentation'
autoload :MessageContext, 'telegram/bot/updates_controller/message_context'
autoload :Botan, 'telegram/bot/updates_controller/botan'
include AbstractController::Callbacks
# Redefine callbacks with default terminator.

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

@@ -0,0 +1,33 @@
module Telegram
module Bot
class UpdatesController
# Helpers for botan.io metrics.
module Botan
class MissingFrom < Error; end
protected
def botan
@botan ||= bot.try!(:botan)
end
# Track custom event for user taken from `from` field:
#
# botan_track :my_event, {data: :val}
#
def botan_track(event, data = {})
raise MissingFrom, 'Can not track without user' unless from
botan.try! { |x| x.track(event, from['id'], data) }
end
# Track current action and payload for current user. Best used with `before_action`:
#
# before_action :botan_track_action
#
def botan_track_action
botan_track(action_name, payload)
end
end
end
end
end