How small micro-channel subscription program to send a message, correct posture, the proposed collection!

Subscribe to news applet beta has been some days, today with the world's best language (PHP), for example, talk about how to send a subscription message.

1, the subscription message

In fact, if the message template used, then switch to subscribe messaging very simple, look at the official documentation will be able to use a little exploration.

But for the first time with the new Moe, it may encounter a variety of pit, so I will talk about the specific implementation process, we experienced proceed immediately to the bottom of the article to view Demo.

Nonetheless, a more detailed description of the parameters still need to see official documents, Portal:

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html

2, ready to work

The first is to get template_id , it is the template ID.

The find suitable selection of templates in the public template gallery applet background, after the template to copy my system assigned to the template ID. If you do not find the right, you need to apply their own templates, 3 - 7 days review period.

Then is a small program  AppId and appsecret , get set way to find developers in the development of small programs function in the background, save ID and secret key.

It should be noted that AppSecret only appears when the first generation, after no longer displayed in clear text, this requires developers to save their own good, if leaked or forget to reset the keys.

As openid obtaining here do not specifically described.

3, authorized to receive

Require the recipient before sending the message is authorized to receive such subscription, or subscription message is not issued, the code is very simple, call the official subscription Interface:

wx.requestSubscribeMessage ({ 
  tmplIds: [ 'template_id'], // here to fill a plurality of template ID, but is not compatible with low micro-channel version only authorize a 
  Success (RES) { 
    the console.log ( 'has been authorized to receive the subscription message' ) 
  } 
})

Pull authorization box is the case, but issued a document before the deadline, developer tools, can not call interface, you can only run on the real machine, helpless.

4, issued subscription message

Under a message calling subscribeMessage.send  , calling into cloud and https call, call the cloud is relatively simple, eliminating the need for access_token acquisition:

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.subscribeMessage.send({
        touser: 'OPENID',
        page: 'index',
        data: {
          name3: {
            value: '我是玖柒后'
          },
          thing4: {
            value: 'Hello World!'
          },
          phrase1: {
            value: "发送成功!"
          },
          date5: {
            value: "发送成功!"
          },
          thing2: {
            value: "1024 身体健康!"
          }
        },
        templateId: 'TEMPLATE_ID'
      })
    console.log(result)
    return result
  } catch (err) {
    console.log(err)
    return err
  }
}

然后在服务器端发起请求,请求地址:

https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=token

在这之前先获取小程序全局唯一后台接口调用凭据(access_token),这就用到了之前保存的 AppId 和 AppSecret。

// 小程序 appID 和 appSecret 获取 token
function getAccessToken($appid, $appsecret)
{
  $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
  $html = file_get_contents($url);
  $output = json_decode($html, true);
  $access_token = $output['access_token'];
  return $access_token;
}

Although it looks a little more complicated, but in fact, as long as a request is sent to the server will be able to obtain and call processing by the server, receive messages and messages about the same template.

PHP API subscription message template complete public concern number, reply back " Subscribe news " get.

Guess you like

Origin www.cnblogs.com/msunh/p/11762573.html