class TelegramWorkflow::Client

Constants

API_VERSION
AVAILABLE_ACTIONS
DEPRECATED_ACTIONS
WebhookConfigPath

Attributes

api_url[R]
inline[RW]
inline_request[RW]

Public Class Methods

new(chat_id = nil) click to toggle source
# File lib/telegram_workflow/client.rb, line 116
def initialize(chat_id = nil)
  @chat_id = chat_id
  @api_url = "https://api.telegram.org/bot#{TelegramWorkflow.config.api_token}"
end

Public Instance Methods

__setup_webhook(webhook_url = TelegramWorkflow.config.webhook_url, params = {}) click to toggle source
# File lib/telegram_workflow/client.rb, line 131
def __setup_webhook(webhook_url = TelegramWorkflow.config.webhook_url, params = {})
  TelegramWorkflow.config.logger.info "[TelegramWorkflow] Checking webhook setup..."

  webhook_params = { url: webhook_url, allowed_updates: [], **params }

  if cached_webhook_config != webhook_params
    TelegramWorkflow.config.logger.info "[TelegramWorkflow] Setting up a new webhook..."
    set_webhook(webhook_params)
  end
end
delete_webhook(params = {}) click to toggle source
# File lib/telegram_workflow/client.rb, line 126
def delete_webhook(params = {})
  make_request("deleteWebhook", params)
  cached_webhook_config(params)
end
set_webhook(params = {}) click to toggle source
# File lib/telegram_workflow/client.rb, line 121
def set_webhook(params = {})
  make_request("setWebhook", params)
  cached_webhook_config(params)
end

Private Instance Methods

cached_webhook_config(new_config = nil) click to toggle source
# File lib/telegram_workflow/client.rb, line 144
def cached_webhook_config(new_config = nil)
  unless WebhookConfigPath.exist?
    WebhookConfigPath.dirname.mkpath
    WebhookConfigPath.write(Marshal.dump({}))
  end

  if new_config.nil?
    Marshal.load(WebhookConfigPath.read)
  else
    WebhookConfigPath.write(Marshal.dump(new_config))
  end
end
make_request(action, params = {}) click to toggle source
# File lib/telegram_workflow/client.rb, line 162
def make_request(action, params = {})
  has_file_params = params.any? { |_, param| param.is_a?(TelegramWorkflow::InputFile) }
  request_type = has_file_params ? :form : :json

  response = ::Retryable.retryable(tries: 3, on: HTTP::ConnectionError) do
    ::HTTP.post("#{@api_url}/#{action}", request_type => { chat_id: @chat_id, **params })
  end

  if response.code != 200
    raise TelegramWorkflow::Errors::ApiError, response.parse["description"]
  end

  response.parse
end
save_request(action, params = {}) click to toggle source
# File lib/telegram_workflow/client.rb, line 157
def save_request(action, params = {})
  raise TelegramWorkflow::Errors::DoubleInlineRequest if @inline_request
  @inline_request = { method: action, chat_id: @chat_id, **params }
end