Asociar cuentas de Telegram

This commit is contained in:
fauno 2020-06-20 21:37:29 -03:00
parent 0c53f64a87
commit 0467384c90
9 changed files with 96 additions and 1 deletions

View file

@ -32,6 +32,9 @@ gem 'bootsnap', '>= 1.1.0', require: false
# making cross-origin AJAX possible
gem 'rack-cors'
gem 'telegram-bot'
gem 'telegram-bot-types'
group :development, :test do
gem 'factory_bot_rails'
gem 'pry'

View file

@ -46,6 +46,10 @@ GEM
sshkit (>= 1.6.1, != 1.7.0)
arel (9.0.0)
ast (2.4.1)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
bcrypt (3.1.13)
bcrypt_pbkdf (1.0.1)
benchmark-ips (2.8.2)
@ -66,6 +70,8 @@ GEM
capistrano (~> 3.1)
sshkit (~> 1.3)
coderay (1.1.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.5)
@ -80,8 +86,11 @@ GEM
ruby-statistics (>= 2.1)
thor (>= 0.19, < 2)
unicode_plot (>= 0.0.4, < 1.0.0)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
ed25519 (1.2.4)
enumerable-statistics (2.0.1)
equalizer (0.0.11)
erubi (1.9.0)
factory_bot (6.0.2)
activesupport (>= 5.0.0)
@ -95,8 +104,10 @@ GEM
activesupport (>= 4.2.0)
heapy (0.1.4)
hkdf (0.3.0)
httpclient (2.8.3)
i18n (1.8.3)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jbuilder (2.10.0)
activesupport (>= 5.0.0)
jwt (2.2.1)
@ -205,6 +216,12 @@ GEM
stackprof (0.2.15)
sucker_punch (2.1.2)
concurrent-ruby (~> 1.0)
telegram-bot (0.14.4)
actionpack (>= 4.0, < 6.1)
activesupport (>= 4.0, < 6.1)
httpclient (~> 2.7)
telegram-bot-types (0.6.2)
virtus (~> 1.0)
thor (1.0.1)
thread_safe (0.3.6)
tzinfo (1.2.7)
@ -215,6 +232,11 @@ GEM
validate_url (1.0.11)
activemodel (>= 3.0.0)
public_suffix
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
webpush (1.0.0)
hkdf (~> 0.2)
jwt (~> 2.0)
@ -250,6 +272,8 @@ DEPENDENCIES
sqlite3
stackprof
sucker_punch
telegram-bot
telegram-bot-types
validate_url
webpush
yard

View file

@ -0,0 +1,26 @@
# 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
# Recibe un token, lo busca y asocia las cuentas
def start!(token = nil)
unless token
respond_with :message, text: t('.need_token')
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
end
end

View file

@ -20,4 +20,13 @@ class Pirata < ApplicationRecord
validates :nick, presence: true, uniqueness: true
scope :todas_menos, ->(pirata) { where.not(id: pirata) }
before_create :telegram_token!
# Asigna un token a cada pirata
def telegram_token!
self.telegram_token = SecureRandom.hex unless telegram_token
true
end
end

View file

