ztino
4 years ago
5 changed files with 75 additions and 21 deletions
@ -0,0 +1,21 @@ |
|||
package service |
|||
|
|||
import "github.com/unknwon/goconfig" |
|||
|
|||
func SendMessage(conf *goconfig.ConfigFile,title,msg string) error { |
|||
if conf.MustValue("messenger","enable","false")=="true" { |
|||
//邮件发送
|
|||
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) |
|||
return err |
|||
} |
|||
} |
|||
return nil |
|||
} |
@ -0,0 +1,37 @@ |
|||
package service |
|||
|
|||
import ( |
|||
"errors" |
|||
"fmt" |
|||
"github.com/Albert-Zhan/httpc" |
|||
"github.com/tidwall/gjson" |
|||
"github.com/unknwon/goconfig" |
|||
"log" |
|||
"net/http" |
|||
) |
|||
|
|||
type Wechat struct { |
|||
conf *goconfig.ConfigFile |
|||
} |
|||
|
|||
func NewWechat(conf *goconfig.ConfigFile) *Wechat { |
|||
return &Wechat{conf: conf} |
|||
} |
|||
|
|||
func (this *Wechat) Send(title,msg string) error { |
|||
client:=httpc.NewHttpClient() |
|||
req:=httpc.NewRequest(client) |
|||
url:=fmt.Sprintf("http://sc.ftqq.com/%s.send",this.conf.MustValue("messenger","server_chan_sckey","")) |
|||
req.SetHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36") |
|||
resp,body,err:=req.SetUrl(url+"?text="+title+"&desp="+msg).SetMethod("get").Send().End() |
|||
if err!=nil || resp.StatusCode!=http.StatusOK { |
|||
log.Println("微信推送失败,网络错误") |
|||
return errors.New("微信推送失败,网络错误") |
|||
} |
|||
if gjson.Get(body,"errno").Int()!=0 { |
|||
log.Println("微信推送失败,返回错误:"+gjson.Get(body,"errmsg").String()) |
|||
return errors.New("微信推送失败,返回错误:"+gjson.Get(body,"errmsg").String()) |
|||
} |
|||
log.Println("微信推送成功") |
|||
return nil |
|||
} |
Loading…
Reference in new issue