1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/interactors/initialize_telegram_bot.rb

53 lines
1 KiB
Ruby
Raw Normal View History

2018-12-06 20:25:38 -05:00
# frozen_string_literal: true
class InitializeTelegramBot
include Interactor
2018-12-06 21:24:33 -05:00
include Rails.application.routes.url_helpers
2018-12-06 20:25:38 -05:00
def call
2018-12-06 21:24:33 -05:00
fetch_info
set_webhook
end
private
def fetch_info
data = call_api_method_get :getMe
2018-12-06 20:25:38 -05:00
context.fail! unless data['ok'] && context.telegram_bot.update(
username: data['result']['username'],
)
end
2018-12-06 21:24:33 -05:00
def set_webhook
data = call_api_method_get(
:setWebhook,
2018-12-06 21:36:09 -05:00
params: {
max_connections: 1,
url: webhook_url,
},
2018-12-06 21:24:33 -05:00
)
context.fail! unless data['ok']
end
def api_method_url(method)
"https://api.telegram.org/bot#{context.telegram_bot.api_token}/#{method}"
end
def call_api_method_get(method, params = {})
JSON.parse RestClient.get(api_method_url(method), params).body
end
def webhook_url
telegram_bot_updates_url(
2018-12-06 21:43:30 -05:00
context.telegram_bot,
format: 'json',
protocol: 'https',
host: Rails.application.config.site_domain,
secret: context.telegram_bot.secret,
2018-12-06 21:24:33 -05:00
)
end
2018-12-06 20:25:38 -05:00
end