@ -1 +1 @@
vHO0Rs4mUo3cmaEwumbHQ1PgQFrckB7qsLtXLeChO84w1nP9m0GZ3j/8P77cPeDa3fa61EIOp4zvTY7/blZfWTiPNN5xb0o87QEH71SrWwJvJD/iYsEaRORm0ziWdE2rEW69sEsEPuR5wvHRRWDgJZoYlf/BygqnRVGpJlvmisZMW/druYf+MMgnbdFIa25g+OyvVP3iuNh5aMxLcZ2BHc0qn7k8ROCAHQM2jtB810MArUYMyza+uDa/uXCioU0wnPvr/UQzEkvstr0DTeuvNxBQ5NCZ2pseokN7GLkZC2hUjlSirgxZR/EqP2B+CybSyLzRZRdCsVbFr//eDIBGJB0vBSjI0UsRNTNEO2ffOQNdX8Q9kxpuX+NRKrpQyRXt3/ltH8Zh4D5vOuTWd+Nf5mrEIX85imrHB1poOFtue9nBJnc8GmHwAIe4CYtbfYlNZskScubLJmLZzN7/+EOG6Q6LfEs8spz6VQ9qIX9gnUlTo2tBsaj5+2qBTqjUxed8HKRcsZXKUrx45c+8VFxI5IH37xQpkRSxtCCNl2wM5+/7g1hlGaNyv3gQTbuo9p+4rYk3P8zIpT41mqf1MHsGjNSDwS8wzjhsh3olceTCCz4yzm8x+MLd8puL3VrN8tw=--p17cT1Xv6vQyo5ES--dIpGtyQMXy3DY8iG1SThQA==
GKevZSe5cgHIFXz6MKFdLl6ndb4ludLWCixl6yiHPtdu/vnxdLB8B8ae1bUMcFiLHemdMyTTPQ8s9/N9F72hnZiBcgoOSwegRnNjEKHt4ryQFEO2HgQyVUcfPo+mqs+4cgiQC36wYpyn8tV3lH9N7LsICRaLbc6g8pLsVNIr8x3ULBDB8+KCUw9WHHpBn8L0uU55NH8Rt9RTV/5RqrYV8Iam11rx+mIvekObVmeYEfx/SPOtvz7KtYBxo0tJHKVww3HP/U0zRI278pwExNgM6yMWvPIXJnH7xh0XatOxlmmsxHHUO+ZxezgIPYeM3lizHZBfpC+TlfaEe0ysrwLUKViMVv3A8iSfym5JdoEMUpLFAzR1IyHMrbUmb1pe2HyKQNCqjVkUSot7ikOkLcDaLA5lWPc7LwZKQVrJZv9/hh+w0YJT2YTLA9vw58+eDhK/NdSVJ0MgjLIm+nr+kEudV5UfPz2dxrf/AZT9b1JlEHD0sL6CcQWfX0mdgx57lRgOBzIjcLSnbc7PcLs1P5QV8Z+aQwyL1Kedoe8jMD5FObasf/EwAH8Sttg=--GSG7fmILLHszNXqA--MCDhQnqJ0MUtZez5CaBrSA==

View file

@ -46,3 +46,9 @@ en:
en_contra: 'is against'
bloqueo: 'blocks'
indiferente: 'is indifferent'
telegram:
webhook:
start:
need_token: 'To be able to link your Lumi and Telegram accounts, you can go to Lumi and find a link to Telegram. Once you open it I can link them P)'
couldnt_find: "I couldn't find you, did you use the correct link?"
hi: "Hi %{pirata}! I linked your accounts, from now on I'll contact you whenever there's news"

View file

@ -52,3 +52,9 @@ es:
en_contra: 'está en contra'
bloqueo: 'bloquea'
indiferente: 'le es indiferente'
telegram:
webhook:
start:
need_token: 'Para poder asociar tus cuentas entre Lumi y Telegram, podés ir a Lumi y buscar el vínculo a Telegram. Al abrirlo se asocian las cuentas P)'
couldnt_find: 'No te encuentro, ¿usaste el vínculo correcto?'
hi: '¡Hola %{pirata}! Ya asocié tus cuentas de Lumi y Telegram, a partir de ahora te voy a contactar con las actualizaciones P)'

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
Rails.application.routes.draw do
telegram_webhook Telegram::WebhookController
get '/piratas/yo', to: 'piratas#yo'
# No queremos un índice de piratas
resources :piratas, only: %i[create]

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
# El token permite asociar piratas con cuentas de Telegram
class AddTelegramTokenToPiratas < ActiveRecord::Migration[5.2]
def up
add_column :piratas, :telegram_token, :string, index: true
add_column :piratas, :telegram_user, :string, index: true
Pirata.find_each do |pirata|
pirata.telegram_token!
pirata.save
end
end
def down
remove_column :piratas, :telegram_token
remove_column :piratas, :telegram_user
end
end