SMS integration in Slack
Build a Slack slash command that sends SMS through the Routee API via a small middleware app.
Build a custom /sms slash command in Slack that sends text messages through the Routee Messaging API. A small web app receives Slack's POST, validates the input, calls Routee, and replies in the channel.
Command syntax
/sms +359899000000 Message text
What you need
- A Routee account with Application Id and Secret (Get Authenticated)
- A Slack workspace where you can create an app
- A publicly reachable HTTPS URL for your middleware (Slack posts slash-command payloads there)
- A text editor or IDE
How it works
- A user runs
/sms +359899000000 Helloin Slack. - Slack sends an HTTP POST to your app's Request URL.
- Your app validates Slack's verification token and parses phone number + message text.
- Your app obtains a Routee Bearer token and calls
POST https://connect.routee.net/sms. - Your app posts a success or error message back to Slack via the
response_url.
Routee setup
- Register at go.routee.net.
- Create a Connect application and copy Application Id and Application Secret from Applications.
- See Get Authenticated for the OAuth token exchange.
Slack setup
Create a workspace and app
- Create a workspace at slack.com if you don't have one.
- Open api.slack.com/apps → Create New App → choose your workspace.
Add the slash command
- In your app, go to Features → Slash Commands → Create New Command.
- Set Command to
/sms. - Set Request URL to your deployed middleware URL.
- Set Usage Hint to
+359899000000 Message text. - Save and install the app to your workspace.
Code examples
Each example follows the same flow: validate Slack token → parse input → authenticate with Routee → send SMS → reply in Slack.
// Config
private String slackVerificationToken = "your-slack-verification-token";
private String slackCommandInputRegex = "^(\\+[0-9]{12})\\s(.*)$";
private String routeeAppId = "your-routee-application-id";
private String routeeAppSecret = "your-routee-application-secret";
private String routeeAuthenticate(String routeeAppId, String routeeAppSecret) throws IOException {
Base64.Encoder encoder = Base64.getEncoder();
String token = encoder.encodeToString((routeeAppId + ":" + routeeAppSecret).getBytes());
URL url = new URL("https://auth.routee.net/oauth/token");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + token);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes("grant_type=client_credentials");
}
StringBuilder response = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) response.append(line);
}
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
return (String) jsonObject.get("access_token");
}
private boolean sendSmsMessage(String authToken, String message, String sender, String receiver) throws IOException {
if (sender.length() > 11) sender = sender.substring(0, 11);
String requestBody = "{\"body\":\"" + message + "\", \"to\":\"" + receiver + "\", \"from\":\"" + sender + "\"}";
URL url = new URL("https://connect.routee.net/sms");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Bearer " + authToken);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes(requestBody);
}
return connection.getResponseCode() == 200;
}slackVerificationToken = 'your-slack-verification-token'
slackCommandInputRegex = r'^(\+[0-9]{12})\s(.*)$'
routeeAppId = 'your-routee-application-id'
routeeAppSecret = 'your-routee-application-secret'
def routee_authenticate(app_id, app_secret):
token = base64.b64encode(f"{app_id}:{app_secret}".encode()).decode()
r = requests.post(
"https://auth.routee.net/oauth/token",
data="grant_type=client_credentials",
headers={"Content-Type": "application/x-www-form-urlencoded", "Authorization": f"Basic {token}"},
)
return r.json()["access_token"]
def send_sms(auth_token, message, sender, receiver):
r = requests.post(
"https://connect.routee.net/sms",
json={"body": message, "from": sender[:11], "to": receiver},
headers={"Content-Type": "application/json", "Authorization": f"Bearer {auth_token}"},
)
return r.status_code == 200$slackVerificationToken = 'your-slack-verification-token';
$slackCommandInputRegex = '/^(\+[0-9]{12})\s(.*)$/';
$routeeAppId = 'your-routee-application-id';
$routeeAppSecret = 'your-routee-application-secret';
function routeeAuthenticate($routeeAppId, $routeeAppSecret) {
$curl = curl_init();
$token = base64_encode($routeeAppId . ':' . $routeeAppSecret);
curl_setopt_array($curl, [
CURLOPT_URL => 'https://auth.routee.net/oauth/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => [
"authorization: Basic $token",
'content-type: application/x-www-form-urlencoded',
],
]);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
return $response['access_token'];
}
function sendSmsMessage($authToken, $message, $sender, $receiver) {
$requestBody = json_encode([
'body' => $message,
'to' => $receiver,
'from' => substr($sender, 0, 11),
]);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://connect.routee.net/sms',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $requestBody,
CURLOPT_HTTPHEADER => [
"authorization: Bearer $authToken",
'content-type: application/json',
],
]);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $code === 200;
}const slackVerificationToken = 'your-slack-verification-token';
const slackCommandInputRegex = /^(\+[0-9]{12})\s(.*)$/;
const routeeAppId = 'your-routee-application-id';
const routeeAppSecret = 'your-routee-application-secret';
function routeeAuthenticate(appId, appSecret) {
const basicHash = Buffer.from(`${appId}:${appSecret}`).toString('base64');
return fetch('https://auth.routee.net/oauth/token', {
method: 'POST',
headers: {
authorization: `Basic ${basicHash}`,
'content-type': 'application/x-www-form-urlencoded',
},
body: 'grant_type=client_credentials',
}).then((r) => r.json()).then((d) => d.access_token);
}
function sendSmsMessage(authToken, message, sender, receiver) {
return fetch('https://connect.routee.net/sms', {
method: 'POST',
headers: {
authorization: `Bearer ${authToken}`,
'content-type': 'application/json',
},
body: JSON.stringify({ body: message, to: receiver, from: sender.slice(0, 11) }),
}).then((r) => r.status === 200);
}
Related
Updated 6 days ago

