TextMagic is a paid service that allows you to send text/SMS messages to people's cell phone numbers. This article will show you how you can use their API to send text messages from your application. Let's get started.
TextMagic Account Setup
Before we begin, you will need to create an account on the TextMagic website. TextMagic offers a free trial that allows you to develop your application without having to pay for a premium account. Once you have created an account, follow the steps below to get your API password, which we will need for our application.
- Log into TextMagic
- Click 'Services'
- Click 'API'
- Click 'Generate new API password'
- Enter your account password
- Copy down the generated API password
Now let's create our Rails application.
Rails Application Setup
To interface with the TextMagic service, we will need to install the
textmagic gem. To do this, add the following line to your Gemfile:
gem 'textmagic', '~> 0.6'
Now run a bundle install to install the gem.
Great, now let's create a controller called
Messages that will handle the user's input and talk to the TextMagic gem. Run the command below to create the Messages controller.
rails g controller Messages new create
Now let's modify the routes file. Open up your routes file and modify it so that it looks like the code listed below.
Rails.application.routes.draw do resources :messages, only: [:new, :create] root to: "messages#new" end
Great, now let's add some code to our messages controller. Modify your messages controller to look like the code listed below.
class MessagesController < ApplicationController def new end def create require 'textmagic' @phone_number = message_params[:phone_number] @message = message_params[:message] @gateway = TextMagic::API.new('username', 'api_password') @gateway.send message_params[:message], message_params[:phone_number] end private def message_params params.require(:message).permit(:phone_number, :message) end end
The create method is responsible for sending the actual text message. The username is your account username you created during the account creation process and the API password is the one you generated earlier. Note that it's recommend that you store these credentials in environment variables on production and use ENV to retrieve them.
Now let's set up our views. First open up the
new.html.erb view for our
Messages controller and modify it so that it looks like the code listed below.
<h2>Send a Text Message</h2> <%= form_for :message, url: messages_path do |f| %> <%= f.label :phone_number %> <%= f.text_field :phone_number %> <%= f.label :message %> <%= f.text_field :message %> <%= f.submit %> <% end %>
Now let's modify our
create.html.erb for the
Messages controller. Open up the view and modify it so that it looks like the code listed below.
<h2>Message Sent!</h2> <p> <%= "The message to [email protected]_number} containing the text [email protected]}' has been sent successfully!" %> </p> <%= link_to "New Message", new_message_path %>
Now if we start a Rails server using
rails s and navigate to http://localhost:3000 we can send a text message to anyone in the world. Note that we will need to use country codes in order to get TextMagic to utilize our phone number. For a US number that would be something like 15555551234 instead of 5555551234.
That's it! Thanks for reading!