Skip to content

JeremyDumesny/notification-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

notification-service

A library providing the ability to send messages.

This project aims to be able to send messages by Mail and/or by Rocket. You can easily implement another notifier type, you just have to respect the interface.

Usage

Straightforward usage

Create an SMTP notifier

notifier, _ := mail.InitNotifier(notification_service.NotifierConfig{
	Name:   "mail-example",
	Type:   "mail",
	Source: notification_service.InfoConfSource{From: "[email protected]", Pwd: "password"},
	Host:   "smtp.gmail.com",
	Port:   587,
	Tls:    true,
	Debug:  false,
})

Create a rocket chat notifier

notifier, _ := rocket.InitNotifier(notification_service.NotifierConfig{
	Name:   "rocket-example",
	Type:   "rocket",
	Source: notification_service.InfoConfSource{From: "[email protected]", Pwd: "password"},
	Host:   "rocket.example.io",
	Port:   443,
	Tls:    true,
	Debug:  false,
})

Use the notifier to send email/rocket messages

// Send an email using smtp notifier
_ = notifier.SendMessage("Test message.", "[email protected]")

// Send a rocket msg to a user
_ = notifier.SendMessage("Test message.", "@user")

// Send a rocket msg to a channel
_ = notifier.SendMessage("Test message.", "#channel")

Using a configuration file

If you want to create a configuration file, you can use Viper to read, and fill this structure by Unmarshalling the config file. The mapstructure will read all configuration file type.

type Config struct {
    Notifiers []notification_service.NotifierConfig `mapstructure:"notifiers"`
}

Here is a JSON configuration file example

"notifiers": [
    {
      "name": "smtp-example",
      "type": "mail",
      "source": {
        "from": "[email protected]",
        "pwd": "secret_pwd"
      },
      "host": "smtp.gmail.com",
      "port": 587,
      "tls": true,
      "debug": false
    },
    {
      "name": "rocket-example",
      "type": "rocket",
      "source": {
        "from": "[email protected]",
        "pwd": "secret_pwd"
      },
      "host": "rocket.example.io",
      "port": 443,
      "tls": true,
      "debug": false
    }
  ]

Fill the structure using Viper

func ParseConfig(configFilePath string) (*Config, error) {
	var configArray Config
	viper.SetConfigName("config")
	viper.SetConfigType(json)
	viper.AddConfigPath("path/to/config/file")
	_ = viper.ReadInConfig()
	_ = viper.Unmarshal(&configArray)
	return &configInfo, nil
}

About

A library providing the ability to send messages.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages