Selaa lähdekoodia

增加钉钉机器人推送

main
pppscn 4 vuotta sitten
vanhempi
commit
d6e4487349
  1. 9
      conf.ini
  2. 4
      go.mod
  3. 4
      jd_seckill/seckill.go
  4. 35
      service/dingtalk.go
  5. 24
      service/service.go

9
conf.ini

@ -53,4 +53,11 @@ port =
# 邮箱地址 xxxxxxxx@xx.com
email_user =
# 邮箱授权码(并不一定是邮箱密码) xxxxxxxxxxxxxxxx
email_pwd =
email_pwd =
#钉钉机器人配置
[dingtalk]
# 机器人的Webhook地址(https://oapi.dingtalk.com/robot/send?access_token=XXXXXX)的access_token
access_token =
# 密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串
secret =

4
go.mod

@ -5,14 +5,18 @@ go 1.15
require (
github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6
github.com/PuerkitoBio/goquery v1.6.0
github.com/blinkbean/dingtalk v0.0.0-20201231030509-45a553a84503
github.com/chromedp/cdproto v0.0.0-20201204063249-be40c824ad18
github.com/chromedp/chromedp v0.5.4
github.com/gobwas/httphead v0.1.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/spf13/cobra v1.1.1
github.com/tidwall/gjson v1.6.7
github.com/unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 // indirect
golang.org/x/text v0.3.4
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

4
jd_seckill/seckill.go

@ -63,7 +63,9 @@ func (this *Seckill) MakeReserve() {
reserveUrl := gjson.Get(body, "url").String()
req = httpc.NewRequest(this.client)
_, _, _ = req.SetUrl("https:" + reserveUrl).SetMethod("get").Send().End()
log.Println("预约成功,已获得抢购资格 / 您已成功预约过了,无需重复预约")
msg := "预约成功,已获得抢购资格 / 您已成功预约过了,无需重复预约![我的预约](https://yushou.jd.com/member/qualificationList.action)"
_ = service.SendMessage(this.conf, "茅台抢购通知", msg)
log.Println(msg)
}
}

35
service/dingtalk.go

@ -0,0 +1,35 @@
package service
import (
"github.com/blinkbean/dingtalk"
"github.com/unknwon/goconfig"
"log"
)
type Dingtalk struct {
conf *goconfig.ConfigFile
}
func NewDingtalk(conf *goconfig.ConfigFile) *Dingtalk {
return &Dingtalk{conf: conf}
}
func (this *Dingtalk) Send(title, msg string) error {
cli := dingtalk.InitDingTalkWithSecret(
this.conf.MustValue("dingtalk", "access_token", ""),
this.conf.MustValue("dingtalk", "secret", ""),
)
markdown := []string{
"### " + title,
"---------",
msg,
}
err := cli.SendMarkDownMessageBySlice(title, markdown)
if err != nil {
log.Println(err)
} else {
log.Println("钉钉机器人推送成功")
}
return nil
}

24
service/service.go

@ -2,20 +2,26 @@ package service
import "github.com/unknwon/goconfig"
func SendMessage(conf *goconfig.ConfigFile,title,msg string) error {
if conf.MustValue("messenger","enable","false")=="true" {
func SendMessage(conf *goconfig.ConfigFile, title, msg string) error {
if conf.MustValue("messenger", "enable", "false") == "true" {
//钉钉机器人
if conf.MustValue("messenger", "type", "none") == "dingtalk" {
dingtalk := NewDingtalk(conf)
err := dingtalk.Send(title, msg)
return err
}
//邮件发送
if conf.MustValue("messenger","type","none")=="smtp" {
email:=NewEmail(conf)
err:=email.Send([]string{conf.MustValue("messenger","email","")},title,msg)
if conf.MustValue("messenger", "type", "none") == "smtp" {
email := NewEmail(conf)
err := email.Send([]string{conf.MustValue("messenger", "email", "")}, title, msg)
return err
}
//Server酱推送
if conf.MustValue("messenger","type","none")=="wechat" {
wechat:=NewWechat(conf)
err:=wechat.Send(title,msg)
if conf.MustValue("messenger", "type", "none") == "wechat" {
wechat := NewWechat(conf)
err := wechat.Send(title, msg)
return err
}
}
return nil
}
}

Ladataan…
Peruuta
Tallenna