Share the SDK of cloud function short message platform developed by TCB router

Posted by fahad on Wed, 04 Dec 2019 09:30:31 +0100

In the last article, we shared how to use the cloud function to develop hazelnut SMS( http://smsow.zhenzikj.com)SDK, due to the limitation of wechat on the number of unpaid cloud functions, this method has defects. After improvement, TCB router is used as the route, so only one cloud function needs to be integrated
Download sdk and demo: http://smsow.zhenzikj.com/sdkdownload/weixinmp_yun2.html

At present, the SDK includes three functions: send (send SMS), balance (query balance), findsmsbymessageid (query single SMS)

SDK source code

// Cloud function entry file
const cloud = require('wx-server-sdk')
const TcbRouter = require('tcb-router')
const rq = require('request')
const baseUrl = 'https://smsdeveloper.zhenzikj.com'

cloud.init()

// Cloud function entry function
exports.main = async (event, context) => {
  const app = new TcbRouter({ event });

  app.router('send', async (ctx) => {
    ctx.body = new Promise(resolve => {
      rq({
        url: baseUrl + '/sms/send.html',
        method: "POST",
        json: true,
        form: {
          apiUrl: event.apiUrl,
          appId: event.appId,
          appSecret: event.appSecret,
          message: event.message,
          number: event.number,
          messageId: event.messageId,
        }
      }, function (error, response, body) {
        resolve({ body: body, error: error })
      });
      // setTimeout(() => {
      //   resolve('male');
      // }, 500);
    });
  });
  app.router('balance', async (ctx) => {
    ctx.body = new Promise(resolve => {
      rq({
        url: baseUrl + '/sms/balance.html',
        method: "POST",
        json: true,
        form: {
          apiUrl: event.apiUrl,
          appId: event.appId,
          appSecret: event.appSecret
        }
      }, function (error, response, body) {
        resolve({ body: body, error: error })
      });
    });
  });
  app.router('findSmsByMessageId', async (ctx) => {
    ctx.body = new Promise(resolve => {
      rq({
        url: baseUrl + '/sms/findSmsByMessageId.html',
        method: "POST",
        json: true,
        form: {
          apiUrl: event.apiUrl,
          appId: event.appId,
          appSecret: event.appSecret,
          messageId: event.messageId
        }
      }, function (error, response, body) {
        resolve({ body: body, error: error })
      });
    });
  });

  return app.serve();
}

How to use SDK

//index.js
const app = getApp()

Page({
  data: {

  },

  onLoad: function() {

  },

  // Sending SMS
  send: function () {
    wx.cloud.callFunction({
      name: 'zhenzisms',
      data: {
        $url: 'send',
        apiUrl: 'https://sms_developer.zhenzikj.com',
        appId: 'Your appId',
        appSecret: 'Your appSecret',
        message: 'Your verification code is:3333',
        number: '15811111111',
        messageId: ''
      }
    }).then((res) => {
      console.log(res.result.body);
    }).catch((e) => {
      //console.log(e);
    });
  },
  // Check the balance
  balance: function () {
    wx.cloud.callFunction({
      name: 'zhenzisms',
      data: {
        $url: 'balance',
        apiUrl: 'https://sms_developer.zhenzikj.com',
        appId: 'Your appId',
        appSecret: 'Your appSecret'
      }
    }).then((res) => {
      console.log(res.result.body);
    }).catch((e) => {
      //console.log(e);
    });

  },
  // Query single information
  findSmsByMessageId: function () {
    wx.cloud.callFunction({
      name: 'zhenzisms',
      data: {
        $url: 'findSmsByMessageId',
        apiUrl: 'https://sms_developer.zhenzikj.com',
        appId: 'Your appId',
        appSecret: 'Your appSecret',
        messageId: 'aaaabbbbba'
      }
    }).then((res) => {
      console.log(res.result.body);
    }).catch((e) => {
      //console.log(e);
    });

  }
})

Topics: Mobile SDK JSON