阿里云的短信服务 (SMS) 允许您使用 REST API 向 200 多个国家和地区发送短信。
这样可以轻松发送:
在本教程中,您将学习如何与 API 通信并直接从 Node.js 应用发送文本消息。
要完成本教程,您需要一个阿里云账号。您可以免费试用阿里巴巴。
创建帐户时,请务必选择一个,因为短消息服务对单个帐户不可用。如果您已经拥有个人帐户,则必须先升级,然后才能继续学习本教程。enterprise account
在开始发送消息之前,您首先必须:
accessKeyId
accessKeySecret
阿里云提供了一个 Node.Js API 客户端,您可以使用该客户端与系统进行交互。
本部分介绍如何安装它。要继续操作,请确保您已在系统上安装了 Node.js。
您将首先创建一个新的 Node.Js 应用程序,然后安装访问 REST API 所需的客户端。如果你已有应用,请直接转到本部分的最后一步。
打开 shell/命令提示符,然后:
1. 使用以下命令为您的应用程序创建新目录:
mkdir sample-app
2.然后导航到它。
cd sample-app
3. 使用以下命令初始化应用程序。它将引导您完成生成文件所需的一系列步骤。现在,只需按 接受默认选项。package.json
enter
npm init
4. 最后一步是使用以下命令安装阿里云 API 客户端:
npm install @alicloud/pop-core
客户端允许您与帐户中激活的任何云服务进行通信。它使用您的 API 访问密钥远程对您进行身份验证。你将使用它直接从你的应用发送短信。
发送消息时,可以使用预先存在的模板,也可以直接指定要发送的消息,除非将其发送到中国大陆。在这种情况下,您必须使用预先存在的模板。
消息模板还必须经过阿里云团队的批准,然后才能使用它们发送短信。
打开短信控制台,然后导航到使用左侧导航栏上的导航链接。Go Globe
单击该按钮,然后按照说明创建模板。New Content
您可以在内容中指定参数表达式。每当您使用模板时,它们将被您提供的任何参数值替换。
下面是您可以使用的示例 SMS 内容:
Hello, ${code} is your verification code.
创建模板后,复制 .它用于在您想要发送短信时标识模板。Content Code
若要使用新模板发送消息,请在项目目录中创建一个新文件,然后添加以下代码:index.js
//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 and . Replace them with your API access key details.<accessKeyId>
<accessSecret>
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>
<contentCode>
<fromName>
<templateParam>
{'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
SendMessageWithTemplate
client.request('SendMessageToGlobe', params, requestOption).then((result) => {
console.log(result);
}, (ex) => {
console.log(ex);
})
以下是完整代码:
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);
})
请记住替换为要发送的实际消息。<message>
现在您已经按照上述过程操作,您可以直接从Node.Js应用程序发送短信。有了这个,您可以:
阿里云短信服务允许您免费发送100条短信。用尽它们后,您将不得不购买新的SMS软件包。