Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions bot/CqHttpBot.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions bot/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ func BotInit() {
if Cfg.Bot.WgpSecBot.Enabled {
register.RegisterBot(&WgpSecBot{})
}
if Cfg.Bot.CqHttpBot.Enabled {
register.RegisterBot(&CqHttpBot{})
}

}
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down
9 changes: 9 additions & 0 deletions config/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
}
Expand Down