node.js + 企业微信实现定时推送消息

一 . 注册企业微信及配置

进入官网 https://work.weixin.qq.com/ 按要求填写资料开通企业微信。

1. 查看企业 ID

在这里插入图片描述

2. 创建应用

在这里插入图片描述

3. 查看应用 AgentId , Secret ,下拉到页面底部还要配置IP白名单

在这里插入图片描述

配置IP白名单
在这里插入图片描述

在这里插入图片描述

如果没有配置白名单,运行node.js 代码会报错 60020
在这里插入图片描述

4. 微信关注企业微信

关注后,你可在微信中收发企业微信的工作消息和通知
在这里插入图片描述

二 . 编写node.js代码

企业微信接口文档:https://developer.work.weixin.qq.com/document/path/90372

配置文件 config.js

/*** config.js ***/

// 导入所需插件模块
const request = require('request')

/******** 企业微信相关配置信息 填写自己的信息 ***********/
// 企业ID 替换成自己
const corpId = '*************'
// 应用密钥 替换成自己
const corpSecret = '***********'
// 应用ID 替换成自己
const agentId = 1000002
// 发送给所有人
const toUser = '@all'

const tokenUrl = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}`
const sendMsgUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
/******** 企业微信相关配置信息 填写自己的信息 ***********/

/**
 * 获取令牌
 */
function getToken(success, error) {
	request(tokenUrl, function(error, response, body) {
		if (!error && response.statusCode == 200) {
			var json = JSON.parse(body);
			console.log(json)
			success(json.access_token)
		} else {
			error('获取token失败')
		}
	})
}

/**
 * 真正发送消息
 */
function sendMessage(token, content) {
	const requestData = {
		touser: toUser,
		msgtype: "text",
		agentid: agentId,
		safe: 0,
		text: {
			content: content
		}
	}

	request({
		url: `${sendMsgUrl}${token}`,
		method: "POST",
		json: true,
		headers: {
			"content-type": "application/json",
		},
		body: requestData
	}, function(error, response, body) {
		console.log(body)
		if (!error && response.statusCode == 200) {}
	});
}

/***
 * 发送具体消息 
 */
function sendText(content) {
	getToken((token) => {
		sendMessage(token, content)
	}, (error) => {
		console.log(error)
	})
}

// 向外导出路由对象
module.exports = {
	sendText,
}

主程序文件 app.js

/*** app.js ***/

const alarmWechat = require('./config.js') // 引入配置模块
const schedule = require('node-schedule');

const scheduleCronstyle = ()=> {
    //每30秒执行一次
    schedule.scheduleJob('30 * * * * *',() =>{
        console.log('scheduleCronstyle:' + new Date());
        alarmWechat.sendText('测试发送的消息')
    }); 
	
	
	// 立即执行发送
	// alarmWechat.sendText('测试发送的消息')
}


scheduleCronstyle();


/* 定时模块说明

* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

6个占位符从左到右分别代表:秒、分、时、日、月、周几

每分钟的第30秒触发: '30 * * * * *'

每小时的1分30秒触发 :'30 1 * * * *'

每天的凌晨1点1分30秒触发 :'30 1 1 * * *'

每月的1日1点1分30秒触发 :'30 1 1 1 * *'

2022年的1月1日1点1分30秒触发 :'30 1 1 1 2022 *'

每周1的1点1分30秒触发 :'30 1 1 * * 1'


*/

打开CMD运行服务: node app.js
在这里插入图片描述

手机微信成功接收到消息
在这里插入图片描述

参考文章:
https://blog.csdn.net/weixin_44614230/article/details/126694984

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值