Skip to content

Commit

Permalink
chore: add discord
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeAndTwo committed Feb 3, 2022
1 parent 7c7bb5a commit bd11072
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 7 deletions.
70 changes: 70 additions & 0 deletions discord/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package discord

import (
"encoding/json"
"errors"
"github.com/imroc/req"
)

var (
ApiURL = "https://discord.com/api/webhooks/"
)

type Options struct {
Token string `json:"token"`
Channel string `json:"channel"`
Text string `json:"text"`
}

type client struct {
opt Options
}

func New(opt Options) *client {
return &client{opt: opt}
}

type Resp struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}

type Webhook struct {
Content string `json:"content"`
}

func (c *client) Send(message string) error {
if "" == c.opt.Token {
return errors.New("missing token")
}

if "" == c.opt.Channel {
return errors.New("missing channel")
}

if "" == message {
return errors.New("missing message")
}
c.opt.Text = message

whMsg := &Webhook{
Content: c.opt.Text,
}

inrec, _ := json.Marshal(whMsg)
params := &req.Param{}
json.Unmarshal(inrec, params)

ApiURL = ApiURL + c.opt.Channel + "/" + c.opt.Token
resp, err := req.Post(ApiURL, *params)
if err != nil {
return nil
}
r := &Resp{}
err = resp.ToJSON(r)

if err != nil || err.Error() != "unexpected end of JSON input" {
return err
}
return nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/deng00/go-notify

go 1.15

require github.com/imroc/req v0.3.0
require (
github.com/bwmarrin/discordgo v0.23.2 // indirect
github.com/imroc/req v0.3.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4=
github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U=
github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
17 changes: 15 additions & 2 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notify

import (
"github.com/deng00/go-notify/discord"
"github.com/deng00/go-notify/pagerduty"
"github.com/deng00/go-notify/pushover"
"github.com/deng00/go-notify/slack"
Expand All @@ -13,6 +14,7 @@ const (
PlatformPushover = "pushover"
PlatformDingDing = "dingding"
Platformpagerduty = "pagerduty"
PaltformDiscord = "discord"
)

type Notify struct {
Expand Down Expand Up @@ -41,6 +43,8 @@ func (n *Notify) Send(msg string) error {
return n.sendSlackNotify(msg)
case Platformpagerduty:
return n.sendPagerdutyNotify(msg)
case PaltformDiscord:
return n.sendDiscordNotify(msg)
default:
panic("not supported notify platform")
}
Expand All @@ -67,10 +71,19 @@ func (n *Notify) sendSlackNotify(msg string) error {

func (n *Notify) sendPagerdutyNotify(msg string) error {
app := pagerduty.New(pagerduty.Options{
Token: n.config.Token,
Source: n.config.Source,
Token: n.config.Token,
Source: n.config.Source,
Severity: n.config.Severity,
})
err := app.Send(msg)
return err
}

func (n *Notify) sendDiscordNotify(msg string) error {
app := discord.New(discord.Options{
Token: n.config.Token,
Channel: n.config.Channel,
})
err := app.Send(msg)
return err
}
19 changes: 15 additions & 4 deletions notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestNotify_Send(t *testing.T) {
"test pagerduty severity is null",
fields{config: &Config{
Platform: Platform("pagerduty"),
Token: os.Getenv("PAGERDUTY_TOKEN"),
Source: "api-test",
Token: os.Getenv("PAGERDUTY_TOKEN"),
Source: "api-test",
Severity: "",
}},
args{msg: "test pagerduty"},
Expand All @@ -49,12 +49,23 @@ func TestNotify_Send(t *testing.T) {
"test pagerduty severity is error",
fields{config: &Config{
Platform: Platform("pagerduty"),
Token: os.Getenv("PAGERDUTY_TOKEN"),
Source: "api-test",
Token: os.Getenv("PAGERDUTY_TOKEN"),
Source: "api-test",
Severity: "error",
}},
args{msg: "test pagerduty is error"},
},
{
"test discord notify",
fields{
config: &Config{
Platform: Platform("discord"),
Token: os.Getenv("DISCORD_TOKEN"),
Channel: os.Getenv("DISCORD_CHANNEL"),
},
},
args{msg: "test case"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit bd11072

Please sign in to comment.