shell脚本调用企业微信api实现微信告警

参考文档:https://work.weixin.qq.com/api/doc/90000/90135/90250

  1. 进入https://work.weixin.qq.com,点击【立即注册】,填写信息注册企业

  2. 点击通讯录,添加一个子部门
    我在这里注册了一个叫“微信告警”的企业,下面有一个子部门叫“测试”


    记住部门“测试”的部门id是2,之后要用到

  3. 点击【应用管理】——》【创建一个应用】



    填写好相关内容,创建应用,记住AgentId和Secret

    如果需要让其他人也看到,需要添加可见范围

  4. 点击我的企业,记住企业ID

  5. 测试连通性
    打开企业微信的接口调试页面https://open.work.weixin.qq.com/wwopen/devtool/interface/combine

填写企业id和Secret

能够正确获取到token就是正常的
6. 编写shell脚本

#!/bin/bash

# 应用ID
AgentID=1000002

# 企业ID
CropID=xxxxxxxxxxxxxx

# 密钥
Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# 获取token
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F " '{print $10}')

# 请求内容,注意是json格式
body='{
  "touser": "@all",
  "toparty": "2",
  "msgtype": "text",
  "agentid": 1000002,
  "text": {
    "content": "床前明月光
疑是地上霜
举头望明月
低头思故乡"
  },
  "safe": 0,
  "enable_id_trans": 0,
  "enable_duplicate_check": 0
}'

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

# 发送消息
/usr/bin/curl --data-ascii "$body" $PURL

tourser处填写收信人id,即企业微信账号

多个收信人用|隔开,@all表示部门所有人。
toparty处填写部门id,多个部门用|隔开。

原文地址:https://www.cnblogs.com/CharrammaBlog/p/14873207.html