小程序客服怎么设置自动回复,小程序自动回复功能设置教程( 三 )


const accessToken = await this._getAccessToken();

/*
* 先判断配置回复的消息类型是不是image类型
* 如果是 则需要先通过 新增素材接口 上传图片文件获得 media_id
*/

let media_id = \'\';
if(options.type === \'image\'){
//获取图片地址(相对路径)
let url = options.url;
const file = fs.createReadStream(url);

//调用微信 uploadTempMedia接口 具体实现见 service.js
const mediaResult = await this.callService(\'wxApplet.uploadTempMedia\',
{
access_token: accessToken,
type: \'image\'
},
{
media: file
}
);

if(mediaResult.status === 0){
media_id = mediaResult.data.media_id;
}else {
//如果图片id获取失败 则按默认处理
options = keyWordObj.default;
}
}

//回复信息给用户
const sendMsgResult = await this.callService(\'wxApplet.sendMessageToCustomer\',
{
access_token: accessToken,
touser: msgJson.FromUserName,
msgtype: options.type || \'text\',
text: {
content: options.description || \'\',
},
link: options.type === \"link\" ?
{
title: options.title,
description: options.description,
url: options.url,
thumb_url: options.thumb_url
}
:
{},
image: {
media_id
}
}
);

}
复制代码
service.js
const request = require(\'request\');
/*
* 获取CMS客服关键词回复配置
* 这个接口只是为了回去CMS配置的字段回复关键字配置 返回的data数据结构如下
*/
async contact(){
return {
data: {
\"1\": {
\"type\": \"link\",
\"title\": \"点击下载[****]APP\",
\"description\": \"注册领取领***元注册红包礼\",
\"url\": \"https://m.renrendai.com/mo/***.html\",
\"thumb_url\": \"https://m.we.com/***/test.png\"
},
\"2\": {
\"url\": \"http://m.renrendai.com/cms/****/test.jpg\",
\"type\": \"image\"
},
\"3\": {
\"url\": \"/cms/***/test02.png\",
\"type\": \"image\"
},
\"default\": {
\"type\": \"text\",
\"description\": \"再见\"
}
}
}
}
/*
* 把媒体文件上传到微信服务器 。目前仅支持图片 。用于发送客服消息或被动回复用户消息 。
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.uploadTempMedia.html
*/

async uploadTempMedia(data,formData){
const url = `https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${data.access_token}&type=${data.type}`;
return new Promise((resolve, reject) => {
request.post({url, formData: formData}, (err, response, body) => {
try{
const out = JSON.parse(body);
let result = {
data: out,
status: 0,
message: \"ok\"
}

return resolve(result);

}catch(err){
return reject({
status: -1,
message: err.message
});
}
});
}
}

/*
* 发送客服消息给用户
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html
*/

async sendMessageToCustomer(data){
const url = `https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${data.access_token}`;
return new Promise((resolve, reject) => {
request.post({url, data}, (err, response, body) => {
...
});
}
}

复制代码
WXDecryptContact.js
消息加密解密文档
const crypto = require(\'crypto\'); // 加密模块
const decodePKCS7 = function (buff) {
let pad = buff[buff.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return buff.slice(0, buff.length - pad);
};
// 微信转发客服消息解密
const decryptContact = (key, iv, crypted) => {
const aesCipher = crypto.createDecipheriv(\'aes-256-cbc\', key, iv);
aesCipher.setAutoPadding(false);
let decipheredBuff = Buffer.concat([aesCipher.update(crypted, \'base64\'), aesCipher.final()]);

推荐阅读