本帖最后由 kover 于 2022-12-10 15:28 编辑
前提:开通了企业微信以及应用(后面开通的需要白名单)
效果图如下
环境需要安装jq。自己修改对应自己的key之类的,放到crontab中定时运行就行了。
存在问题:点开链接的内容跟这个图文不一致,有大佬知道如何修改的可以改进一下,用了人家的超链接貌似不好获取地址。
代码如下
[Bash shell] 纯文本查看 复制代码 #!/bin/bash
TQ=$(curl -X POST https://v2.alapi.cn/api/mryw/random -H 'Content-Type: application/x-www-form-urlencoded' -d 'token=ik4565474')
#TQ=$(cat test.json)
biaoti=$(echo $TQ|jq '.data'|jq -r '.title')
zuozhe="作者:【"$(echo $TQ | jq '.data' |jq -r '.author')"】"
gaiyao=$(echo $TQ | jq '.data' |jq -r '.desc'|sed 's/ \s/./g'|sed ":a;N;s/\n//g;ta")
url="https:\/\/meiriyiwen.com\/"
picurl="https:\/\/meiriyiwen.com\/images\/new_feed\/bg_$((1 + RANDOM % 98)).jpg"
btntxt="阅读全文"
#echo $url
#echo $picurl
#echo $btntxt
expireTime=7200
dbFile="/var/www/tianqi/db.json"
corpid='3424234'
appsecret='435346kDMXsAsUVOhwuarzRk'
agentid='123344'
timenow=`date +%Y-%m-%d\..%H:%M:%S`
if [ ! -f "$dbFile" ];then
touch "$dbFile"
fi
req_time=`jq '.req_time' $dbFile`
current_time=$(date +%s)
refresh=false
if [ ! -n "$req_time" ];then
refresh=true
else
if [ $((current_time-req_time)) -gt $expireTime ];then
refresh=true
fi
fi
if $refresh ;then
token=$(curl "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${appsecret}" | jq -r '.access_token')
if [ -z "$token" ];then
# echo "not get access_token,check jq and rm /etc/tuisong/db.json"
exit
else
# echo "access_token:$token"
## save
echo "" > $dbFile
echo -e "{" > $dbFile
echo -e "\"access_token\":\"$token\"," >> $dbFile
echo -e "\"req_time\":$current_time" >> $dbFile
echo -e "}" >> $dbFile
fi
fi
# 发送消息
accesstoken=`jq -r '.access_token' $dbFile`
msgsend_url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accesstoken}"
json_params="{\"toparty\":\"2\",\"msgtype\":\"news\",\"agentid\":\"${agentid}\",\"news\":{\"articles\":[{\"title\":\"$biaoti\",\"description\":\"$zuozhe$gaiyao\",\"url\":\"$url\",\"picurl\":\"$picurl\",\"btntxt\":\"$btntxt\"}]}}"
# curl -X POST ${msgsend_url} -d ${json_params}
echo $json_params
req_msg=$(curl -X POST ${msgsend_url} -d ${json_params} | jq -r '.errcode')
if [ "$req_msg" = "0" ];then
echo "推送成功:$req_msg"
else
echo "推送失败:$req_msg"
fi
if [ "$req_msg" = "40014" ];then
echo "not availe access_token,retry"
token=$(curl "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${appsecret}" | jq -r '.access_token')
if [ -z "$token" ];then
echo "second access_token fail,check jq and rm /etc/tuisong/db.json"
exit
else
echo "second access_token:$token"
## 保存文件
echo "" > $dbFile
echo -e "{" > $dbFile
echo -e "\"access_token\":\"$token\"," >> $dbFile
echo -e "\"req_time\":$current_time" >> $dbFile
echo -e "}" >> $dbFile
fi
# 发送消息
accesstoken=`jq -r '.access_token' $dbFile`
msgsend_url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accesstoken}"
json_params="{\"toparty\":\"2\",\"msgtype\":\"news\",\"agentid\":\"${agentid}\",\"news\":{,\"articles\":[{\"title\":\"$biaoti\",\"description\":\"$zuozhe$gaiyao\",\"url\":\"$url\",\"picurl\":\"$picurl\",\"btntxt\":\"$btntxt\"}]}}"
# curl -X POST ${msgsend_url} -d ${json_params}
echo $json_params
req_msg=$(curl -X POST ${msgsend_url} -d ${json_params} | jq -r '.errcode')
if [ "$req_msg" = "0" ];then
echo "重新推送成功:$req_msg"
else
echo "重新推送失败:$req_msg"
fi
fi
|