MessageBird 💙 Slack: send incoming SMS to any Slack channel
At MessageBird we, like thousands of other companies, love to use Slack. We use it for our internal communication, to receive (technical) notifications from our production platforms, and — what this blog is about — we use it to get feedback from our clients via SMS.
When you have a Virtual Mobile Number (VMN) (for the readers in the US, it’s called a Long code) you can send an SMS to your clients and ask them for feedback. By replying to your message they can let you know how they feel about your product or service. This way you can get information about the experience from clients from all over the world.
The replies you receive on your Virtual Mobile Number can be forwarded to an email address, to a mobile number, or to a group of numbers. And — in this case — they can also be forwarded to a URL that contains a script that forwards the message to a Slack channel. I used Lumen PHP Framework to create a simple controller that performs this action.
Requirements
What you need to make this happen.
1. A MessageBird account with a dedicated and active Virtual Mobile Number.
2. A Slack account that you’re the admin of.
3. The ability to host a PHP site.
Step 1: Install Lumen and MessageBird API Client
With composer it’s very easy to install Lumen and the MessageBird API PHP client. Create a composer.json file in your working directory as shown below and run composer install on the command line. This will install all the libraries you need.
Configure your webserver so it uses the public directory as document root.
Step 2: Create Controller with forward to Slack action
The code below collects the given data from the message sent to MessageBird Virtual Mobile Number, combines it to an object Slack can handle and pushes it to the API of Slack. Save this code as app/Http/Controllers/MessageBirdController.php:
The example doesn’t contain validation and sanitation to keep the code clear with functionality only.
To make this piece of code reachable to use, copy file below to app/Http/routes.php:
Now this can be used for POST requests at http://your.domain/messagebird/sms-to-slack
Step 3: Configure your Virtual Mobile Number at MessageBird
At the MessageBird Virtual Mobile Number page choose to configure your Virtual Mobile Number and select: If [Always], then [Forward to URL] [POST] and enter the URL of your domain including /messagebird/sms-to-slack (e.g. https://messagebird.example/messagebird/sms-to-slack).
All incoming messages on your Virtual Mobile Number will be forwarded to the website we’ve just installed.
Step 4: Configure the implementation hook at Slack
At the Slack admin page you need to add the Incoming WebHooks integration, by installing it via Integrations > All Services. After adding the integration, add a new Incoming Webhooks integration and choose in which channel you want to show the incoming messages. Copy the Webhook URL and paste it at line 37 at the current placeholder for the Slack hook in the MessageBirdController.php.
Step 5: Test and use to get feedback
Send an SMS message to your Virtual Mobile Number and it will appear in the chosen Slack channel.
Now you can send SMS messages — via our API or our web form — to your (recent) clients to ask for feedback. If you use your Virtual Mobile Number as originator, your clients can reply directly when they receive your message.