WeChat Mini Programs push messages to Official Accounts.
Published: 2024-07-15
The interface for sending messages is: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
The component used by the program to guide users to follow the Official Account is:
<official-account></official-account>
Account Preparation:
1. Register a WeChat Mini Program account and complete verification
2. Register a WeChat Official Account and complete verification
3. Register on WeChat Open Platform and complete verification
▲ Settings & Development —> Official Account Settings —> Account Details —> Set Service Category
▲ Advertising & Services —> Template Message —> Add Template from Template Library
▲ Settings & Development —> Basic Configuration —> Set IP address (the IP address of the device that will call the message-sending interface, and the IP address of the server)
4. Associate the Official Account and Mini Program with each other and bind them both under the same WeChat Open Platform
5. To obtain the Mini Program user's oppenid and UnionId, refer to this article: https://zhuanlan.zhihu.com/p/603503623
I am using the wx.login + code2Session method.
1)
wx.login({
success (res) {
if (res.code) {
// Get code
} else {
console.log('Login failed! ' + res.errMsg)
}
}})2)https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
The backend database should store the Mini Program's openid and the Open Platform's UnionId, and the database will save the data.
6. Obtain the access_token of the WeChat Official Account:
Address: https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
7. Obtain the list of user openids of the WeChat Official Account:
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
8. Call the template interface for sending Official Account messages:
Be sure to use https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN,
The other one has been deprecated; for details, see: https://developers.weixin.qq.com/community/develop/doc/000ae8d6348af08e7030bc2546bc01?blockType=1
