用云函数怎么实现模板消息
发布于 6 年前 作者 xhan 12466 次浏览 来自 问答

现在用小程序云开发需要用到模板消息,研究了模板消息需要用客户端获取tocken,再获取模板消息。

利用云开发如何实现模板消息

4 回复

我之前写了个测试发消息的云函数,希望能帮到你

建议在小程序打开的时候就获取access_token,要不然你会频繁的获取access_token,官方不建议这样

模板消息与统一服务消息教程:https://github.com/TencentCloudBase/mp-book/blob/master/basic-tutorial/message.md

如果需要缓存access_token,可考虑使用云开发的数据库。

//npm   install  request-promise         --安装 request-promise命令
 
const rp = require('request-promise');
 
 
 
 
 
 
 
 
//appid   和秘钥
 
  const appid = 'wxxxxxxxx',
 
     secret = 'xxxxxxxxxxxx';
 
  
 
  const AccessToken_options = {
 
     method: 'GET',
 
     url: 'https://api.weixin.qq.com/cgi-bin/token',
 
     qs: {
 
       appid,
 
       secret,
 
       grant_type:'client_credential'
 
     },
 
     json: true
 
     
 
   };
 
  
 
  //获取AccessToken
 
  const resultValue = await rp(AccessToken_options);
 
  const token = resultValue.access_token;
 
 
 

const  body=

{

  "touser": "OPENID",

  "template_id": "TEMPLATE_ID",

  "page": "index",

  "form_id": "FORMID",

  "data": {

    "keyword1": {

      "value": "339208499"

    },

    "keyword2": {

      "value": "2015年01月05日 12:30"

    },

    "keyword3": {

      "value": "腾讯微信总部"

    },

    "keyword4": {

      "value": "广州市海珠区新港中路397号"

    }

  },

  "emphasis_keyword": "keyword1.DATA"

}




const send= {    

method: 'POST',    

url: ' https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+toke,    

body ,    

json: true   

};

const send_res= await rp(send);

回到顶部