Notify Go is a free command line / API that allows you to easily send (bot style) messages to a recipient . This features a base implementation for all your favourite messaging platforms
like :
- telegram
- discord
- slack
Note: this is not a 2 way messaging protocol . This project only publishes messages
Also , I would like to mention , Slack and Discord both use webhooks
. So if you just want to use those and have simple needs . NO NEED TO USE THIS SOLUTION it's overkill. Just Send an http request to the webhook route with the body. ie curl it.
It's aimed to be super minimal and simple to configure. This solution is great for the following scenarios :
- Command Line Notifications
- System Notifications
- IOT Devices
- Home Automation
- Anything that can run linux .
Table of contents
- Usage
- Installation
- Authorization Setup -- you cannot skip this step
- API Usage
- Cli Usage
To use the API in your existing go code simply run the following
go get -u github.com/baderkha/notify-go
-
go to release page
-
download the binary that matches your system
-
[windows instrructions]
- if on windows , then just download the .msi file
- install it on your computer
-
[unix instructions] move to path
Linux
move binary to /usr/local/bin
sudo mv ~/Downloads/notify-go-linux-x86 /usr/local/bin/notify-go sudo chmod +x /usr/local/bin/notify-go
Mac
move binary to /usr/local/bin
sudo mv ~/Downloads/notify-go-linux-x86 /usr/local/bin/notify-go sudo chmod +x /usr/local/bin/notify-go
*Note you must do this in order to use the cli/api*
This section will cover how to setup authorization for each of the message senders . Note that
- Slack
- you need to create an app
- give it permissions
- create a webhook
- use that webhook with this cli / api
- Discord
- go to your server settings
- under integrations (create a webhook)
- call it a cool name
- use that webhook with this cli / api
- Telegram
- // todo
This section will cover how to use the api.
Note You Need Go v1.18+ Since The API uses generics
notify.MessageSender
is an interface which has implementations for slack,discord,and telegram. If your usage is simple and you only want to message Discord for example . Then attaching this interface is perfect for you.
- init
slackSender := notify.NewSlackSender()
- send a message to a webhook
err := slackSender.Send("https://slack.channel.com/your/other/channel/webhook",[]byte("some_message"))
if err != nil {
log.Fatal(err)
}
Discord is very similar to slack since it also uses webhooks to send messages to a chat . So the init logic is the same .
- init
discSender := notify.NewDiscordSender()
- send a message to a different webhook
err := discSender.Send("https://slack.channel.com/your/other/channel/webhook",[]byte("somemessage"))
if err != nil {
log.Fatal(err)
}
notify.Manager
enables you to orchestrate sending messages to multiple social platforms . This should be used only if you expect your application to use more than 1 notification platform .
- init
// default contains all the clients
msgMgr := notify.Default()
- send a message to a specific client (discord , slack , telegram)
// send to a specific client like discord
err := msgMgr.SendToSpecificType(
notify.DiscordSenderType,
"https://www.some-webhook.url.com",
[]byte("hi mom"),
)
- broadcast same message to all clients
// 1 - create a mapping
ralias := notify.NewEmptyRecieverAlias()
// returns an error
_ = ralias.Add(notfy.DiscordSenderType,"https://www.google.com")
_ = ralias.Add(notify.SlackSenderType,"https://www.google.com")
// 2 - send msg
msgMgr.SendAll(ralias,[]byte("hi mom "))
To implement your own Message publisher client you have to implement the notify.Sender
Method and then add your sender to the manager
Send(reciever string, bodyContent []byte) error
Example :
- Implement the interface
// equivalent of implements keyword
var _ notify.Sender = &WhatsappSender{}
type WhatsappSender struct {}
func (w *WhatsappSender) Send(reciver string , bodyContent[]byte) error {
return nil
}
- [Optional] Enroll it to the manager if you're using it in tandum with other services
// your main go
func main() {
mgr := notify.Default()
mgr.AddSender("whatsapp",new(WhatsappSender))
_ = msgMgr.SendToSpecificType(
"whatsapp",
"chat_id",
[]byte("hi mom"),
)
}
notify-go --help
Contacts allow you to map channels / entities to different social profiles . IE you can map your 1 channel to discord , slack , telegram
This is powerful , because you can leverage the broadcast functionality of the cli to send to all those channel with 1 alias
Example :
-
Add Contact Entry
notify-go newcon crypto_channel
-
Add Contact Social Mapping
this will make it easy for you to reference by name rather than have to repaste the webhook everytime
notify-go apcon crypto_channel discord https://www.google.com notify-go apcon crypto_channel slack https://www.google.com
-
List Contacts
Allows you to list all / 1 contact
- all
notify-go cons
- specific
notify-go cons crypto_channel
- all
- with webhook
notify-go msg discord https://webhook.com "hi mom"
- through contact alias (see above section for how to create contacts)
notify-go msg discord crypto_channel "hi mom"
- to all social platforms via contact mapping*
in this scenario we setup mapping for crypto_channel to discord
and slack . So this will send to both channels concurrently
notify-go msgcon crypto_channel "hi mom"
Note this will message everyone in the contact list and all platforms. use this with caution :)
notify-go msgbrod "hi mom"