The case of this article is included in https://github.com/chengxy-nds/Springboot-Notebook
Hello everyone, I am Xiaofu~
Recently I took a task and webhook
. After someone submits the code to the remote warehouse, a message will be sent in the enterprise WeChat group, similar to @XXX at XXX time, submit XXXX code to XXX project Copywriting.
As for why I want to make such a tool, there is no way to crush people at a higher level. In fact, I refuse it in my heart. It always feels weird like being watched. Could it be that I found out that I mentioned the code secretly and fixed the bug silently?
webhook
webhook
hook we often say. If you are not familiar with hooks, it doesn't matter, then we should change the concept. Callback
URL
should have heard of it. For example, three-party platforms such as WeChat Pay support the configuration of callback URLs to notify the payment status.
When some events are triggered, such as: " push
code to remote warehouse", or "provide a issue
", etc., the source website can initiate a HTTP
request to the URL configured by webhook
The following figure shows the workflow of this tool. Developers GitHub
project will trigger pull event
POST
request to the three-party URL configured in the GitHub webhook. This three-party platform can be DingTalk, Feishu, and Enterprise. Platforms such as WeChat.
Below we use GitHub
+ enterprise WeChat to implement code submission monitoring, and automatically push messages to the enterprise WeChat group.
Configure GitHub webhook
Settings
GitHub corresponding project, and do the basic configuration of webhook
Four parts of the main configuration:
Payload URL
callback service address;
Content type
callback request header, the format of JSON
Secret
for security verification, will header
after setting to distinguish the source of the request and prevent the exposed request from being maliciously accessed;
X-Hub-Signature: sha1=2478e400758f6114aa18abc4380ef8fad0b16fb9
X-Hub-Signature-256: sha256=68bde5bee18bc36fd95c9b71b4a89f238cb01ab3bf92fd67de3a1de12b4f5c72
Finally, we choose which events to trigger the webhook
callback, push event
(code push event), everything
(all events), and some specific events.
We can Recent Deliveries
view webhook
callback records, as well as the full request and parameter data can also be redelivery
analog transmission request.
Configure Enterprise WeChat
The configuration of enterprise WeChat is actually simpler. Let's create a group first. Right-click the group and there is a option to add robots. After successful addition, it will generate
webhook
address. We only need to send a POST
request to this address, and the group will receive a push message.
The message content supports four message types: text (text), markdown (markdown), image (image), and graphic (news), and it also supports @group members in the group. The text format is demonstrated below.
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=145a516a-dd15-421f-97a3-ba3bf1479369' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "你好,我是程序员内点事"
}
}'
Directly request the url and find that the message push is successful, indicating that the configuration is no problem.
But at this point, everyone has found a problem. The GitHub
and enterprise WeChat only need to start the request, and the other only accepts the request of the fixed data format. The data of the two interfaces are not compatible at all?
Request forwarding
Since they are not compatible, there is no way, we can only do a layer of adaptation in the middle, who can not afford to bother!
The forwarding logic is also relatively simple, just accept GitHub
, slightly modify and assemble it into the data format required by the enterprise WeChat, and send it directly.
GitHub
includes information such as warehouse, author, submitter, submission content, etc., which is basically sufficient.
code implementation is relatively rough, will look at it
@Slf4j
@RestController
public class WebhookController {
private static String WECHAT_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=145a516a-dd15-421f-97a3-ba3bf1479369";
private static String GITHUB_API = "https://api.github.com/users/";
/**
* @param webhook webhook
* @author 程序员内点事
* @Description: github 回调
* @date 2021/05/19
*/
@PostMapping("/webhook")
public String webhookGithub(@RequestBody GithubWebhookPullVo webhook) {
log.info("webhook 入参接收 weChatWebhook {}", JSON.toJSONString(webhook));
// 仓库名
String name = webhook.getRepository().getName();
SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = simpleFormatter.format(new Date());
String content = null;
if (webhook.getCommits().size() > 0) {
GithubWebhookPullVo.CommitsDTO commitsDTO = webhook.getCommits().get(0);
content = "[" + commitsDTO.getCommitter().getName() + "]" +
"于:" + now + "," +
"向作者:[" + commitsDTO.getAuthor().getName() + "]的,远程仓库" + name + "推送代码" +
"详情:";
List<String> addeds = commitsDTO.getAdded();
if (addeds.size() > 0) {
content += "添加文件:";
for (int i = 0; i < addeds.size(); i++) {
content = (i + 1) + content + addeds.get(i);
}
}
List<String> modifieds = commitsDTO.getModified();
if (modifieds.size() > 0) {
content += "修改文件:";
for (int i = 0; i < modifieds.size(); i++) {
content = (i + 1) + content + modifieds.get(i);
}
}
List<String> removeds = commitsDTO.getRemoved();
if (removeds.size() > 0) {
content += "删除文件:";
for (int i = 0; i < removeds.size(); i++) {
content = (i + 1) + content + removeds.get(i);
}
}
}
log.info(content);
WeChatWebhook weChatWebhook = new WeChatWebhook();
weChatWebhook.setMsgtype("text");
WeChatWebhook.TextDTO textDTO = new WeChatWebhook.TextDTO();
textDTO.setContent(content);
textDTO.setMentionedList(Arrays.asList("@all"));
textDTO.setMentionedMobileList(Arrays.asList("@all"));
weChatWebhook.setText(textDTO);
/**
* 组装参数后向企业微信发送webhook请求
*/
log.info("企业微信发送参数 {}", JSON.toJSONString(weChatWebhook));
String post = HttpUtil.sendPostJsonBody(WECHAT_URL, JSON.toJSONString(weChatWebhook));
log.info("企业微信发送结果 post {}", post);
return JSON.toJSONString(post);
}
}
Here is a reminder that some of the data callback from the GitHub webhook cannot be used directly. In some scenarios, you still need to call GitHub API
in exchange for some data.
Document address: https://docs.github.com/en/rest/reference
The above configuration work is completed, and then the forwarded code is deployed to the server, and the entire link is tested to see the effect. The pom.xml
file is deliberately modified to submit. After the code is submitted, it is found that the message is successfully sent to the enterprise WeChat, which is consistent with our expected effect.
Source address: https://github.com/chengxy-nds/Springboot-Notebook/
This project includes all the cases in my previous articles, such as: tool source code,
face recognition project source code, and various problem solving cases for middleware such
redis
, Seata
, MQ
Star
It will definitely be used in actual development.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。