One day, a boy named Xiao Ming, want to give a girl he likes for a long time to send a paragraph of anonymous confession message.
The things xiao Ming needs to prepare are:
- After the installation is complete, run the Node -v command on the CLI to view the version of node.js
- Editor Vscode or WebStorm author here uses Vscode
- Tencent Cloud Account Tencent Cloud
- Xiaoming needs to use any of the following credentials to create the SMS signature
- Patience + perseverance
- Log in to Tencent Cloud, title bar => Product => Communication Service => SMS => Apply now
- Add an App a. Create an App B. Name, Type, And Introduction of the App Fill in the Notice as required: You need to remember your SDK AppID and App Key, which will be used in the following sections
- Application configuration => Domestic SMS
- Domestic SMS => SMS Content Configuration => (Create signature && Create body Template)
- Signature: a. Signature content: the signature type should be the same as that specified below. For example, if my public account is “Global Science channel”, fill in “Global Science channel” here. B. Signature type/certificate: Upload materials as prompted (no more than 5M)
- Template: a. Template name: fill in as required, such as “test template” B. TEXT message content: fill in as prompted, the author here fill in the test content is “in heaven is willing to be a pair of birds, in earth is willing to be interconnected branches.” (templates do not allow special symbols such as: ^_^)
NOTICE: You need to remember your SMS signature ID and SMS body ID, which are used below
Tencent cloud SMS can send 100 free messages. You need to renew the fee if you exceed the limit
- Create a folder and run the following command on the CLI
npm init // Generate package.json file
Copy the code
npm install qcloudsms_js --save-dev // Install Tencent cloud SMS SDK and save the information to package.json
Copy the code
- Create a new JS file named index.js with the following contents
// Introduce the Tencent SDK module
let QcloudSms = require("qcloudsms_js");
// SMS SDK AppID
let appid = "* * *"; // *** the SDK AppID you applied for
// SMS app SDK Appkey
let appkey = "* * *"; // *** the SDK AppKey you applied for
// Enter the phone number for receiving SMS messages
let phoneNumbers = ["* * *"]; // This is an array, you can fill in multiple phone numbers to achieve group SMS
// ID of the SMS template
let templateId = ***; // There is no ""! Bear in mind that
/ / signature
let smsSign = "* * *"; // Here is the SMS signature name for your application
// instantiate QcloudSms
let qcloudsms = QcloudSms(appid, appkey);
// Single SMS notification
let ssender = qcloudsms.SmsSingleSender();
let params = [];If {1}{2} is in the template, fill in [" I am variable 1"," I am variable 2"]
ssender.sendWithParam(86, phoneNumbers[0], templateId,
params, smsSign, ""."", callback); // If the signature parameter is not provided or empty, the default signature is used to send SMS messages
// Set up the request callback processing
function callback(err, res, resData) {
if (err) {
console.log("err: ", err);
}
else {
console.log("request data: ", res.req);
console.log("response data: ", resData); }}Copy the code
7. Run the following command
node index.js
Copy the code
If all goes well, the response shown below appears
8. If you want to receive the status of SMS receipt and SMS reply, you need to contact Tencent cloud SMS technical support to open, set the callback URL on the console, and add the following content in index.js
let beginTime = 1535032882; // Start time (Unix timestamp)
let endTime = 1535209822; // End time (Unix timestamp)
let maxNum = 10; // The maximum amount of a single pull
let mspuller = qcloudsms.SmsMobileStatusPuller();
// Pull the SMS receipt
mspuller.pullCallback("86", phoneNumbers[0], beginTime, endTime, maxNum, callback);
// pull the reply
mspuller.pullReply("86", phoneNumbers[0], beginTime, endTime, maxNum, callback);
Copy the code
Run in the CLI
node index.js
Copy the code
Normally, it will return a response message.
9. Conclusion: code word is not easy, thank you so beautiful/handsome you still like it once. ^_^. Feel free to comment below.