使用 Node.js 推送微信消息的几种方法
以下是几种使用 Node.js 向微信推送消息的方法,包括微信公众号模板消息、企业微信应用消息和微信小程序消息推送。
方法一:通过微信公众号模板消息推送(需服务号)
const axios = require('axios');
const crypto = require('crypto');
// 配置信息
const config = {
appId: '你的微信公众号appId',
appSecret: '你的微信公众号appSecret',
templateId: '模板消息ID',
openId: '接收用户的openid'
};
// 获取access_token
async function getAccessToken() {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${config.appId}&secret=${config.appSecret}`;
const response = await axios.get(url);
return response.data.access_token;
}
// 发送模板消息
async function sendTemplateMessage() {
try {
const accessToken = await getAccessToken();
const url = `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${accessToken}`;
const data = {
touser: config.openId,
template_id: config.templateId,
url: 'https://example.com', // 可选,点击跳转链接
data: {
first: {
value: '您好,您有一条新消息',
color: '#173177'
},
keyword1: {
value: 'Node.js推送',
color: '#173177'
},
keyword2: {
value: new Date().toLocaleString(),
color: '#173177'
},
remark: {
value: '感谢您的使用!',
color: '#173177'
}
}
};
const response = await axios.post(url, data);
console.log('消息发送结果:', response.data);
} catch (error) {
console.error('发送消息失败:', error);
}
}
sendTemplateMessage();
方法二:通过企业微信应用消息推送
const axios = require('axios');
// 企业微信配置
const qyWechatConfig = {
corpId: '你的企业ID',
corpSecret: '应用的Secret',
agentId: '应用ID',
toUser: '接收成员ID,多个用|分隔'
};
// 获取企业微信access_token
async function getQYAccessToken() {
const url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${qyWechatConfig.corpId}&corpsecret=${qyWechatConfig.corpSecret}`;
const response = await axios.get(url);
return response.data.access_token;
}
// 发送企业微信应用消息
async function sendQYMessage() {
try {
const accessToken = await getQYAccessToken();
const url = `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accessToken}`;
const data = {
touser: qyWechatConfig.toUser,
msgtype: 'text',
agentid: qyWechatConfig.agentId,
text: {
content: '这是一条来自Node.js的企业微信消息\n时间:' + new Date().toLocaleString()
},
safe: 0
};
const response = await axios.post(url, data);
console.log('企业微信消息发送结果:', response.data);
} catch (error) {
console.error('发送企业微信消息失败:', error);
}
}
sendQYMessage();
方法三:通过Server酱等第三方服务推送(到微信)
const axios = require('axios');
// Server酱配置(Turbo版需要填写自己的SendKey)
const serverChanConfig = {
sendKey: '你的SendKey',
title: 'Node.js消息推送',
desp: '这是一条通过Server酱从Node.js发送的微信消息\n\n' +
'时间:' + new Date().toLocaleString() + '\n\n' +
'[点击查看详情](https://example.com)'
};
// 通过Server酱发送微信消息
async function sendServerChanMessage() {
try {
const url = `https://sctapi.ftqq.com/${serverChanConfig.sendKey}.send`;
const response = await axios.post(url, {
title: serverChanConfig.title,
desp: serverChanConfig.desp
});
console.log('Server酱消息发送结果:', response.data);
} catch (error) {
console.error('通过Server酱发送消息失败:', error);
}
}
sendServerChanMessage();
方法四:通过微信小程序订阅消息推送
const axios = require('axios');
// 小程序配置
const miniProgramConfig = {
appId: '小程序appId',
appSecret: '小程序appSecret',
templateId: '订阅消息模板ID',
openId: '用户openid'
};
// 获取小程序access_token
async function getMiniProgramToken() {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${miniProgramConfig.appId}&secret=${miniProgramConfig.appSecret}`;
const response = await axios.get(url);
return response.data.access_token;
}
// 发送小程序订阅消息
async function sendMiniProgramMessage() {
try {
const accessToken = await getMiniProgramToken();
const url = `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${accessToken}`;
const data = {
touser: miniProgramConfig.openId,
template_id: miniProgramConfig.templateId,
page: 'pages/index/index', // 点击消息跳转的小程序页面
data: {
thing1: { value: 'Node.js推送测试' },
time2: { value: new Date().toLocaleString() },
thing3: { value: '这是一条测试消息' }
}
};
const response = await axios.post(url, data);
console.log('小程序订阅消息发送结果:', response.data);
} catch (error) {
console.error('发送小程序订阅消息失败:', error);
}
}
sendMiniProgramMessage();
注意事项
-
微信公众号模板消息:需要服务号且用户已关注,模板消息需要事先申请
-
企业微信消息:需要企业微信账号和应用权限
-
Server酱:免费版有发送限制,适合个人使用
-
小程序订阅消息:需要用户事先授权订阅
根据你的具体需求选择合适的推送方式,并确保已获取所有必要的API权限和密钥。