actionmessage

ActionMailer heavily-inspired gem to handle SMS/Text Messages


License
MIT
Install
gem install actionmessage -v 0.0.13

Documentation

ActionMessage

ActionMessage is heavily-inspired on ActionMailer. It's a gem for sending SMS/Text messages like we do for sending e-mails on ActionMailer. Pull requests are more than welcome!

Gem Version Build Status codecov Code Climate

Setup

Install it using bundler:

# Gemfile
gem 'actionmessage'

If you're using Rails, place this on your environment file or application.rb

require 'action_message/railtie'

config.action_message = {
  from: "number to send from in international format, e.g.: +11231231234", 
  adapter: { 
    name: :twilio,
    credentials: {
      account_sid: 'MY TWILIO ACCOUNT SID'.freeze,
      auth_token: 'MY AUTH TOKEN'.freeze
    }
  }
}

Usage

In order to generate your message class, you can either place this code under app/messages/welcome_message.rb or just use our generators by running the following command: rails g message Welcome send_welcome_sms

class WelcomeMessage < ActionMessage::Base
  def send_welcome_sms(name, phone_number_to_send_message)
    @name = name
    sms(to: phone_number_to_send_message)
  end
end

Define your views under your view path, such as: app/views/welcome_message/send_welcome_sms.text.erb

Welcome, <%= @name %>!

And to send is really simple!

name = 'John Doe'
phone = '+11231231234'

# To send right away:
WelcomeMessage.send_welcome_sms(name, phone).deliver_now

# To send through a background job
WelcomeMessage.send_welcome_sms(name, phone).deliver_later

Interceptors

In order to prevent sending messages to a specific number or containing any specific text on it's body you can use Interceptors:

# You can use strings (exact comparison)
ActionMessage::Interceptor.register(to: 'number I want to prevent sending messages')

# Regular expressions
ActionMessage::Interceptor.register(body: /something innapropriate/i)

# Pass Multiple arguments on the same call
ActionMessage::Interceptor.register(to: '+11231231234', body: /anything/i)