TUTORIAL

Receive and respond to SMS

With 46elks you can send text messages from a phone number as well as receive, process and respond to text messages sent to that number. To do this you need a 46elks account, have a phone number connected to 46elks and some code that deals with incoming SMS and responds to them.

🇸🇪 Är du i Sverige så rekommenderar vi att du läser den svenska versionen av denna guide.

1. Your 46elks phone number

To be able to receive and respond to SMS with the 46elks API you will need a 46elks phone number. The easiest way to get such a number is to login to the dashboard, go to the Numbers page and add a number with SMS enabled to your 46elks account. This is your number that you can use to send and receive text messages.

Note that the 46elks number has a minor monthly cost. See the pricing page for exact prices. If you prefer you can of course add and manage numbers via the API instead of via the dashboard.

2. Send SMS from your number

Now you have your own 46elks phone number and the next step is to send SMS from this number. Use your number as the sender when you send SMS. Test this by going through the tutorial to send SMS and switch the from to your new 46elks phone number. This tutorial is available for PHP, Ruby and Python.

Tips: When you use a phone number in from when sending an SMS you need to specify this number with country code and without spaces and dashes. For example use +46709000000 instead of 0709 - 000000 or +46 709 000000.

3. Test receiving and responding to SMS

When an SMS is sent to your 46elks number 46elks will send this message (plus some metadata) to your code that needs to deal with the incoming text message. Technically 46elks does a POST request to a URL you have defined (i.e., a callback to your webhook).

Let’s test this using the free tool RequestBin which will provide us with a URL to send the SMS data to. Go to RequestB.in and click on Create a RequestBin. Copy the Bin URL. Open a new tab in your web browser and to the Numbers page in the 46elks dashboard. Paste the Bin URL into your phone number sms_url and click Save. What you have done is telling 46elks that when your phone number receives an SMS then 46elks should send data to your RequestBin.

Next step in the test is to send an SMS from your phone to your 46elks number. Then go to the tab in your web browser where you have Request Bin open and reload the page (or go to your Bin URL?inspect if you already closed the tab). Now you can see all the data 46elks have send when you sent the text message to your 46elks number.

You will also receive a response to the text message you sent to your 46elks number. This is RequestBin sending back an OK to 46elks and 46elks forwarding this response as an SMS to your phone. That is how you respond to an SMS - just return a text when you have received a POST request form 46elks. If you do not want to respond to an SMS you just don’t return a text.

4. Receive and respond to an SMS with your own code

Using RequestBin you have now dealt with incoming text messages and responded to them. Time to do the same thing but with your own code instead. What is required os to write some code that receives a POST request from 46elks, make this code accessible over the internet and tell 46elks at what URL this code is available.

Let’s do this by using the simplest code possible to just respond to incoming SMS. In the POST request 46elks makes the content of the incoming SMS is sent in the POST parameter message. In the PHP example below we are using this POST parameter to respond to an SMS with a thank you.

<?php 
  echo 'Thank you for sending me the message: ' . $_POST['message'];

Copy this code into a file named respond.php and upload it to a web server that can execute PHP (most web servers can). This PHP file must be accessible over the internet. Specify the URL to the PHP fil as sms_url for your 46elks phone number in the dashboard.

If you now send an SMS from your phone to your 46elks number you will receive a thank you message. Congratulations, you have just received and responded to an SMS using your own code.

5. Next step

You can now send, receive and respond to text message using your own 46elks number. Use this to automate communication - make customers confirm that they will come to an appointment, get employees to confirm that they have recived an alarm message , etc.

When 46elks send a POST to your sms_url you will receive more than just the POST parameter message - from (the senders phone numer or name) and to (the receivers phone numer) are also included. Use this information to keep track of who sent a message and to tailor what to do when an SMS is received.

Since you now know how to deal with SMS you can just as easily send and receive MMS and automate voice calls. For examples of this and much more check out the code examples on GitHub.

Contact us if you have any questions and we will help you solve your problems.