Shell 发送 企业微信消息

#!/bin/bash
#########################################################################
# File Name: weixin.sh
# Author: rojes
# Email: yangping_198910@163.com
# Created Time: Sun 24 Jul 2016 05:48:14 AM CST
#########################################################################
# Functions: send messages to wechat app

# set variables
CropID='ww6aef11232f5'
Secret='w7gv4123BiNoonTzPKQ'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"

# get acccess_token
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $4}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

# 发送消息体
function body() {
local int AppID=100220003                       #企业号中的应用id
local UserID="rojers"                        #部门成员id,zabbix中定义的微信接收者
local PartyID=2                           #部门id,定义了范围,组内成员都可接收到消息
local Msg=$(echo "$@" | cut -d" " -f3-)   #过滤出zabbix传递的第三个参数
printf '{\n'
printf '\t"touser": "'"$UserID"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"$Msg"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $! $2 $3)" $PURL