Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ChenPt opened this issue Mar 9, 2020 · 0 comments
Open

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

ChenPt opened this issue Mar 9, 2020 · 0 comments
Labels

Comments

@ChenPt
Copy link
Owner

ChenPt commented Mar 9, 2020

企业微信机器人
调用机器人的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
@ChenPt ChenPt changed the title 【小玩意】 记录创建企业微信机器人遇到的问题 Mar 9, 2020
@ChenPt ChenPt added the CLI label Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant