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