Skip to content

Commit

Permalink
Adding specific clients for mail and push bullet
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Apr 21, 2017
1 parent 8d03807 commit 673fafe
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
17 changes: 17 additions & 0 deletions alert_task.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package main

import (
"bytes"
"fmt"
)

// Checks for and sends alerts
func SendAlerts() {
var n []Notification
Expand Down Expand Up @@ -66,3 +71,15 @@ func SendPushNotification(a AlertGroup, n []Notification) bool {
func SendEmailNotification(a AlertGroup, n []Notification) bool {
return true
}

func createEmailBody(n []Notification, t string) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("<html><div><p>New notifications</p><ul>", t))

for _, v := range n {
buf.WriteString(fmt.Sprintf("<li>%s (%s)- %s</li>", v.Message, v.Source, v.Action))
}

buf.WriteString("</ul><p>Do not reply to this email</p></div></html>")
return buf.String()
}
20 changes: 17 additions & 3 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/op/go-logging"
"gopkg.in/go-playground/validator.v9"
"gopkg.in/mailgun/mailgun-go.v1"
"net/http"
"os"
"strings"
Expand All @@ -22,14 +21,28 @@ type Application struct {
server *socketio.Server
socket socketio.Socket
router *mux.Router
mg mailgun.Mailgun
mg Alerter
pb Alerter
log *logging.Logger
test bool
port string
user string
pass string
}

type Alerter interface {
SendNotification(a AlertGroup, t string) bool
}

// Mock for alerting
type MockAlerter struct {
Pass bool
}

func (m *MockAlerter) SendNotification(a AlertGroup, t string) bool {
return m.Pass
}

// Creates a new application and returns the pointer value
func NewApp(port string, dbDriver string, dbCreds string, user string, pass string) *Application {
db, err := gorm.Open(dbDriver, dbCreds)
Expand All @@ -49,7 +62,8 @@ func NewApp(port string, dbDriver string, dbCreds string, user string, pass stri
user: user,
log: logging.MustGetLogger("notify"),
pass: pass,
mg: mailgun.NewMailgun(os.Getenv("MG_DOMAIN"), os.Getenv("MG_APIKEY"), os.Getenv("MG_PUBKEY")),
mg: NewMailClient(),
pb: NewPushBullet(),
}
}

Expand Down
36 changes: 36 additions & 0 deletions mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"gopkg.in/mailgun/mailgun-go.v1"
"os"
"strings"
)

type Mailgun struct {
Client mailgun.Mailgun
}

func NewMailClient() *Mailgun {
return &Mailgun{
Client: mailgun.NewMailgun(os.Getenv("MG_DOMAIN"), os.Getenv("MG_APIKEY"), os.Getenv("MG_PUBKEY")),
}
}

func (m *Mailgun) SendNotification(a AlertGroup, t string) bool {
ms := m.Client.NewMessage("notify@valeska.co.uk", fmt.Sprintf("New notifications for %s", t), "New Notifications!")
ms.SetHtml(t)

for _, v := range strings.Split(a.Emails, ",") {
ms.AddRecipient(v)
}

_, _, e := m.Client.Send(ms)

if e != nil {
App.log.Error(e)
return false
}

return true
}
30 changes: 30 additions & 0 deletions push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
pb "github.com/mitsuse/pushbullet-go"
"github.com/mitsuse/pushbullet-go/requests"
"os"
)

type PushBullet struct {
Client *pb.Pushbullet
}

func NewPushBullet() *PushBullet {
return &PushBullet{
Client: pb.New(os.Getenv("PB_Token")),
}
}

func (p *PushBullet) SendNotification(a AlertGroup, t string) bool {
n := requests.NewNote()
n.Title = "New Notifications"
n.Body = t

if _, err := p.Client.PostPushesNote(n); err != nil {
App.log.Error(err)
return false
}

return true
}

0 comments on commit 673fafe

Please sign in to comment.