ChenPt / dailyNote

dailyNode for myself
https://github.com/ChenPt/dailyNote/issues
0 stars 0 forks source link

记录创建企业微信机器人遇到的问题 #37

Open ChenPt opened 4 years ago

ChenPt commented 4 years ago

企业微信机器人 调用机器人的web hook,发起一个post请求,就可以把消息发送到包含该机器人的企业微信群组中。

主要使用nodejs ,node-cron, axios来实现。

const axios = require('axios');
const cron = require('node-cron');
const express = require('express');
const app = express();

const URL = 'https://qyapi.weixin.qq.com/xxxxxxxxxxxxxxxx' // 机器人的web hook地址
const PORT = 8578;
const sendTextMsg = (url, msg) => {
  try {
    axios.post(url, {
      msgtype: 'text',
      text: {
        content: msg,
        mentioned_list: ['@all']  // 通知所有人
      }
    })
  } catch (error) {
    console.log(error)
  }
}

app.listen(PORT)

//  在每天19:00 的时候会发消息到群组中
cron.schedule("0 19 * * *", function() {
  sendTextMsg(URL, '记得填写每日工作任务汇报');
  console.log("running a task call sendTextMsg");
});

该nodejs 程序使用pm2来管理

安装pm2

npm i pm2 -g
pm2
// pm2: command not found

报以上错误,说明需要创建个软连接

首先找到pm2的安装目录

find / -name pm2

/usr/local/Celler/node/12.6.0/bin/pm2
// .... 

得到的路径从上到下依次尝试创建软连接

ln -s /usr/local/Celler/node/12.6.0/bin/pm2 /usr/local/bin/pm2

如提示ln: creating symbolic link/usr/local/bin/pm2': File exists` 则将软链接删除后重新创建

# 删除软链接
mv /usr/local/bin/pm2 /tmp/
# 重新创建
ln -s /usr/local/Celler/node/12.6.0/bin/pm2 /usr/local/bin/pm2
# 查看结果
which pm2
# 结果
/usr/local/bin/pm2

开启程序

pm2 start index.js index

终止程序,stop后跟程序名字

pm2 stop index