Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Mini Program to send template messages by Android

Shulou Source: shulou.com Published: 2022-06-01 07:53:23 09月22日 Update

This article mainly explains "Android how to achieve Mini Program to send template messages", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve Mini Program to send template messages by Android".

I. preparatory work

First of all, the message push function is activated on the Wechat public platform, and a message template is added. You can select a template from the template library or create a template. After the template is added, the template ID we will use next.

Accesstoken, formId, and openID are required to send template messages. FormID is the message template ID,openID. We'd better store it in the global variable when we get the user information or when the user logs in.

Second, get the formID.

Add an event to submit the form on the page that needs to trigger the message push. The goal is to get that formID,formID is a necessary parameter for message push.

Pay immediately

The "getFormID" in the above code is the event triggered when the form is submitted.

GetFormID: function (e) {

This.setData ({

FormId: e.detail.formId})}

The above method is to get the formId.

Third, configure the message template parameters and pass them to the background.

Var config = require ('.. / config.js')

Var app = getApp ()

Function pushMsg (formID, access_token) {

Var openId = app.globalData.userInfo.openId

Var messageDemo = {

Touser: openId,//openId

Template_id: 'PjtLeqq-UeF49r5jr88s27HBzBDobijr6QfiwJwIkPg',// template message id

Page: 'pages/index/index',// jumps to the home page when clicking on the details

Form_id: formID,//formID

Data: {/ / the following keyword* is the keyword variable of the set template message

"keyword1": {

"value": "keyword1"

"color": "# 4a4a4a"

}

"keyword2": {

"value": "keyword2"

"color": "# 9b9b9b"

}

"keyword3": {

"value": "keyword3"

"color": "red"

}

}

Color: 'red',// color

Emphasis_keyword: 'keywords that keyword3.DATA'// needs to highlight

}

Wx.request ({

Url: config.service.sendMsgUrl

Data: {value: messageDemo, access_token: access_token}

Method: 'POST'

Success: function (res) {

Console.log ("push msg")

Console.log (res)

}

Fail: function (err) {

Console.log ("push err")

Console.log (err)

}

});

}

Module.exports = {pushMsg: pushMsg}

Fourth, push messages.

Const request = require ('.. / tools/ih_request')

Var conf = require ('.. / config.js')

Module.exports = async (ctx, next) = > {

Var body = ctx.request.body.value

Await request.postJson ({

Url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + ctx.request.body.access_token

Body: body

Success: function (res) {

Ctx.body = {

Result: res

}

Console.log ('res=',res)

}

Error: function (err) {

Ctx.body = {

Result: err

}

Console.log (err)

}

});}

Ih_request.js

Const request = require ('request')

Var ih_request = {}

Module.exports = ih_request

Ih_request.postJson = async function (option) {

Var res = await request ({

Url: option.url

Method: 'post'

Headers: {

'content-type': 'application/json'

}

Body: JSON.stringify (option.body)

}, function (err, res, body) {

Res? Option.success (body): option.error (res.msg)

Console.log ('MSGresult=', body)

});

} at this point, I believe you have a deeper understanding of "how to achieve Mini Program to send template messages by Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Templates messages push programs events keys keywords content parameters variables methods users forms learning practical deeper next home page code information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn macOS Redmi Shulou Technology