shell 判断微信发送消息是否成功

shell 判断微信发送消息是否成功,并检查token是否过期

#/bin/bash
 
url_get_token=https://qyapi.weixin.qq.com/cgi-bin/gettoken
url_send_msg=https://qyapi.weixin.qq.com/cgi-bin/message/send
msg="$2 $3"
 
#未经测试,自行排查错误,666666
corpid=''
corpsecret=''
 
echo `date "+%Y-%m-%d %H:%M:%S"`: $msg >> send_wechat_op.log
 
body=$(cat << HERE
{
    "totag": "1",
    "msgtype": "text",
    "agentid": "2",
    "text": {
        "content": "$msg"
    },
    "safe":"0"
}
HERE
)
 
save_data=$(cat wechat_token.txt 2>/dev/null)
save_ts=$(echo "$save_data" | awk '{print $1}')
save_token=$(echo "$save_data" | awk '{print $2}')
if [[ $save_ts != "" && $save_token != "" && $(( $(date +%s) - save_ts )) -lt 1800 ]]; then
  accesstoken=$save_token
else
  accesstoken=$(curl --connect-timeout 20 -m 20 -k -q -H 'Host: qyapi.weixin.qq.com' "$url_get_token?corpid=$corpid&corpsecret=$corpsecret"|awk -F"\"" '{print $4}')
  echo "$(date +%s) $accesstoken" > wechat_token.txt
fi
 
 
RESULT=$(curl -s --connect-timeout 20 -m 20 -k -q -H 'Host: qyapi.weixin.qq.com' -d "$body" $url_send_msg?access_token=$accesstoken |awk -F"\"|:|," '{print $4}')
echo $RESULT
 
if [ "$RESULT" -eq "0" ] ; then
	echo "send wechat success"
    exit 0
 
else [ "$RESULT" -ne "0" ] ;
        echo "send wechat error"
	echo $RESULT
    fi

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 65 queries in 0.429 seconds