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数据中


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM