2020-06-21 00:37:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Telegram
|
|
|
|
# Recibe los mensajes desde Telegram y asocia piratas con cuentas de
|
|
|
|
# Telegram.
|
|
|
|
class WebhookController < Telegram::Bot::UpdatesController
|
|
|
|
include Telegram::Bot::UpdatesController::TypedUpdate
|
|
|
|
|
2020-06-21 01:02:54 +00:00
|
|
|
MESSAGES = %w[aha what hi haha].freeze
|
|
|
|
|
|
|
|
def message(_)
|
|
|
|
respond_with :message, text: t('.' + MESSAGES.sample)
|
|
|
|
end
|
|
|
|
|
2020-06-21 00:37:29 +00:00
|
|
|
# Recibe un token, lo busca y asocia las cuentas
|
|
|
|
def start!(token = nil)
|
|
|
|
unless token
|
2020-06-22 16:17:09 +00:00
|
|
|
respond_with :message, text: t('.need_token', token_link: vincular_a_telegram_url)
|
2020-06-21 00:37:29 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
unless (pirata = Pirata.find_by(telegram_token: token))
|
|
|
|
respond_with :message, text: t('.couldnt_find')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
pirata.update_attribute :telegram_user, payload.from.id
|
|
|
|
|
|
|
|
respond_with :message, text: t('.hi', pirata: pirata.nick)
|
|
|
|
end
|
2020-06-22 17:51:43 +00:00
|
|
|
|
2020-06-22 16:17:09 +00:00
|
|
|
private
|
2020-06-22 17:51:43 +00:00
|
|
|
|
2020-06-22 16:17:09 +00:00
|
|
|
def vincular_a_telegram_url
|
2020-06-22 17:51:43 +00:00
|
|
|
"https://#{ENV.fetch('APP_HOST', 'lumi.partidopirata.com.ar')}/#/vincularATelegram"
|
2020-06-22 16:17:09 +00:00
|
|
|
end
|
2020-06-21 00:37:29 +00:00
|
|
|
end
|
|
|
|
end
|