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:
In this tutorial, you will learn how to communicate with the API and send text messages straight from your Node.js app.
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.
Before you can start sending messages, you first have to:
accessKeyId
and accessKeySecret
pair. It is used to authenticate your API requests.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.
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:
<phoneNumber>
with the phone number of the person receiving the text. The format of the number is Country Code + Phone Number.<contentCode>
with the content code you received from the previous step. This code identifies the template that you intend to use.<fromName>
with the sender id you would like to use. For example, Company Name.<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.
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:
var params = {
"RegionId": "ap-southeast-1",
"To": "<phoneNumber>",
"Message": "<message>",
"From": "<fromName>"
}
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.
Now that you have followed the above procedure, you can send text messages straight from your Node.Js app. With this, you can:
Alibaba Cloud SMS service allows you to send 100 free SMS. After exhausting them, you will have to buy a new SMS package.
2,599 posts | 762 followers
Followhyj1991 - June 20, 2019
Alibaba Clouder - November 25, 2019
Anna Chat APP - October 14, 2024
Anna Chat APP - November 4, 2024
Anna Chat APP - September 3, 2024
Alibaba Clouder - February 12, 2019
2,599 posts | 762 followers
FollowShort Message Service (SMS) helps enterprises worldwide build channels to reach their customers with user-friendly, efficient, and intelligent communication capabilities.
Learn MoreElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAPI Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.
Learn MoreMore Posts by Alibaba Clouder