1 Webhook
Webhook is an HTTP-based callback function that enables lightweight event-driven communication between two application programming interfaces (APIs). The client provides a unique URL to the server API and specifies the events it wants to be informed about. Once the webhook is set up, the client no longer needs to poll the server; when the specified event occurs, the server automatically sends the relevant payload to the client’s Webhook URL.
Webhook can be an important part of automation, and its implementation is simple (just one HTTP request, which can be embedded anywhere). Domestic platforms like Feishu, DingTalk, and WeChat Work all support Webhook message pushing.
The advantage is its simplicity, but the downside is the lack of uniformity in HTTP request parameters. Because it’s so simple, there is no protocol specification, and each platform is different. For example, a message might be called “msg” on one platform, but “message” or “text” on another. Therefore, to truly leverage the power of Webhook, you need to set up your own Webhook parsing and processing service. Fortunately, this is not complicated. This article will use Github Webhook as an example, based on Flask, to parse and push to WeChat Work.
However, if you do not have highly customized needs and only want to use Github Webhook without using other platforms, you can use Dingling , which can forward Github Webhook to WeChat Work. Dingling only supports pushing Markdown formatted messages (which cannot be directly viewed in WeChat), which is why I gave it up.
2 CloudFlare Webhook
The message body of CloudFlare Webhook is very simple, as follows:
1
{ "text" : "CloudFlare is simply the industry's conscience!" }
Use Flask to parse and forward the message to WeChat Work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from flask import Flask , request , jsonify , abort
import requests
from config import config
app = Flask ( __name__ )
@app.route ( '/cloudflare' , methods = [ 'POST' ])
def cloudflare ():
if request . method == 'POST' :
print ( request . json )
text = request . json [ 'text' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : text
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_3' ], json = data )
print ( r . json ())
return 'success' , 200
else :
abort ( 400 )
if __name__ == '__main__' :
app . run ( port = 5700 , host = '0.0.0.0' , debug = True )
Deploy the Flask process to the server and fill in the address on Cloudflare as shown in the picture
After saving and testing, you should receive a short test message in WeChat Work.
3 Github Webhook
If CloudFlare’s Webhook is a short message, then Github wants to send you all the information on the entire website. There are dozens of parameters, but none in natural language, so you need to parse them according to your needs. The logic of the following code is to first determine the event type and then extract the required information based on the event type to forward it. The parsed content includes actions such as push, release, issue, pull, etc.
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
@app.route ( '/github' , methods = [ 'POST' ])
def github ():
githubEvent = request . headers [ 'x-github-event' ]
if githubEvent == 'push' :
respority = request . json [ 'repository' ][ 'name' ]
branch = request . json [ 'ref' ] . split ( '/' )[ - 1 ]
commits = request . json [ 'commits' ]
for commit in commits :
author = commit [ 'author' ][ 'name' ]
message = commit [ 'message' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone committed code \n Branch: { branch } \n Author: { author } \n Commit message: { message } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
return 'success' , 200
if githubEvent == 'issues' :
action = request . json [ 'action' ]
if action == 'opened' :
respority = request . json [ 'repository' ][ 'name' ]
issue = request . json [ 'issue' ]
title = issue [ 'title' ]
body = issue [ 'body' ]
user = issue [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone opened an issue \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
if action == 'closed' :
respority = request . json [ 'repository' ][ 'name' ]
issue = request . json [ 'issue' ]
title = issue [ 'title' ]
body = issue [ 'body' ]
user = issue [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone closed an issue \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
if action == 'reopened' :
respority = request . json [ 'repository' ][ 'name' ]
issue = request . json [ 'issue' ]
title = issue [ 'title' ]
body = issue [ 'body' ]
user = issue [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone reopened an issue \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
return 'success' , 200
if githubEvent == 'pull_request' :
action = request . json [ 'action' ]
if action == 'opened' :
respority = request . json [ 'repository' ][ 'name' ]
pull_request = request . json [ 'pull_request' ]
title = pull_request [ 'title' ]
body = pull_request [ 'body' ]
user = pull_request [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone submitted a pull request \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
if action == 'closed' :
respority = request . json [ 'repository' ][ 'name' ]
pull_request = request . json [ 'pull_request' ]
title = pull_request [ 'title' ]
body = pull_request [ 'body' ]
user = pull_request [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone closed a pull request \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
if action == 'reopened' :
respority = request . json [ 'repository' ][ 'name' ]
pull_request = request . json [ 'pull_request' ]
title = pull_request [ 'title' ]
body = pull_request [ 'body' ]
user = pull_request [ 'user' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone reopened a pull request \n Title: { title } \n Content: { body } \n Submitter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
return 'success' , 200
if githubEvent == 'release' :
action = request . json [ 'action' ]
if action == 'published' :
respority = request . json [ 'repository' ][ 'name' ]
release = request . json [ 'release' ]
tag_name = release [ 'tag_name' ]
body = release [ 'body' ]
user = release [ 'author' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone published a release \n Version: { tag_name } \n Content: { body } \n Publisher: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
return 'success' , 200
if githubEvent == 'ping' :
data = {
"msgtype" : "text" ,
"text" : {
"content" : "github webhook test"
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ], json = data )
return 'success' , 200
if githubEvent == 'create' :
ref_type = request . json [ 'ref_type' ]
if ref_type == 'branch' :
respority = request . json [ 'repository' ][ 'name' ]
branch = request . json [ 'ref' ]
user = request . json [ 'sender' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone created a branch \n Branch: { branch } \n Creator: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
if ref_type == 'tag' :
respority = request . json [ 'repository' ][ 'name' ]
tag = request . json [ 'ref' ]
user = request . json [ 'sender' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone created a tag \n tag: { tag } \n Creator: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
return 'success' , 200
if githubEvent == 'delete' :
ref_type = request . json [ 'ref_type' ]
if ref_type == 'branch' :
respority = request . json [ 'repository' ][ 'name' ]
branch = request . json [ 'ref' ]
user = request . json [ 'sender' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone deleted a branch \n Branch: { branch } \n Deleter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
if ref_type == 'tag' :
respority = request . json [ 'repository' ][ 'name' ]
tag = request . json [ 'ref' ]
user = request . json [ 'sender' ][ 'login' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone deleted a tag \n tag: { tag } \n Deleter: { user } "
}
}
r = requests . post ( 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
return 'success' , 200
if githubEvent == 'pull_request_review' :
action = request . json [ 'action' ]
if action == 'submitted' :
respority = request . json [ 'repository' ][ 'name' ]
pull_request = request . json [ 'pull_request' ]
title = pull_request [ 'title' ]
body = pull_request [ 'body' ]
user = pull_request [ 'user' ][ 'login' ]
review = request . json [ 'review' ]
state = review [ 'state' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone submitted a pull request review \n Title: { title } \n Content: { body } \n Submitter: { user } \n State: { state } "
}
}
r = requests . post (
'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
return 'success' , 200
if githubEvent == 'pull_request_review_comment' :
action = request . json [ 'action' ]
if action == 'created' :
respority = request . json [ 'repository' ][ 'name' ]
pull_request = request . json [ 'pull_request' ]
title = pull_request [ 'title' ]
body = pull_request [ 'body' ]
user = pull_request [ 'user' ][ 'login' ]
comment = request . json [ 'comment' ]
comment_body = comment [ 'body' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone submitted a pull request review comment \n Title: { title } \n Content: { body } \n Submitter: { user } \n Content: { comment_body } "
}
}
r = requests . post (
'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
return 'success' , 200
# if githubEvent == 'check_run':
# action = request.json['action']
# if action == 'created':
# respority = request.json['repository']['name']
# check_run = request.json['check_run']
# name = check_run['name']
# conclusion = check_run['conclusion']
# data = {
# "msgtype": "text",
# "text": {
# "content": f"{respority} Someone submitted a check run\nName: {name}\nState: {conclusion}"
# }
# }
# r = requests.post(
# 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='+config['API_KEY_2'],
# json=data)
# return 'success', 200
if githubEvent == 'check_suite' :
action = request . json [ 'action' ]
if action == 'completed' :
respority = request . json [ 'repository' ][ 'name' ]
check_suite = request . json [ 'check_suite' ]
conclusion = check_suite [ 'conclusion' ]
data = {
"msgtype" : "text" ,
"text" : {
"content" : f " { respority } Someone submitted a check suite \n State: { conclusion } "
}
}
r = requests . post (
'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + config [ 'API_KEY_2' ],
json = data )
return 'success' , 200
After configuring the server and Github, you can view detailed push information in the history.
4 Reference Documents
Github Official Documentation