Send Messages from Node.js App with Alibaba Cloud SMS

Alibaba Cloud
5 min readAug 12, 2019

--

By Brian Mutende, Alibaba Cloud Community Blog author.

Alibaba Cloud’s Short Message Service (SMS) allows you to send text messages to over 200 countries and regions using its REST API.

This makes it easy to send:

  • E-commerce transaction notifications.
  • Phone number verification codes.
  • Marketing messages.
  • OTP (One-time-passwords) e.t.c

In this tutorial, you will learn how to communicate with the API and send text messages straight from your Node.js app.

Before You Begin

To complete this tutorial, you will need an Alibaba Cloud account. You can try Alibaba for free.

When creating an account, be sure to select an enterprise account since the short message service is not available to individual accounts. If you already have an individual account, you will have to upgrade first before continuing with this tutorial.

Setting up

Before you can start sending messages, you first have to:

Installing the API Client

Alibaba cloud provides a Node.Js API client that you can use to interact with the system.

This section shows you how to install it. To follow along, make sure that you have installed Node.js on your system.

You will first create a new Node.Js App then install the client required to access the REST API. If you already have an app, go straight to the last step in this section.

Open your shell/command prompt then:

1. Create a new directory for your app using the command:

mkdir sample-app

2. Then navigate into it.

cd sample-app

3. Initialize the application using the command below. It will walk you through a series of steps required to generate a package.json file. For now, just accept the default options by pressing enter.

npm init

4. The last step is to install the Alibaba Cloud API client with the following command:

npm install @alicloud/pop-core

The client allows you to communicate with any cloud service that is activated in your account. It uses your API access key to authenticate you remotely. You will use it to send text messages straight from your app.

When sending a message, you can either use a pre-existing template or directly specify the message that you wish to send except when sending it to Mainland China. In that case, you will have to use a pre-existing template.

Message templates will also have to be approved by the Alibaba Cloud team before you can use them to send text messages.

Sending a Message Using a Message Template

Open the SMS console and then navigate to Go Globe using the navigation link on the left navigation bar.

Click on the New Content button and follow the instructions to create your template.

You can specify parameter expressions in the content. They will be replaced by any parameter values you supply whenever you use the template.

Here is a sample SMS content that you can use:

Hello, ${code} is your verification code.

After creating the template, copy the Content Code. It is used to identify the template whenever you want to send a text message.

To send messages using your new template, create a new index.js file in your project directory and add the following code:

//Step 1
const Core = require('@alicloud/pop-core');
//Step 2
var client = new Core({
accessKeyId: '<accessKeyId>',
accessKeySecret: '<accessSecret>',
endpoint: 'https://sms-intl.ap-southeast-1.aliyuncs.com',
apiVersion: '2018-05-01'
});
//Step 3
var params = {
"RegionId": "ap-southeast-1",
"To": "<phoneNumber>",
"TemplateCode": "<contentCode>",
"From": "<fromName>",
"TemplateParam": "<templateParam>", //Optional
}
var requestOption = {
method: 'POST'
};
//Step 4
client.request('SendMessageWithTemplate', params, requestOption).then((result) => {
console.log(result);
}, (ex) => {
console.log(ex);
})

Here is an explanation of what the code does:

1. Step 1 imports the SDK client which is used to interact with the API.

2. Step 2 sets up the client. The only thing you should change is the <accessKeyId> and <accessSecret>. Replace them with your API access key details.

3. Step 3 sets the parameters used to configure the message. Remember to replace the following parameters with the information you intend to use:

  • Replace <phoneNumber> with the phone number of the person receiving the text. The format of the number is Country Code + Phone Number.
  • Replace <contentCode> with the content code you received from the previous step. This code identifies the template that you intend to use.
  • Replace <fromName> with the sender id you would like to use. For example, Company Name.
  • Replace <templateParam> with a valid JSON string containing the values of any parameters you set in your template. For example, {"code": "12345"}.

4. Step 4 sends the message and returns a JavaScript promise. You can provide success and error callbacks to be invoked after the request is complete.

Sending a Message Directly

This method allows you to send a text message directly. You do not have to specify a template code.

It uses the same code as the previous step, except for a few changes:

  • First of all, you specify the message instead of a template code as shown:
var params = {
"RegionId": "ap-southeast-1",
"To": "<phoneNumber>",
"Message": "<message>",
"From": "<fromName>"
}
  • You will also use SendMessageToGlobe as the request action instead of SendMessageWithTemplate. This tells the client that we would like to send the message directly instead of using a preconfigured template.
client.request('SendMessageToGlobe', params, requestOption).then((result) => {
console.log(result);
}, (ex) => {
console.log(ex);
})

Here is the full code:

const Core = require('@alicloud/pop-core');
var client = new Core({
accessKeyId: '<accessKeyId>',
accessKeySecret: '<accessSecret>',
endpoint: 'https://sms-intl.ap-southeast-1.aliyuncs.com',
apiVersion: '2018-05-01'
});
var params = {
"RegionId": "ap-southeast-1",
"To": "<phoneNumber>",
"Message": "<message>",
"From": "<fromName>"
}
var requestOption = {
method: 'POST'
};
client.request('SendMessageToGlobe', params, requestOption).then((result) => {
console.log(result);
}, (ex) => {
console.log(ex);
})

Remember to replace <message> with the actual message that you wish to send.

Conclusion

Now that you have followed the above procedure, you can send text messages straight from your Node.Js app. With this, you can:

  • Enable Two-Factor authentication on your website or app.
  • Send OTP (One Time Passwords) to your users.
  • Send verification codes when a user adds a phone number to their account.
  • Send promotional messages.
  • And more!!!

Alibaba Cloud SMS service allows you to send 100 free SMS. After exhausting them, you will have to buy a new SMS package.

Original Source

--

--

Alibaba Cloud
Alibaba Cloud

Written by Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

No responses yet