diff --git a/README.md b/README.md index dfed110..418c7f2 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ ChromeDriver镜像站:http://npm.taobao.org/mirrors/chromedriver/ - [x] [飞书群机器人](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN) - [x] [钉钉群机器人](https://open.dingtalk.com/document/robots/custom-robot-access) - [x] [WgpSecBot](https://bot.wgpsec.org) +- [x] [CqHttpBot](https://github.com/Mrs4s/go-cqhttp) - [ ] [pushplus](http://pushplus.hxtrip.com/) ## Install diff --git a/bot/CqHttpBot.go b/bot/CqHttpBot.go new file mode 100644 index 0000000..51f2573 --- /dev/null +++ b/bot/CqHttpBot.go @@ -0,0 +1,78 @@ +package bot + +import ( + . "SecCrawler/config" + "SecCrawler/register" + "SecCrawler/utils" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "strings" +) + +type CqHttpBot struct{} + +func (bot CqHttpBot) Config() register.BotConfig { + return register.BotConfig{ + Name: "CqHttpBot", + } +} + +// Send 推送消息给CqHttpBot。 +func (bot CqHttpBot) Send(crawlerResult [][]string, description string) error { + var msg string + + for _, i := range crawlerResult { + text := fmt.Sprintf("%s\n%s\n\n", i[1], i[0]) + msg += text + } + title := fmt.Sprintf("%s\n%s\n\n", description, utils.CurrentTime()) + + client := utils.BotClient(Cfg.Bot.CqHttpBot.Timeout) + + allData := msg + for { // 消息分片,避免过长消息发送失败 + if len(allData) > 3000 { + left := allData[:3000] + right := allData[3000:] + index := strings.Index(right, "\n\n") + left = left + right[:index] + allData = right[index+2:] + + err := sendslip(client, left, title) + if err != nil { + return err + } + } else { + err := sendslip(client, allData, title) + if err != nil { + return err + } + break + } + } + return nil +} + +func sendslip(client *http.Client, msg string, title string) error { + msgEncode := url.QueryEscape(title + msg) + data := fmt.Sprintf(`group_id=%d&message=%s`, Cfg.Bot.CqHttpBot.QQGroup, msgEncode) + + req, err := http.NewRequest("POST", Cfg.Bot.CqHttpBot.Api+"/send_group_msg?access_token="+Cfg.Bot.CqHttpBot.Key, strings.NewReader(data)) + if err != nil { + return err + } + req.Header.Set("Content-type", "application/x-www-form-urlencoded") + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + respString, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + fmt.Printf("[*] send to CqHttpBot: %s\n", respString) + return nil +} diff --git a/bot/init.go b/bot/init.go index 312b38c..151a99d 100644 --- a/bot/init.go +++ b/bot/init.go @@ -24,5 +24,8 @@ func BotInit() { if Cfg.Bot.WgpSecBot.Enabled { register.RegisterBot(&WgpSecBot{}) } + if Cfg.Bot.CqHttpBot.Enabled { + register.RegisterBot(&CqHttpBot{}) + } } diff --git a/config/config.go b/config/config.go index a0e7123..d033ccb 100644 --- a/config/config.go +++ b/config/config.go @@ -106,6 +106,13 @@ func DefaultConfig() Config { Key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", Timeout: 2, }, + CqHttpBot: CqHttpBotStruct{ + Enabled: false, + Api: "http://xxxxxx.com/send", + QQGroup: 000000000, + Key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + Timeout: 2, + }, }, } } diff --git a/config/type.go b/config/type.go index 746a276..bc206af 100644 --- a/config/type.go +++ b/config/type.go @@ -47,6 +47,7 @@ type BotStruct struct { HexQBot HexQBotStruct `yaml:"HexQBot"` ServerChan ServerChanStruct `yaml:"ServerChan"` WgpSecBot WgpSecBotStruct `yaml:"WgpSecBot"` + CqHttpBot CqHttpBotStruct `yaml:"CqHttpBot"` } type WecomBotStruct struct { @@ -87,6 +88,14 @@ type WgpSecBotStruct struct { Timeout uint8 `yaml:"timeout"` } +type CqHttpBotStruct struct { + Enabled bool `yaml:"enabled"` + Api string `yaml:"api"` + QQGroup uint64 `yaml:"qqgroup"` + Key string `yaml:"key"` + Timeout uint8 `yaml:"timeout"` +} + type EdgeForumStruct struct { Enabled bool `yaml:"enabled"` }