decidim/decidim

View on GitHub
docs/modules/services/pages/sms.adoc

Summary

Maintainability
Test Coverage
= SMS

If you want to verify your users by sending a verification code via SMS you need to provide a SMS gateway service class through the xref:configure:initializer.adoc[initializer].

This service can also be used by some spaces, and external modules could use it too.

An example class would be something like:

[source,ruby]
....
class MySMSGatewayService
  attr_reader :mobile_phone_number, :code, :context
  def initialize(mobile_phone_number, code, context = {})
    @mobile_phone_number = mobile_phone_number
    @code = code
    @context = context
  end
  def deliver_code
    # Actual code to deliver the code
    true
  end
end
....

The arguments provided for the initialize method are:

- `mobile_phone_number` - The full mobile phone number to send the message to, containing the country code.
- `code` - The code or the message to be sent to the given mobile phone number.
- `context` - An extra context attribute which can be used to pass e.g. the correct organization for the gateway to utilize.

Then you will need to configure it in the Decidim initializer:

[source,ruby]
....
  config.sms_gateway_service = "MySMSGatewayService"
....

You can find an example on how this is set up at https://github.com/AjuntamentdeBarcelona/decidim-barcelona/blob/672f5a8938d884940899b4304f0a17e25d42d2a0/app/services/sms_gateway.rb[DecidimBarcelona's app/services/sms_gateway.rb]. Your final implementation will depend on how your SMS provider works.

Another example which also utilizes the `context` argument can be found from the Twilio SMS integration available at:
https://github.com/Pipeline-to-Power/decidim-module-ptp/blob/aa82286d91d404e83ea16a55d281a1b049bbaca2/decidim-sms-twilio/lib/decidim/sms/twilio/gateway.rb