joe 3 years ago
parent
commit
fbecd55d8b

+ 1 - 1
README.md

@@ -24,4 +24,4 @@ I also recommend you:
 - [notify](https://github.com/gomodules/notify)
 > First what i used.
 
-For me, both of them doesnt support Chinese social app bot, maybe i will pull requests for them someday.
+For me, both of them doesnt support Chinese social app bot, maybe i will submit pull requests for them.

+ 17 - 10
beaconfire.go

@@ -5,19 +5,25 @@ import (
 	"fmt"
 )
 
+const (
+	BEACON_FMT_HTML     = "html"
+	BEACON_FMT_MARKDOWN = "markdown"
+	BEACON_FMT_PLAIN    = "plain"
+)
+
 var (
 	ErrNoReceiver = errors.New("no receiver")
 )
 
-type BeaconParam struct {
-	Title   string
-	Content string
-	Ts      int64
+type BeaconMessage struct {
+	Title   string // Message Title
+	Content string // Message Content
+	Ts      int64  //..
 	Fmt     string
 }
 
-func NewBeaconParam(title, content string, ts int64, fmt string) *BeaconParam {
-	bp := &BeaconParam{
+func NewBeaconMessage(title, content string, ts int64, fmt string) *BeaconMessage {
+	bp := &BeaconMessage{
 		Title:   title,
 		Content: content,
 		Ts:      ts,
@@ -27,19 +33,20 @@ func NewBeaconParam(title, content string, ts int64, fmt string) *BeaconParam {
 	return bp
 }
 
-func (bp BeaconParam) FormatMarkdown() string {
+func (bp BeaconMessage) FormatMarkdown() string {
 	return fmt.Sprintf("### %v\n> %v\n> %v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
-func (bp BeaconParam) FormatHTML() string {
+func (bp BeaconMessage) FormatHTML() string {
 	return fmt.Sprintf("<b>%v</b>\n<i>%v</i>\n%v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
-func (bp BeaconParam) FormatPlainText() string {
+func (bp BeaconMessage) FormatPlainText() string {
 	return fmt.Sprintf("%v\n%v\n%v", bp.Title, Ts2Str(bp.Ts), bp.Content)
 }
 
+// Interface of BeaconFire
 type BeaconFire interface {
 	Name() string
-	Send(*BeaconParam) error
+	Send(*BeaconMessage) error
 }

+ 9 - 0
httpclient.go

@@ -19,6 +19,15 @@ func PostJson(url string, body []byte) ([]byte, error) {
 	return Request(http.MethodPost, url, body, map[string]string{"Content-Type": "application/json"}, "", "")
 }
 
+// Delivery a HTTP request
+//
+// @method: http Method, eg. http.MethodGet
+// @url: endpoint
+// @body: post body
+// @headers: request headers
+// @usename: basic auth
+// @password: basic auth
+// @return: response, error
 func Request(method string, url string, body []byte, headers map[string]string, username, password string) ([]byte, error) {
 	client := http.Client{
 		Timeout: time.Second * 5,

+ 3 - 3
providers/aliyun.go

@@ -5,7 +5,7 @@ import "git.wenlab.co/joe/beaconfire"
 // aliyun SMS support
 
 const (
-	ALIYUN_NAME = "aliyun"
+	BEACON_NAME_ALIYUN = "aliyun"
 )
 
 type OptionsAliyun struct {
@@ -24,9 +24,9 @@ func NewAliyun(opt *OptionsAliyun) *aliyun {
 }
 
 func (a *aliyun) Name() string {
-	return ALIYUN_NAME
+	return BEACON_NAME_ALIYUN
 }
 
-func (a *aliyun) Send(bp *beaconfire.BeaconParam) error {
+func (a *aliyun) Send(bp *beaconfire.BeaconMessage) error {
 	return nil
 }

+ 3 - 3
providers/dingtalk.go

@@ -14,7 +14,7 @@ const (
 	_DINGTALK_ENDPOINT        = "https://oapi.dingtalk.com/robot/send"
 	_DINGTALK_DEFAULT_MSGTYPE = "markdown" // this type would be lcompatible with others easily
 
-	DINGTALK_NAME = "dingtalk"
+	BEACON_NAME_DINGTALK = "dingtalk"
 )
 
 type OptionsDingtalk struct {
@@ -35,10 +35,10 @@ func NewDingtalk(opt *OptionsDingtalk) *dingtalk {
 }
 
 func (c dingtalk) Name() string {
-	return DINGTALK_NAME
+	return BEACON_NAME_DINGTALK
 }
 
-func (c *dingtalk) Send(bp *beaconfire.BeaconParam) error {
+func (c *dingtalk) Send(bp *beaconfire.BeaconMessage) error {
 
 	msgType := _DINGTALK_DEFAULT_MSGTYPE
 

+ 3 - 3
providers/discord.go

@@ -6,7 +6,7 @@ import (
 )
 
 const (
-	DISCORD_NAME = "discord"
+	BEACON_NAME_DISCORD = "discord"
 )
 
 type OptionsDiscord struct {
@@ -27,10 +27,10 @@ func NewDiscord(opt *OptionsDiscord) *discord {
 }
 
 func (c discord) Name() string {
-	return DISCORD_NAME
+	return BEACON_NAME_DISCORD
 }
 
-func (c *discord) Send(bp *beaconfire.BeaconParam) error {
+func (c *discord) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 6 - 6
providers/mailgun.go

@@ -7,10 +7,10 @@ import (
 )
 
 const (
-	MAILGUNFIRE_NAME = "mailgunfire"
+	BEACON_NAME_MAILGUN = "mailgun"
 )
 
-type OptionsMailgunfire struct {
+type OptionsMailgun struct {
 	Domain          string
 	ApiKey          string
 	DisableTracking bool
@@ -19,20 +19,20 @@ type OptionsMailgunfire struct {
 }
 
 type mailgunfire struct {
-	opt *OptionsMailgunfire
+	opt *OptionsMailgun
 }
 
 var _ beaconfire.BeaconFire = &mailgunfire{}
 
-func NewMailgunfire(opt *OptionsMailgunfire) *mailgunfire {
+func NewMailgunfire(opt *OptionsMailgun) *mailgunfire {
 	return &mailgunfire{opt: opt}
 }
 
 func (c mailgunfire) Name() string {
-	return MAILGUNFIRE_NAME
+	return BEACON_NAME_MAILGUN
 }
 
-func (c *mailgunfire) Send(bp *beaconfire.BeaconParam) error {
+func (c *mailgunfire) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) == 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 3 - 3
providers/mattermost.go

@@ -8,7 +8,7 @@ import (
 	"git.wenlab.co/joe/beaconfire"
 )
 
-const MATTERMOST_NAME = "mattermost"
+const BEACON_NAME_MATTERMOST = "mattermost"
 
 type OptionsMattermost struct {
 	Url     string
@@ -31,7 +31,7 @@ func NewMattermost(opt *OptionsMattermost) *mattermost {
 }
 
 func (c mattermost) Name() string {
-	return MATTERMOST_NAME
+	return BEACON_NAME_MATTERMOST
 }
 
 type message struct {
@@ -41,7 +41,7 @@ type message struct {
 	Text     string `json:"text"`
 }
 
-func (c *mattermost) Send(bp *beaconfire.BeaconParam) error {
+func (c *mattermost) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 3 - 3
providers/slack.go

@@ -7,7 +7,7 @@ import (
 	"github.com/slack-go/slack"
 )
 
-const SLACKFIRE_NAME = "slackfire"
+const BEACON_NAME_SLACK = "slack"
 
 type OptionsSlack struct {
 	AuthToken string
@@ -27,10 +27,10 @@ func NewSlackfire(opt *OptionsSlack) *slackfire {
 }
 
 func (c slackfire) Name() string {
-	return SLACKFIRE_NAME
+	return BEACON_NAME_SLACK
 }
 
-func (c *slackfire) Send(bp *beaconfire.BeaconParam) error {
+func (c *slackfire) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 3 - 3
providers/smtp.go

@@ -7,7 +7,7 @@ import (
 	gomail "gopkg.in/gomail.v2"
 )
 
-const SMTP_NAME = "smtp"
+const BEACON_NAME_SMTP = "smtp"
 
 type OptionsSmtp struct {
 	Host               string
@@ -31,10 +31,10 @@ func NewSmtp(opt *OptionsSmtp) *smtp {
 }
 
 func (c *smtp) Name() string {
-	return SMTP_NAME
+	return BEACON_NAME_SMTP
 }
 
-func (c *smtp) Send(bp *beaconfire.BeaconParam) error {
+func (c *smtp) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 1 - 1
providers/smtp_test.go

@@ -35,7 +35,7 @@ func TestSmtp(t *testing.T) {
 		Password:           password,
 		To:                 []string{TO},
 	})
-	err := bf.Send(&beaconfire.BeaconParam{
+	err := bf.Send(&beaconfire.BeaconMessage{
 		Ts:      time.Now().Unix(),
 		Title:   "test",
 		Content: "cfdfgddfdfdf",

+ 4 - 4
providers/telegram.go

@@ -11,9 +11,9 @@ import (
 )
 
 const (
-	TELEGRAM_NAME = "telegram"
-
 	_TELEGRAM_DEF_PARSE_MODE = "HTML"
+
+	BEACON_NAME_TELEGRAM = "telegram"
 )
 
 type OptionsTelegram struct {
@@ -36,10 +36,10 @@ func NewTelegram(opt *OptionsTelegram) *telegram {
 }
 
 func (c *telegram) Name() string {
-	return TELEGRAM_NAME
+	return BEACON_NAME_TELEGRAM
 }
 
-func (c *telegram) Send(bp *beaconfire.BeaconParam) error {
+func (c *telegram) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 1 - 1
providers/telegram_test.go

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

+ 3 - 3
providers/twilio.go

@@ -9,7 +9,7 @@ import (
 )
 
 const (
-	TWILIO_NAME = "twilio"
+	BEACON_NAME_TWILIO = "twilio"
 )
 
 type OptionsTwilio struct {
@@ -32,10 +32,10 @@ func NewTwilio(opt *OptionsTwilio) *twilio {
 }
 
 func (c twilio) Name() string {
-	return TWILIO_NAME
+	return BEACON_NAME_TWILIO
 }
 
-func (c *twilio) Send(bp *beaconfire.BeaconParam) error {
+func (c *twilio) Send(bp *beaconfire.BeaconMessage) error {
 	if len(c.opt.To) <= 0 {
 		return beaconfire.ErrNoReceiver
 	}

+ 3 - 3
providers/workwx.go

@@ -16,7 +16,7 @@ const (
 	_WORKWX_ENDPOINT         = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
 	_WORKWX_MSGTYPE_MARKDOWN = "markdown" // this type would be lcompatible with others easily
 
-	WORKWX_NAME = "workwx"
+	BEACON_NAME_WORKWX = "workwx"
 )
 
 type OptionsWorkwx struct {
@@ -37,10 +37,10 @@ func NewWorkWx(opt *OptionsWorkwx) *workwx {
 }
 
 func (c *workwx) Name() string {
-	return WORKWX_NAME
+	return BEACON_NAME_WORKWX
 }
 
-func (c *workwx) Send(bp *beaconfire.BeaconParam) error {
+func (c *workwx) Send(bp *beaconfire.BeaconMessage) error {
 	msgtype := _WORKWX_MSGTYPE_MARKDOWN
 	// modify content
 

+ 1 - 1
providers/workwx_test.go

@@ -21,7 +21,7 @@ func TestWorkwx(t *testing.T) {
 		Key: key,
 	})
 
-	bf.Send(&beaconfire.BeaconParam{
+	bf.Send(&beaconfire.BeaconMessage{
 		Ts:      time.Now().Unix(),
 		Title:   "title",
 		Content: "test message",

+ 2 - 2
sample/main.go

@@ -28,11 +28,11 @@ func main() {
 	for _, bf := range bfs {
 		// checker original
 		name = bf.Name()
-		if name == providers.TELEGRAM_NAME {
+		if name == providers.BEACON_NAME_TELEGRAM {
 			fmt.Println("telegram sending ...")
 		}
 		// send
-		err = bf.Send(beaconfire.NewBeaconParam("subject", "im a message", time.Now().Unix(), ""))
+		err = bf.Send(beaconfire.NewBeaconMessage("subject", "im a message", time.Now().Unix(), ""))
 		if err != nil {
 			// log
 			fmt.Println("Error:", err)

+ 1 - 0
utils.go

@@ -2,6 +2,7 @@ package beaconfire
 
 import "time"
 
+// Translate Unix timestamp to string formated with 'YYYY-mm-dd HH:MM:SS'
 func Ts2Str(ts int64) string {
 	return time.Unix(ts, 0).Format("2006-01-02 15:04:05")
 }