08-查询指定数据发送企微应用消息

脚本:查询指定数据发送企微应用消息脚本

基于企业微信(钉钉、飞书同理)自建应用消息,核心为查询远程数据库并处理输出内容:

image-20241223144611102

特别注意:自建应用内的【企业可信IP】也需要提前配置为发起调用的服务器ip,否则调用会受限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh

# 定义 MySQL 用户名、密码和数据库
HOST="127.0.0.1" #可以为远程数据库地址
USER="root"
PASSWORD="password"
DATABASE="db_name" #数据库名

#企业微信的 userid
#QY_USERID="ZhangSan|LiSi|WangWu|JiangYuan"
QY_USERID="JiangYuan" #自测用

#企业微信:企业id[CorpId], 应用id[AgentId], 应用Secret[Secret]
CORP_ID="xxx"
CORP_AGENTID="1000035"
CORP_SECRET="yyy"

#开放API url
ACCESS_TOKEN_URL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET"
SEND_APP_MSG_URL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN"

#计算指定日期所在星期的星期一
DATE=`printf '%(%Y%m%d)T\n' -1`
#DATE='2024-01-01' #测试日期
echo DATE="$DATE"
i=$((`date -d $DATE +'%w'`-1))
MONDAY_DATE=`date -d "$DATE -$i days" +'%Y-%m-%d 00:00:00'`
echo MONDAY_DATE=$MONDAY_DATE

# 查询当周新建课程数:分科时每个科目算1个课程
SQL_COMMAND1="SELECT CONCAT(create_name, ':', SUM(count), ',') as '本周截止当前时间【新建课程】总数:\\\\n'
FROM (SELECT create_name, COUNT(1) AS count
FROM course
WHERE tenant_id = 83
AND division_type = 2
AND del_flag = '1'
AND create_time > '$MONDAY_DATE'
GROUP BY create_name
UNION ALL
SELECT c.create_name, COUNT(1) AS count
FROM course c
LEFT JOIN course_subjects s ON c.id = s.course_id
WHERE c.tenant_id = 83
AND c.division_type = 1
AND c.del_flag = '1'
AND c.create_time > '$MONDAY_DATE'
GROUP BY c.create_name) t1
GROUP BY create_name;"

# 查询当周新建课时数
SQL_COMMAND2="SELECT CONCAT(create_name, ':', SUM(num), ',') AS '本周截止当前时间【新建课时】总数:\\\\n'
FROM (SELECT c.create_name, COUNT(1) AS num
FROM course c
LEFT JOIN course_chapter cc ON c.id = cc.course_id
WHERE c.tenant_id = 83
AND c.del_flag = '1'
AND c.create_time > '$MONDAY_DATE'
AND cc.del_flag = '1'
GROUP BY c.create_name) t1
GROUP BY create_name;"

# 查询当周新建题目数
SQL_COMMAND3="SELECT CONCAT(create_name, ':', count(*), ',') as '本周截止当前时间【新建题目】总数:\\\\n'
FROM question
WHERE parent_id = 0
AND del_flag = '0'
AND create_time > '$MONDAY_DATE'
GROUP BY create_by;"

# 使用 mysql 命令执行 SQL 并获取结果
RESULT1=$(/opt/zbox/bin/mysql -h $HOST -u $USER -p$PASSWORD $DATABASE -e "$SQL_COMMAND1")
RESULT2=$(/opt/zbox/bin/mysql -h $HOST -u $USER -p$PASSWORD $DATABASE -e "$SQL_COMMAND2")
RESULT3=$(/opt/zbox/bin/mysql -h $HOST -u $USER -p$PASSWORD $DATABASE -e "$SQL_COMMAND3")

echo $RESULT1 > /tmp/newCourses
echo $RESULT2 > /tmp/newCourseChapters
echo $RESULT3 > /tmp/newQuestions

# 打印结果
cat /tmp/newCourses
echo
cat /tmp/newCourseChapters
echo
cat /tmp/newQuestions

#获取企业微信access_token
get_qy_access_token() {
echo "get_access_token start..."
CURL_URL=$(echo $ACCESS_TOKEN_URL | sed "s/ID/$CORP_ID/g; s/SECRET/$CORP_SECRET/g")
echo CURL_URL=$CURL_URL

CURL_CMD="curl \"$CURL_URL\""
echo $CURL_CMD

CURL_RES=$(eval $CURL_CMD) # 使用eval执行curl命令
echo $CURL_RES

ACCESS_TOKEN=$(echo $CURL_RES | jq -r '.access_token')
echo ACCESS_TOKEN=$ACCESS_TOKEN
}

#推送文本的应用消息
push_qy_text_app_msg() {
echo "push_qy_text_app_msg start..."
CURL_URL=$(echo $SEND_APP_MSG_URL | sed "s/ACCESS_TOKEN/$ACCESS_TOKEN/g")
echo CURL_URL=$CURL_URL

currentDate=`date +%Y-%m-%d`
newCourses=`perl -pe 's/,/\\\\n/g' /tmp/newCourses`
echo newCourses=$newCourses
newCourseChapters=`perl -pe 's/,/\\\\n/g' /tmp/newCourseChapters`
echo newCourseChapters=$newCourseChapters
newQuestions=`perl -pe 's/,/\\\\n/g' /tmp/newQuestions`
echo newQuestions=$newQuestions

if [[ -z "$newCourses" ]]; then
newCourses="本周截止当前时间【新建课程】总数:0"
fi
if [[ -z "$newCourseChapters" ]]; then
newCourseChapters="本周截止当前时间【新建课时】总数:0"
fi
if [[ -z "$newQuestions" ]]; then
newQuestions="本周截止当前时间【新建题目】总数:0"
fi
finalDataStr="$newCourses \\n $newCourseChapters \\n $newQuestions"

CURL_DATA="{\"touser\":\"$QY_USERID\",\"msgtype\":\"text\",\"agentid\":$CORP_AGENTID,\"text\":{\"content\":\"截止$currentDate >> 本周新增数据统计:\\n $newCourses \\n $newCourseChapters \\n $newQuestions\"}}"
echo "$CURL_DATA"

CURL_CMD="curl \"$CURL_URL\" -H \"Content-Type: application/json\" -d '$CURL_DATA'"
#echo $CURL_CMD

CURL_RES=$(eval $CURL_CMD) # 使用eval执行curl命令
echo $CURL_RES
}

get_qy_access_token
push_qy_text_app_msg

exit 0

把脚本加到计划任务就可以了:

1
2
$ crontab -e
30 11 * * 5 /root/.notice/data_notice.sh #每周的周5下午17:30执行一次脚本

效果展示
image-20241223144906129


08-查询指定数据发送企微应用消息
https://janycode.github.io/2024/02/14/02_编程语言/03_Shell/08-查询指定数据发送企微应用消息/
作者
Jerry(姜源)
发布于
2024年2月14日
许可协议