shell 脚本通过Webhook 发送消息到微信群

代码如下:

#!/bin/sh
# Filename: msg.sh
#
# Usage msg.sh "message text"
#

# 1. check if missing arguments 
if [ $# -lt 1 ]; then
  echo "Usage:";
  echo "\033[1;31m $0 \033[m\033[1;32m  Missing arguments message!!! \033[m.";
  echo "Such as:";
  echo "\033[1;31m $0\033[m\033[1;32m  "Hello, world!" \033[m";
  exit;
fi

# 2. Message is OK

## 下一行 curl 后面为 Webhook 的url值
result=`curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=这里隐藏了keyvalue' \  
   -H 'Content-Type: application/json' \
   -d '
   {
        "msgtype": "text",
        "text": {
            "content": "'"${1}"'"
   		}
   	}'`

# 3 check return value

ret=`echo $result | cut -d ":" -f 3 | cut -d '"' -f 2`
if [[ $ret == "ok" ]]; then
	echo "success";
else
	echo "fail"
fi

# 4 批注:本节curl 命令执行时候 json数据
# json中花括号中的变量,先需要用'扩起来; 然而'又需要用"号扩起来:
# 最终形式为:"'"${variable_name}"'"
# 这样变量就能传入json数据中

posted on 2020-02-08 21:44  小林觉  阅读(4554)  评论(0编辑  收藏  举报

导航