天天看點

node.js + 企業微信實作定時推送消息

一 . 注冊企業微信及配置

進入官網 https://work.weixin.qq.com/ 按要求填寫資料開通企業微信。

1. 檢視企業 ID

node.js + 企業微信實作定時推送消息

2. 建立應用

node.js + 企業微信實作定時推送消息

3. 檢視應用 AgentId , Secret ,下拉到頁面底部還要配置IP白名單

node.js + 企業微信實作定時推送消息

配置IP白名單

node.js + 企業微信實作定時推送消息
node.js + 企業微信實作定時推送消息

如果沒有配置白名單,運作node.js 代碼會報錯 60020

node.js + 企業微信實作定時推送消息

4. 微信關注企業微信

關注後,你可在微信中收發企業微信的工作消息和通知

node.js + 企業微信實作定時推送消息

二 . 編寫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

node.js + 企業微信實作定時推送消息

手機微信成功接收到消息

node.js + 企業微信實作定時推送消息

參考文章:

https://blog.csdn.net/weixin_44614230/article/details/126694984

繼續閱讀