以下例子是调用小程序官方的API,如何调用API来进行对内容的安全检测:
第一步:新建一个文件名为msgCheck的Node.js的云函数,安装相关依赖(wx-server-sdk、got)上传并部署,在该目录下的index.js文件编辑代码如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
const got =require('got')
let appid ='wx3879622dfa8bc582'
let secret ='4428b0913c211f136c47127ccf444a7a'
let msgCheckUrl ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' //请求接口的链接
let tokenUrl ='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret //API入口凭证
cloud.init()//初始化
// 云函数入口函数
exports.main = async (event, context) => {
let tokenResponse=await got(tokenUrl)
let token = JSON.parse(tokenResponse.body).access_token
let checkResponse= await got(msgCheckUrl+token,{
body:JSON.stringify({
content:event.text
})