-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for discord notifications (#110)
* add: functionality for discord bot This was acheived using what seems to be the most well update/documented library for discord in go. https://github.com/bwmarrin/discordgo * rename: botapikey to botapitoken for discord config * document: discord notification configuration * fix: moved message_template documentation back to proper place Co-authored-by: Chris Smith <[email protected]>
- Loading branch information
1 parent
31d1158
commit 1605a59
Showing
6 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package notify | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/TimothyYe/godns" | ||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
type DiscordNotify struct { | ||
conf *godns.Settings | ||
} | ||
|
||
func NewDiscordNotify(conf *godns.Settings) INotify { | ||
return &DiscordNotify{conf: conf} | ||
} | ||
|
||
func (n *DiscordNotify) Send(domain, currentIP string) error { | ||
|
||
if n.conf.Notify.Discord.BotApiToken == "" { | ||
return errors.New("bot api token cannot be empty") | ||
} | ||
|
||
if n.conf.Notify.Discord.Channel == "" { | ||
return errors.New("channel id cannot be empty") | ||
} | ||
|
||
tpl := n.conf.Notify.Discord.MsgTemplate | ||
if tpl == "" { | ||
tpl = "Your IP address for {{.Domain}} has been updated to {{ .CurrentIP }} " | ||
} | ||
msg := buildTemplate(currentIP, domain, tpl) | ||
|
||
//Create discordgo client | ||
d, err := discordgo.New("Bot " + n.conf.Notify.Discord.BotApiToken) | ||
if err != nil { | ||
return errors.New("error creating discord bot") | ||
} | ||
//Open socket connection | ||
err = d.Open() | ||
if err != nil { | ||
return errors.New("error opening connection,") | ||
} | ||
//Send message | ||
_, err = d.ChannelMessageSend(n.conf.Notify.Discord.Channel, msg) | ||
if err != nil { | ||
return errors.New("error sending message") | ||
} | ||
//Close socket connection | ||
err = d.Close() | ||
if err != nil { | ||
return errors.New("error closing discord connection") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters