From 9be07a462609e7f1d6f0791ca107dcfc56cff950 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sun, 18 Apr 2021 11:52:33 +0200 Subject: [PATCH] Add Decidim Twilio SMS configuration setup --- lxc-apps/decidim/app | 4 +- lxc-apps/decidim/decidim.image | 2 +- .../decidim.image.d/srv/decidim-app/Gemfile | 1 + .../install/decidim_conf/application.yml | 4 ++ .../decidim_conf/initializers/decidim.rb | 39 ++++++++++++++++--- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lxc-apps/decidim/app b/lxc-apps/decidim/app index 1afade4..70fbeb1 100644 --- a/lxc-apps/decidim/app +++ b/lxc-apps/decidim/app @@ -1,5 +1,5 @@ { - "version": "0.23.5-210408", + "version": "0.23.5-210416", "meta": { "title": "Decidim", "desc-cs": "Platforma pro účast občanů", @@ -8,7 +8,7 @@ }, "containers": { "decidim": { - "image": "decidim_0.23.5-210408", + "image": "decidim_0.23.5-210416", "depends": [ "decidim-postgres" ], diff --git a/lxc-apps/decidim/decidim.image b/lxc-apps/decidim/decidim.image index 161d64a..fef15f4 100644 --- a/lxc-apps/decidim/decidim.image +++ b/lxc-apps/decidim/decidim.image @@ -1,4 +1,4 @@ -IMAGE decidim_0.23.5-210408 +IMAGE decidim_0.23.5-210416 FROM decidim-nginx_1.18.0-210106 # https://github.com/Platoniq/decidim-install/blob/master/decidim-bionic.md diff --git a/lxc-apps/decidim/decidim.image.d/srv/decidim-app/Gemfile b/lxc-apps/decidim/decidim.image.d/srv/decidim-app/Gemfile index c14d418..4de66c1 100644 --- a/lxc-apps/decidim/decidim.image.d/srv/decidim-app/Gemfile +++ b/lxc-apps/decidim/decidim.image.d/srv/decidim-app/Gemfile @@ -38,6 +38,7 @@ group :production do gem "passenger" gem "delayed_job_active_record" gem "daemons" + gem "twilio-ruby" end # Modules diff --git a/lxc-apps/decidim/install/decidim_conf/application.yml b/lxc-apps/decidim/install/decidim_conf/application.yml index e72713d..107b983 100644 --- a/lxc-apps/decidim/install/decidim_conf/application.yml +++ b/lxc-apps/decidim/install/decidim_conf/application.yml @@ -4,4 +4,8 @@ SMTP_USERNAME: "admin@example.com" SMTP_PASSWORD: "" SMTP_ADDRESS: "decidim-smtp" SMTP_DOMAIN: "example.com" +HERE_API_KEY: "" +TWILIO_ACCOUNT_SID: "" +TWILIO_AUTH_TOKEN: "" +TWILIO_SENDER_NUMBER: "" RAILS_LOG_TO_STDOUT: "1" diff --git a/lxc-apps/decidim/install/decidim_conf/initializers/decidim.rb b/lxc-apps/decidim/install/decidim_conf/initializers/decidim.rb index 09874ca..4822f9e 100644 --- a/lxc-apps/decidim/install/decidim_conf/initializers/decidim.rb +++ b/lxc-apps/decidim/install/decidim_conf/initializers/decidim.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "twilio-ruby" + Decidim.configure do |config| # The name of the application config.application_name = "Decidim" @@ -33,10 +35,12 @@ Decidim.configure do |config| # config.force_ssl = true # Geocoder configuration - # config.geocoder = { - # static_map_url: "https://image.maps.ls.hereapi.com/mia/1.6/mapview", - # here_api_key: Rails.application.secrets.geocoder[:here_api_key] - # } + if !ENV["HERE_API_KEY"].to_s.empty? + config.geocoder = { + static_map_url: "https://image.maps.ls.hereapi.com/mia/1.6/mapview", + here_api_key: ENV["HERE_API_KEY"] + } + end # Custom resource reference generator method. Check the docs for more info. # config.reference_generator = lambda do |resource, component| @@ -126,7 +130,32 @@ Decidim.configure do |config| # end # end # - # config.sms_gateway_service = "MySMSGatewayService" + + class TwilioSMSGatewayService + attr_reader :mobile_phone_number, :code + + def initialize(mobile_phone_number, code) + @mobile_phone_number = mobile_phone_number + @code = code + end + + def deliver_code + Rails.logger.debug("Twilio SMS gateway service, verification code is: #{code}, should have been delivered to #{mobile_phone_number}") + client.messages.create( + from: ENV["TWILIO_SENDER_NUMBER"], + to: mobile_phone_number, + body: code + ) + end + + def client + ::Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"] + end + end + + if !ENV["TWILIO_ACCOUNT_SID"].to_s.empty? + config.sms_gateway_service = "TwilioSMSGatewayService" + end # Timestamp service configuration #