소스 검색

remove to Options

joe 4 년 전
부모
커밋
5e2f061094

+ 9 - 12
beaconfire.go

@@ -3,7 +3,6 @@ package beaconfire
 import (
 	"errors"
 	"fmt"
-	"time"
 )
 
 var (
@@ -11,35 +10,33 @@ var (
 )
 
 type BeaconParam struct {
-	From, Title, Content string
-	Ts                   int64
-	To                   []string
-	Fmt                  string
+	Title   string
+	Content string
+	Ts      int64
+	Fmt     string
 }
 
-func NewBeaconParam(from, title, content string, fmt string, to ...string) *BeaconParam {
+func NewBeaconParam(title, content string, ts int64, fmt string) *BeaconParam {
 	bp := &BeaconParam{
-		From:    from,
 		Title:   title,
 		Content: content,
-		Ts:      time.Now().Unix(),
+		Ts:      ts,
 		Fmt:     fmt,
 	}
 
-	bp.To = append(bp.To, to...)
 	return bp
 }
 
 func (bp BeaconParam) FormatMarkdown() string {
-	return fmt.Sprintf("### %v\n> %v\n> %v\n> %v", bp.Title, bp.From, Ts2Str(bp.Ts), bp.Content)
+	return fmt.Sprintf("### %v\n> %v\n> %v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
 func (bp BeaconParam) FormatHTML() string {
-	return fmt.Sprintf("<b>%v</b>\n<i>%v</i>\n<i>%v</i>\n%v", bp.Title, bp.From, Ts2Str(bp.Ts), bp.Content)
+	return fmt.Sprintf("<b>%v</b>\n<i>%v</i>\n%v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
 func (bp BeaconParam) FormatPlainText() string {
-	return fmt.Sprintf("%v\n%v\n%v\n%v", bp.Title, bp.From, Ts2Str(bp.Ts), bp.Content)
+	return fmt.Sprintf("%v\n%v\n%v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
 type BeaconFire interface {

+ 5 - 2
providers/dingtalk.go

@@ -18,7 +18,8 @@ const (
 )
 
 type OptionsDingtalk struct {
-	AccessToken string `envconfig:"ACCESS_TOKEN"`
+	AccessToken string
+	To          []string // userid
 }
 
 type dingtalk struct {
@@ -42,14 +43,16 @@ func (c *dingtalk) Send(bp *beaconfire.BeaconParam) error {
 	msgType := _DINGTALK_DEFAULT_MSGTYPE
 
 	url := fmt.Sprintf("%s?access_token=%s", _DINGTALK_ENDPOINT, c.opt.AccessToken)
+
 	content := bp.FormatMarkdown()
+
 	values := map[string]interface{}{
 		"msgtype": msgType,
 		msgType: map[string]interface{}{
 			"content": content,
 		},
 		"at": map[string]interface{}{
-			"atUserIds": bp.To,
+			"atUserIds": c.opt.To,
 			"isAtAll":   false,
 		},
 	}

+ 3 - 2
providers/discord.go

@@ -11,6 +11,7 @@ const (
 
 type OptionsDiscord struct {
 	AuthToken string
+	To        []string
 }
 
 type discord struct {
@@ -30,7 +31,7 @@ func (c discord) Name() string {
 }
 
 func (c *discord) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
@@ -44,7 +45,7 @@ func (c *discord) Send(bp *beaconfire.BeaconParam) error {
 	}
 	defer discord.Close()
 
-	for _, channel := range bp.To {
+	for _, channel := range c.opt.To {
 		if _, err := discord.ChannelMessageSend(channel, bp.Content); err != nil {
 			return err
 		}

+ 4 - 2
providers/mailgun.go

@@ -14,6 +14,8 @@ type OptionsMailgunfire struct {
 	Domain          string
 	ApiKey          string
 	DisableTracking bool
+	From            string
+	To              []string
 }
 
 type mailgunfire struct {
@@ -31,7 +33,7 @@ func (c mailgunfire) Name() string {
 }
 
 func (c *mailgunfire) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) == 0 {
+	if len(c.opt.To) == 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
@@ -44,7 +46,7 @@ func (c *mailgunfire) Send(bp *beaconfire.BeaconParam) error {
 		}
 	}
 
-	msg := mg.NewMessage(bp.From, bp.Title, content, bp.To...)
+	msg := mg.NewMessage(c.opt.From, bp.Title, content, c.opt.To...)
 	if inHtml {
 		msg.SetHtml(content)
 	}

+ 3 - 3
providers/mattermost.go

@@ -15,7 +15,7 @@ type OptionsMattermost struct {
 	HookId  string
 	IconUrl string
 	BotName string
-	// Channel []string
+	To      []string
 }
 
 type mattermost struct {
@@ -42,13 +42,13 @@ type message struct {
 }
 
 func (c *mattermost) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
 	u := fmt.Sprintf("%s/hooks/%s", c.opt.Url, c.opt.HookId)
 
-	for _, channel := range bp.To {
+	for _, channel := range c.opt.To {
 
 		m := message{
 			Channel:  channel,

+ 3 - 2
providers/slack.go

@@ -11,6 +11,7 @@ const SLACKFIRE_NAME = "slackfire"
 
 type OptionsSlack struct {
 	AuthToken string
+	To        []string
 }
 
 type slackfire struct {
@@ -30,12 +31,12 @@ func (c slackfire) Name() string {
 }
 
 func (c *slackfire) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
 	s := slack.New(c.opt.AuthToken)
-	for _, channel := range bp.To {
+	for _, channel := range c.opt.To {
 		if _, _, err := s.PostMessageContext(
 			context.TODO(),
 			channel,

+ 4 - 3
providers/smtp.go

@@ -15,6 +15,7 @@ type OptionsSmtp struct {
 	InsecureSkipVerify bool
 	Username           string
 	Password           string
+	To                 []string
 }
 
 type smtp struct {
@@ -34,13 +35,13 @@ func (c *smtp) Name() string {
 }
 
 func (c *smtp) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
 	mail := gomail.NewMessage()
-	mail.SetHeader("From", bp.From)
-	mail.SetHeader("To", bp.To...)
+	mail.SetHeader("From", c.opt.Username)
+	mail.SetHeader("To", c.opt.To...)
 	mail.SetHeader("Subject", bp.Title)
 	inHtml := true // format == "html"
 	content := bp.FormatHTML()

+ 1 - 2
providers/smtp_test.go

@@ -33,10 +33,9 @@ func TestSmtp(t *testing.T) {
 		InsecureSkipVerify: false,
 		Username:           username,
 		Password:           password,
+		To:                 []string{TO},
 	})
 	err := bf.Send(&beaconfire.BeaconParam{
-		From:    FROM,
-		To:      []string{TO},
 		Ts:      time.Now().Unix(),
 		Title:   "test",
 		Content: "cfdfgddfdfdf",

+ 3 - 2
providers/telegram.go

@@ -18,6 +18,7 @@ const (
 
 type OptionsTelegram struct {
 	Token string
+	To    []string // userId
 	// Channel []string
 	// ParseMode string
 }
@@ -39,7 +40,7 @@ func (c *telegram) Name() string {
 }
 
 func (c *telegram) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
@@ -50,7 +51,7 @@ func (c *telegram) Send(bp *beaconfire.BeaconParam) error {
 	v.Set("parse_mode", _TELEGRAM_DEF_PARSE_MODE)
 	v.Set("text", content)
 
-	for _, channel := range bp.To {
+	for _, channel := range c.opt.To {
 
 		v.Set("chat_id", channel)
 

+ 1 - 2
providers/telegram_test.go

@@ -23,10 +23,9 @@ func TestTelegram(t *testing.T) {
 
 	bf := NewTelegram(&OptionsTelegram{
 		Token: token,
+		To:    []string{TO},
 	})
 	err := bf.Send(&beaconfire.BeaconParam{
-		From:    "sender",
-		To:      []string{TO},
 		Ts:      time.Now().Unix(),
 		Title:   "title",
 		Content: "new Message",

+ 5 - 3
providers/twilio.go

@@ -15,6 +15,8 @@ const (
 type OptionsTwilio struct {
 	AccountSid string
 	AuthToken  string
+	From       string
+	To         []string
 }
 
 type twilio struct {
@@ -34,7 +36,7 @@ func (c twilio) Name() string {
 }
 
 func (c *twilio) Send(bp *beaconfire.BeaconParam) error {
-	if len(bp.To) <= 0 {
+	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}
 
@@ -61,10 +63,10 @@ func (c *twilio) Send(bp *beaconfire.BeaconParam) error {
 	urlStr := fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%v/Messages.json", c.opt.AccountSid)
 
 	v := url.Values{}
-	v.Set("From", bp.From)
+	v.Set("From", c.opt.From)
 	v.Set("Body", bp.Content)
 
-	for _, receiver := range bp.To {
+	for _, receiver := range c.opt.To {
 		v.Set("To", receiver)
 
 		_, err := beaconfire.Request(http.MethodPost, urlStr, []byte(v.Encode()), map[string]string{

+ 3 - 2
providers/workwx.go

@@ -20,7 +20,8 @@ const (
 )
 
 type OptionsWorkwx struct {
-	Key string
+	Key string   // robot key
+	To  []string // mention_list
 }
 
 type workwx struct {
@@ -52,7 +53,7 @@ func (c *workwx) Send(bp *beaconfire.BeaconParam) error {
 		"msgtype": msgtype,
 		msgtype: map[string]interface{}{
 			"content":        content,
-			"mentioned_list": bp.To,
+			"mentioned_list": c.opt.To,
 		},
 	}
 

+ 0 - 2
providers/workwx_test.go

@@ -22,8 +22,6 @@ func TestWorkwx(t *testing.T) {
 	})
 
 	bf.Send(&beaconfire.BeaconParam{
-		From:    "sender",
-		To:      []string{""},
 		Ts:      time.Now().Unix(),
 		Title:   "title",
 		Content: "test message",

+ 2 - 1
sample/main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"fmt"
+	"time"
 
 	"git.wenlab.co/joe/beaconfire"
 	"git.wenlab.co/joe/beaconfire/providers"
@@ -31,7 +32,7 @@ func main() {
 			fmt.Println("telegram sending ...")
 		}
 		// send
-		err = bf.Send(beaconfire.NewBeaconParam("sender", "subject", "im a message", "", "receiver1", "receiver2"))
+		err = bf.Send(beaconfire.NewBeaconParam("subject", "im a message", time.Now().Unix(), ""))
 		if err != nil {
 			// log
 			fmt.Println("Error:", err)