博客评论提醒
最近给博客提醒配置了gitalk评论系统, 如果有某个人评论了我的文章, 就会在github仓库中对应
gitalk的repo生成相应的issue, 而评论就会被记录在该issue的comments历史记录中.
但是评论更新时, 没有提醒功能, 也不知道哪篇文章被评论了. 所以就写了一个发送信息到企业
微信的go程序和一个获取github仓库repo的issue信息的脚本. 结合二者就可以知道哪篇文章被
评论了.
发送消息至企业邮箱的golang代码
- send_message_via_wecom.go
1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "os"
9 "time"
10)
11
12var (
13 corpID = "企业微信app corpID"
14 corpSecret = "企业微信app corpSecret"
15 agentID = "企业微信agentID"
16 tokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +
17 corpID + "&corpsecret=" + corpSecret
18 sendClient = &http.Client{Timeout: 10 * time.Second}
19 msgTo = os.Args[1]
20 subject = os.Args[2]
21 msgBody = os.Args[2] + "\n\n" + os.Args[3]
22 msgURL string
23)
24
25type returnMessage struct {
26 Errcode int `json:"errcode"`
27 Errmsg string `json:"errmsg"`
28 Access_token string `json:"access_token"`
29 Expires_in int `json:"expires_in`
30}
31
32type Text struct {
33 Content string `json:"content"`
34}
35type postBody struct {
36 Touser string `json:"touser"`
37 Msgtype string `json:"msgtype"`
38 Agentid string `json:"agentid"`
39 Text `json:"text"`
40 Safe int `json:"safe"`
41}
42
43var postData = postBody{
44 msgTo,
45 "text",
46 agentID,
47 Text{
48 msgBody,
49 },
50 0,
51}
52
53func main() {
54 var returnMsg = new(returnMessage)
55 var contentType = "application/json; charset=UTF-8"
56 fmt.Println("send content: ", os.Args[3])
57
58 // fill returnMsg
59 errGet := getJson(tokenURL, returnMsg)
60 if errGet != nil {
61 fmt.Println(errGet)
62 return
63 }
64
65 // encoding
66 jm, _ := json.Marshal(postData)
67
68 msgURL := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + returnMsg.Access_token
69 // send message
70 _, err := sendClient.Post(msgURL, contentType, bytes.NewBuffer(jm))
71 if err != nil {
72 panic(err)
73 }
74
75}
76
77// get response body which contains json
78func getJson(url string, target interface{}) error {
79 r, err := sendClient.Get(url)
80 if err != nil {
81 return err
82 }
83 defer r.Body.Close()
84 return json.NewDecoder(r.Body).Decode(target)
85}
获取github仓库issue信息的shell代码
- gitalk_comment_notify.sh
1#!/usr/bin/env bash
2#
3#*******************************************************************************
4# Data: 2021-02-25
5# URL: suosuoli.cn
6# Copyright (C): 2021 All rights reserved
7#*******************************************************************************
8#
9
10export notify_man=suo.li
11export subject=gitalk_event
12export comment_article="Someone left a comment on your blog"
13export comment_md5_dir="/tmp/comment"
14export issues="${comment_md5_dir}/issues"
15
16curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}
17
18while :; do
19 curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}.new
20 if [[ "$(cat ${issues} | md5sum)" == "$(cat ${issues}.new | md5sum)" ]]
21 then
22 echo "sleep 3800s..."
23 sleep 3800
24 else
25 export len=$(cat ${issues}.new | jq length)
26 echo len:$len
27 for ((i=0; i<${len}; i++))
28 do
29 issues_md5_last=$(cat ${comment_md5_dir}/`cat ${issues} | jq '.['''$i'''].title' | awk -F'"| ' '{print $2}'` | md5sum)
30 echo "issues_md5_last: $issues_md5_last"
31 issues_md5_current=$(cat ${issues}.new | jq '.['''$i'''].url,.['''$i'''].body,.['''$i'''].title,.['''$i'''].comments' | md5sum)
32 echo "issues_md5_current: $issues_md5_current"
33
34 if [[ "${issues_md5_last}" == "${issues_md5_current}" ]]
35 then
36 echo "equal...."
37 continue
38 else
39 echo "sending message..."
40 # 直接使用go run *.go运行了, 反正很快
41 go run /home/send_message_via_wecom.go ${notify_man} ${subject} "${comment_article}, article is: $(cat ${issues}.new | jq '.['''$i'''].title' | awk -F'"| ' '{print $2}')"
42 echo "message sent."
43 cat ${issues}.new | jq '.['''$i'''].url,.['''$i'''].body,.['''$i'''].title,.['''$i'''].comments' > ${comment_md5_dir}/$(cat ${issues}.new | jq '.['''$i'''].title' | awk -F'"| ' '{print $2}')
44 fi
45 done
46 curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}
47 fi
48done
49
50unset notify_man
51unset subject
52unset comment_article
53unset comment_md5_dir
54unset issues