实现文件、消息、图片的发送

#!/bin/bash
#set -x
Key=xxx
api_sendfile="https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=${Key}&type=file"
api_post="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${Key}"

check-arguments (){
if  [[ -z "$1" ]] || [[ -z "$2" ]]
then
    sleep 0.3
    echo -e "\033[31mThis script requires exactly  arguments.\033[0m"
    cat <<EOF
    Usage:
    1)  $0 sendfile filename
    2)  $0 sendcontent [content] [mobilephone]
    3)  $0 sendpic picname
EOF
    exit 99
fi
}
send-content () {
curl ${api_post} -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": {"content": "'${1}'","mentioned_mobile_list":["'${2}'"]}}'
# $1是内容,$2是需要@的人,填入电话号码
# 由于是位参传入,暂不支持空格
}
send-file () {
#media_id=$(curl -s -H 'Content-Type: multipart/form-data' ${api_sendfile} -F "media=@$1" | jq -r .media_id)
#media_id=$(curl -s -H 'Content-Type: multipart/form-data' ${api_sendfile} -F "media=@$1" |awk -F'"' '{print $14}')
media_id=$(curl -s -H 'Content-Type: multipart/form-data' ${api_sendfile} -F "media=@$1" |sed -n 's/.*"media_id":"\([^"]*\)".*/\1/p')
# 上传文件并获取media_id
curl ${api_post} -H 'Content-Type: application/json' -d '{"msgtype": "file", "file": {"media_id": "'$media_id'"}}'
# 通过media_id发送文件至企业微信
}

send-pic () {
json_data=$(cat <<EOF
{
    "msgtype": "image",
    "image": {
       "base64" : "$(base64 $1)",
       "md5": "$(md5sum $1 | awk '{print $1}')"
    }
}
EOF
)
echo "$json_data"|curl -X POST -H "Content-Type: application/json" -d @- ${api_post}
}


#check-arguments
case $1 in
        -h)
                check-arguments
                ;;
        sendfile)
                send-file $2 ;;
        sendcontent)
                send-content $2 $3 ;;
        sendpic)
                send-pic $2  ;;
        *)
                echo -e "\033[33;5mrguments error.\033[0m\n"
                sleep 0.5
                check-arguments
                ;;
esac

实现图文消息的发送

#!/bin/bash
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx' \
   -H 'Content-Type: application/json' \
   -d '
   {
    "msgtype": "news",
    "news": {
       "articles" : [
           {
               "title" : "请及时报工",
               "description" : "报工!!!",
               "url" : "https://www.baidu.com/",
               "picurl" : "https://s21.ax1x.com/2024/10/16/pANGPxI.jpg"
           }
        ]
    }
}'

# 需要先将图片上传至这个免费图床 https://imgse.com/

在这里插入图片描述

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