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.
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.
The next step is to send SMS from your new 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 C#, Node.js, 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.
When a text message is sent to your 46elks number this message (plus some metadata) will be forwarded to your code. This code needs to deal with the incoming 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 go 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. When you have done this 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 makes available to you about the text message you just sent.
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 in turn 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 any text.
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. You need 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. As you’ve seen in RequestBin the text of an incoming SMS is available in the POST varialbe message. In the PHP example below we are using message 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.
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.