博客评论提醒

Send Message When Someone Comments

博客评论提醒
Page content

博客评论提醒

最近给博客提醒配置了gitalk评论系统, 如果有某个人评论了我的文章, 就会在github仓库中对应
gitalk的repo生成相应的issue, 而评论就会被记录在该issue的comments历史记录中.

但是评论更新时, 没有提醒功能, 也不知道哪篇文章被评论了. 所以就写了一个发送信息到企业
微信的go程序和一个获取github仓库repo的issue信息的脚本. 结合二者就可以知道哪篇文章被
评论了.

发送消息至企业邮箱的golang代码

  • send_message_via_wecom.go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"time"
)

var (
	corpID     = "企业微信app corpID"
	corpSecret = "企业微信app corpSecret"
	agentID    = "企业微信agentID"
	tokenURL   = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +
		corpID + "&corpsecret=" + corpSecret
	sendClient = &http.Client{Timeout: 10 * time.Second}
	msgTo      = os.Args[1]
	subject    = os.Args[2]
	msgBody    = os.Args[2] + "\n\n" + os.Args[3]
	msgURL     string
)

type returnMessage struct {
	Errcode      int    `json:"errcode"`
	Errmsg       string `json:"errmsg"`
	Access_token string `json:"access_token"`
	Expires_in   int    `json:"expires_in`
}

type Text struct {
	Content string `json:"content"`
}
type postBody struct {
	Touser  string `json:"touser"`
	Msgtype string `json:"msgtype"`
	Agentid string `json:"agentid"`
	Text    `json:"text"`
	Safe    int `json:"safe"`
}

var postData = postBody{
	msgTo,
	"text",
	agentID,
	Text{
		msgBody,
	},
	0,
}

func main() {
	var returnMsg = new(returnMessage)
	var contentType = "application/json; charset=UTF-8"
	fmt.Println("send content: ", os.Args[3])

	// fill returnMsg
	errGet := getJson(tokenURL, returnMsg)
	if errGet != nil {
		fmt.Println(errGet)
		return
	}

	// encoding
	jm, _ := json.Marshal(postData)

	msgURL := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + returnMsg.Access_token
	// send message
	_, err := sendClient.Post(msgURL, contentType, bytes.NewBuffer(jm))
	if err != nil {
		panic(err)
	}

}

// get response body which contains json
func getJson(url string, target interface{}) error {
	r, err := sendClient.Get(url)
	if err != nil {
		return err
	}
	defer r.Body.Close()
	return json.NewDecoder(r.Body).Decode(target)
}

获取github仓库issue信息的shell代码

  • gitalk_comment_notify.sh
#!/usr/bin/env bash
#
#*******************************************************************************
# Data:              2021-02-25
# URL:               suosuoli.cn
# Copyright (C):     2021 All rights reserved
#*******************************************************************************
#

export notify_man=suo.li
export subject=gitalk_event
export comment_article="Someone left a comment on your blog"
export comment_md5_dir="/tmp/comment"
export issues="${comment_md5_dir}/issues"

curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}

while :; do
    curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}.new
    if [[ "$(cat ${issues} | md5sum)" == "$(cat ${issues}.new | md5sum)" ]]
    then
         echo "sleep 3800s..."
         sleep 3800
    else
         export len=$(cat ${issues}.new | jq length)
         echo len:$len
         for ((i=0; i<${len}; i++))
         do 
            issues_md5_last=$(cat ${comment_md5_dir}/`cat ${issues} | jq '.['''$i'''].title' | awk -F'"| ' '{print $2}'` | md5sum)
            echo "issues_md5_last: $issues_md5_last"
            issues_md5_current=$(cat ${issues}.new | jq '.['''$i'''].url,.['''$i'''].body,.['''$i'''].title,.['''$i'''].comments' | md5sum)
            echo "issues_md5_current: $issues_md5_current"

        	if [[ "${issues_md5_last}" == "${issues_md5_current}" ]] 
            then
                echo "equal...."
                continue
            else
                echo "sending message..."
                # 直接使用go run *.go运行了, 反正很快
                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}')"
                echo "message sent."
                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}')
            fi
        done
        curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jaccyli/hugo-blog-gitalk/issues > ${issues}
    fi
done

unset notify_man
unset subject
unset comment_article
unset comment_md5_dir
unset issues