go语言 爬虫 钉钉群机器人

第一步:钉钉新建一个群机器人
钉钉创建群机器人文档:https://open.dingtalk.com/document/orgapp/custom-robot-access

安全设置选择签名
签名设置文档:https://open.dingtalk.com/document/robots/customize-robot-security-settings

第二步 代码:

参考了两个

爬虫部分:http://liuqh.icu/2021/07/15/go/package/26-colly/

钉钉群机器人发消息部分:https://www.cnblogs.com/xll970105/p/13176253.html

例子代码:

go 复制代码
package main

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/gocolly/colly/v2"
)

func main() {
	DouBanBook()
}

// 豆瓣书榜单
func DouBanBook() error {

	// 获取钉钉群消息机器人url
	url := Sign()

	// 创建 Collector 对象
	collector := colly.NewCollector()
	// 在请求之前调用
	collector.OnRequest(func(request *colly.Request) {
		fmt.Println("回调函数OnRequest: 在请求之前调用")
	})
	// 请求期间发生错误,则调用
	collector.OnError(func(response *colly.Response, err error) {
		fmt.Println("回调函数OnError: 请求错误", err)
	})
	// 收到响应后调用
	collector.OnResponse(func(response *colly.Response) {
		fmt.Println("回调函数OnResponse: 收到响应后调用")
	})
	//OnResponse如果收到的内容是HTML ,则在之后调用
	collector.OnHTML("ul[class='subject-list']", func(element *colly.HTMLElement) {
		// 遍历li
		element.ForEach("li", func(i int, el *colly.HTMLElement) {
			// 获取封面图片
			coverImg := el.ChildAttr("div[class='pic'] > a[class='nbg'] > img", "src")
			// 获取书名
			bookName := el.ChildText("div[class='info'] > h2")
			// 获取发版信息,并从中解析出作者名称
			authorInfo := el.ChildText("div[class='info'] > div[class='pub']")
			split := strings.Split(authorInfo, "/")
			author := split[0]
			fmt.Printf("封面: %v 书名:%v 作者:%v\n", coverImg, trimSpace(bookName), author)

			ddtext := fmt.Sprintf("![%s](%s)<br/> 书名:%s,  作者:%s <br/>", trimSpace(bookName), coverImg, trimSpace(bookName), author)
			fmt.Println(ddtext)

			// 发送消息
			dingToInfo(bookName, ddtext, url)
		})
	})
	// 发起请求
	return collector.Visit("https://book.douban.com/tag/小说")
}

// 删除字符串中的空格信息
func trimSpace(str string) string {
	// 替换所有的空格
	str = strings.ReplaceAll(str, " ", "")
	// 替换所有的换行
	return strings.ReplaceAll(str, "\n", "")
}

// 签名生成算法
func hmacSha256(stringToSign string, secret string) string {
	h := hmac.New(sha256.New, []byte(secret))
	h.Write([]byte(stringToSign))
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

// Sign 加密生成钉钉签名
func Sign() string {
	secret := "钉钉群机器人secret"
	webhook := "钉钉群机器人webhook"
	timestamp := time.Now().UnixNano() / 1e6
	stringToSign := fmt.Sprintf("%d\n%s", timestamp, secret)
	sign := hmacSha256(stringToSign, secret)
	url := fmt.Sprintf("%s&timestamp=%d&sign=%s", webhook, timestamp, sign)
	return url
}

// 钉钉初始化
func dingToInfo(t, s, url string) bool {
	content, data := make(map[string]string), make(map[string]interface{})
	content["title"] = t
	content["text"] = s
	data["msgtype"] = "markdown"
	data["markdown"] = content
	b, _ := json.Marshal(data)

	resp, err := http.Post(url,
		"application/json",
		bytes.NewBuffer(b))
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	return true
}
相关推荐
喵手24 分钟前
Python爬虫实战:采集菜谱网站的“分类/列表页”(例如“家常菜”或“烘焙”频道)数据,构建高可用的美食菜谱数据采集流水线(附CSV导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集菜谱网站数据·家常菜或烘焙频道·构建高可用食谱数据采集系统
喵手25 分钟前
Python爬虫实战:硬核解析 Google Chrome 官方更新日志(正则+文本清洗篇)(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·监控谷歌版本发布历史·获取稳定版更新日志
牛奔3 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
不老刘7 小时前
LiveKit 本地部署全流程指南(含 HTTPS/WSS)
golang·实时音视频·livekit
Tony Bai13 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
深蓝电商API14 小时前
处理字体反爬:woff字体文件解析实战
爬虫·python
NPE~15 小时前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
念何架构之路16 小时前
Go进阶之panic
开发语言·后端·golang
先跑起来再说16 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
喵手21 小时前
Python爬虫实战:电商价格监控系统 - 从定时任务到历史趋势分析的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·电商价格监控系统·从定时任务到历史趋势分析·采集结果sqlite存储