Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development SMS sending


May 22, 2021 Mini Program Cloud Development Advanced



Combined with some third-party SMS APIs, using cloud-developed cloud functions can also send SMS verification codes, systems and activity notifications, and so on, taking Tencent Cloud's SMS service as an example. Tencent Cloud has the tencentcloud-sdk-nodejs module for the Node environment, which integrates Tencent Cloud services, and almost all of Tencent's cloud services are integrated into the Developer Tools Suite (SDK) in addition to SMS.

First, the opening of Tencent cloud SMS service

1, the opening of SMS services

Login SMS console, the account here is not limited to the account of small programs, other accounts can also be; If the account has been certified, directly apply for SMS service can be opened.

2, create an app

Creating an app can be used to personalize the management of SMS sending tasks, such as setting different sending frequencies and sending oversized reminders. O pen the App Management - List of Apps in the menu on the left, click Create an app, and the app name can be developed for your little program name, Cloud, for easy differentiation of management. Once created, there will be SDKAppID which will be used later.

3, build signature and body template

Domestic SMS consists of a signature and body, the signature symbol is , the text message content must be signed. So to send a text message, you need to request a SMS signature and body template, both of which have passed the review, you can start sending SMS.

(1) Create a signature

Open the domestic TEXT message in the menu on the left - Signature Management, click Create Signature, and after you create the 签名内容 content will be used later.

  • Purpose of signature: Select self-use (signature is the real name of this account certified company, website, product name, etc.).

  • Signature type: Select Small Program.

  • Signature: Enter the company name or small program name or product name

  • Proof type: select the small program settings page screenshot, and then upload the small program settings page screenshots, you can refer to the case;

Cloud development SMS sending

(2) Create a body template

Open the domestic TEXT message in the left menu - body template management, click Create body template, after creating the template, there will be a ID which will be used later, but also remember the variable position of your template.

  • Template name, suggested with a clear purpose of the name, such as "registration notice", "purchase success feedback" and so on;

  • Type of SMS: Select Normal SMS

  • Text message content: for example, "You are applying for mobile phone registration, verification code is: {1}, {2} minutes valid!" "The {1} {2} here are the variables that you want to pass in in your code, and the code for the variables must start at {1} and be passed in in order when you pass them in

Cloud development SMS sending

3, obtain security credentials

Before using the cloud API, users first need to apply for security credentials (API keys) on the Tencent cloud console, including SecretID and SecretKey. Open the API key management of Tencent Cloud Access Key, click New Key, you can create the key, and once you create it, you can see SecretID and SecretKey both of which will be used later.

API key is an important credential for building Tencent Cloud API requests, using Tencent Cloud API can operate all Tencent cloud resources under your account name, be sure to keep and update regularly, do not share with others or upload to the network.

Cloud development SMS sending

Second, the use of cloud functions to send text messages

Use developer tools to create a new cloud function, such as sms, open package.json in the cloud function directory, add the latest version of the tencentcloud-sdk-nodejs dependency, right-click cloud function directory select to open the input command npm install installation dependency in the terminal:

  1. "dependencies": {
  2. "wx-server-sdk":"latest",
  3. "tencentcloud-sdk-nodejs":"latest"
  4. }

Then create a new config folder under the directory of the cloud function, and create a config folder in the .js, as shown in the following image of the directory structure of the cloud function:

  1. sms // 云函数目录
  2. ├── config //config文件夹
  3. └── config.js //config.js文件
  4. └── index.js
  5. └── config.json
  6. └── package.json

Then enter .js code in theconfig code to fill in ThescretID and SecretKey in the security credentials:

  1. module.exports = {
  2. secretId: 'wxda99ae45313257046',
  3. secretKey: 'josgjwoijgowjgjsogjo',
  4. }

Then enter the following code in .js, the content of the code is more, but basically from Tencent cloud SMS technical documents directly Copy over, we just need to change the corresponding parameters inside, for example

  • req. SmsSdkAppid for Creating SDKAppID

  • req. Sign creates the signature content 签名内容

  • req. TemplateID is the template ID in the create body ID

  • req. TemplateParamSet is a variable in the contents of a template with an array of values, and how many variables are filled in the array

  • req. PhoneNumberSet is the user's mobile phone number and can be filled in for your own test

After modifying the above, you can trigger the cloud function to send a text message to the appropriate mobile phone number:

  1. const cloud = require('wx-server-sdk')
  2. const tencentcloud = require("tencentcloud-sdk-nodejs");
  3. cloud.init({
  4. env: cloud.DYNAMIC_CURRENT_ENV,
  5. })
  6. const config= require("./config/config.js")
  7. const {secretId,secretKey} = config
  8. exports.main = async (event, context) => {
  9. const smsClient = tencentcloud.sms.v20190711.Client;
  10. const models = tencentcloud.sms.v20190711.Models;
  11. const Credential = tencentcloud.common.Credential;
  12. const ClientProfile = tencentcloud.common.ClientProfile;
  13. const HttpProfile = tencentcloud.common.HttpProfile;
  14. let cred = new Credential(secretId,secretKey)
  15. let httpProfile = new HttpProfile();
  16. httpProfile.reqMethod = "POST";
  17. httpProfile.reqTimeout = 30;
  18. httpProfile.endpoint = "sms.tencentcloudapi.com";
  19. let clientProfile = new ClientProfile();
  20. clientProfile.signMethod = "HmacSHA256";
  21. clientProfile.httpProfile = httpProfile;
  22. let client = new smsClient(cred, "ap-guangzhou", clientProfile);
  23. let req = new models.SendSmsRequest();
  24. req.SmsSdkAppid = "1400364657";
  25. req.Sign = "HackWeek";
  26. req.ExtendCode = "";
  27. req.SenderId = "";
  28. req.SessionContext = "";
  29. req.PhoneNumberSet = ["+86185****3"];
  30. req.TemplateID = "597853";
  31. req.TemplateParamSet = ["1234","5"];
  32. client.SendSms(req, function (err, response) {
  33. if (err) {
  34. console.log(err);
  35. return;
  36. }
  37. console.log(response.to_json_string());
  38. });
  39. }