GithubHelp home page GithubHelp logo

actionmessage's Introduction

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 = {
  # sender in international format you can also pass an array and it will
  # shuffle and randomly pick one number.
  from: "+11231231234",

  # adapter information. Right now we only support Twilio.
  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

  # Inline body example, body parameter has preference compared
  # to a text.erb template.
  def welcome_with_inline_body(name, phone_number_to_send_message)
    @name = name
    sms(to: phone_number_to_send_message, body: 'Inline body!')
  end

  # While on development environment, you can use debug: true to
  # prevent sending SMS and spending funds on your Twilio account.
  def welcome_with_debug_mode(name, phone_number_to_send_message)
    @name = name
    sms(to: phone_number_to_send_message, debug: true)
  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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.