From 0467384c90c53ccdd2fa41ede38f5bde8b2cc651 Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 20 Jun 2020 21:37:29 -0300 Subject: [PATCH] Asociar cuentas de Telegram --- Gemfile | 3 +++ Gemfile.lock | 24 +++++++++++++++++ .../telegram/webhook_controller.rb | 26 +++++++++++++++++++ app/models/pirata.rb | 9 +++++++ config/credentials.yml.enc | 2 +- config/locales/en.yml | 6 +++++ config/locales/es.yml | 6 +++++ config/routes.rb | 2 ++ ...620234913_add_telegram_token_to_piratas.rb | 19 ++++++++++++++ 9 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 app/controllers/telegram/webhook_controller.rb create mode 100644 db/migrate/20200620234913_add_telegram_token_to_piratas.rb diff --git a/Gemfile b/Gemfile index 0d9bf3c..00c02df 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 0587680..b87dd8d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/controllers/telegram/webhook_controller.rb b/app/controllers/telegram/webhook_controller.rb new file mode 100644 index 0000000..8ec7c46 --- /dev/null +++ b/app/controllers/telegram/webhook_controller.rb @@ -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 diff --git a/app/models/pirata.rb b/app/models/pirata.rb index 0816708..5f1a229 100644 --- a/app/models/pirata.rb +++ b/app/models/pirata.rb @@ -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 diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index c085c63..35f2052 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -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== \ No newline at end of file +GKevZSe5cgHIFXz6MKFdLl6ndb4ludLWCixl6yiHPtdu/vnxdLB8B8ae1bUMcFiLHemdMyTTPQ8s9/N9F72hnZiBcgoOSwegRnNjEKHt4ryQFEO2HgQyVUcfPo+mqs+4cgiQC36wYpyn8tV3lH9N7LsICRaLbc6g8pLsVNIr8x3ULBDB8+KCUw9WHHpBn8L0uU55NH8Rt9RTV/5RqrYV8Iam11rx+mIvekObVmeYEfx/SPOtvz7KtYBxo0tJHKVww3HP/U0zRI278pwExNgM6yMWvPIXJnH7xh0XatOxlmmsxHHUO+ZxezgIPYeM3lizHZBfpC+TlfaEe0ysrwLUKViMVv3A8iSfym5JdoEMUpLFAzR1IyHMrbUmb1pe2HyKQNCqjVkUSot7ikOkLcDaLA5lWPc7LwZKQVrJZv9/hh+w0YJT2YTLA9vw58+eDhK/NdSVJ0MgjLIm+nr+kEudV5UfPz2dxrf/AZT9b1JlEHD0sL6CcQWfX0mdgx57lRgOBzIjcLSnbc7PcLs1P5QV8Z+aQwyL1Kedoe8jMD5FObasf/EwAH8Sttg=--GSG7fmILLHszNXqA--MCDhQnqJ0MUtZez5CaBrSA== \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 5a62954..484b9cf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/config/locales/es.yml b/config/locales/es.yml index e96a496..9366598 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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)' diff --git a/config/routes.rb b/config/routes.rb index 45d559e..bb0f7c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/db/migrate/20200620234913_add_telegram_token_to_piratas.rb b/db/migrate/20200620234913_add_telegram_token_to_piratas.rb new file mode 100644 index 0000000..0cf17fd --- /dev/null +++ b/db/migrate/20200620234913_add_telegram_token_to_piratas.rb @@ -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