xmpp-webhook/main.go

103 wiersze
2.1 KiB
Go
Czysty Zwykły widok Historia

2017-09-24 18:44:39 +00:00
package main
import (
"log"
2017-09-25 15:58:44 +00:00
"net/http"
2017-09-24 18:44:39 +00:00
"os"
2017-09-25 15:58:44 +00:00
"strings"
2017-09-24 18:44:39 +00:00
"github.com/emgee/go-xmpp/src/xmpp"
)
2017-09-25 12:59:08 +00:00
// starts xmpp session and returns the xmpp client
func xmppLogin(id string, pass string) (*xmpp.XMPP, error) {
2017-09-25 11:33:43 +00:00
// parse jid structure
2017-09-25 08:54:06 +00:00
jid, err := xmpp.ParseJID(id)
2017-09-25 12:59:08 +00:00
if err != nil {
return nil, err
}
2017-09-24 18:44:39 +00:00
2017-09-25 11:33:43 +00:00
// extract/generate address:port from jid
2017-09-24 18:44:39 +00:00
addr, err := xmpp.HomeServerAddrs(jid)
2017-09-25 12:59:08 +00:00
if err != nil {
return nil, err
}
2017-09-24 18:44:39 +00:00
2017-09-25 11:33:43 +00:00
// create xml stream to address
2017-09-24 18:44:39 +00:00
stream, err := xmpp.NewStream(addr[0], nil)
2017-09-25 12:59:08 +00:00
if err != nil {
return nil, err
}
2017-09-24 18:44:39 +00:00
2017-09-25 11:33:43 +00:00
// create client (login)
2017-09-25 08:54:06 +00:00
client, err := xmpp.NewClientXMPP(stream, jid, pass, nil)
2017-09-25 12:59:08 +00:00
if err != nil {
return nil, err
}
2017-09-24 18:44:39 +00:00
2017-09-25 12:59:08 +00:00
return client, nil
2017-09-25 08:54:06 +00:00
}
func main() {
2017-09-26 12:39:19 +00:00
// get xmpp credentials and message receivers from env
xi := os.Getenv("XMPP_ID")
xp := os.Getenv("XMPP_PASS")
xr := os.Getenv("XMPP_RECEIVERS")
2017-09-25 08:54:06 +00:00
2017-09-25 15:58:44 +00:00
// check if xmpp credentials and receiver list are supplied
if len(xi) < 1 || len(xp) < 1 || len(xr) < 1 {
2017-09-26 12:39:19 +00:00
log.Fatal("XMPP_ID, XMPP_PASS or XMPP_RECEIVERS not set")
2017-09-25 08:54:06 +00:00
}
2017-09-26 12:39:19 +00:00
// connect to xmpp server
xc, err := xmppLogin(xi, xp)
if err != nil {
log.Fatal(err)
}
// announce initial presence
xc.Out <- xmpp.Presence{}
// listen for incoming xmpp stanzas
2017-09-25 12:59:08 +00:00
go func() {
2017-09-26 12:39:19 +00:00
for stanza := range xc.In {
// check if stanza is a message
m, ok := stanza.(*xmpp.Message)
if ok && len(m.Body) > 0 {
// echo the message
xc.Out <- xmpp.Message{
To: m.From,
Body: m.Body,
}
2017-09-25 12:59:08 +00:00
}
}
2017-09-26 12:39:19 +00:00
// xc.In is closed when the server closes the stream
log.Fatal("connection lost")
2017-09-25 12:59:08 +00:00
}()
2017-09-24 18:44:39 +00:00
2017-09-26 12:39:19 +00:00
// create chan for messages (webhooks -> xmpp)
messages := make(chan string)
2017-09-25 15:58:44 +00:00
2017-09-26 12:39:19 +00:00
// wait for messages from the webhooks and send them to all receivers
2017-09-25 15:58:44 +00:00
go func() {
2017-09-26 12:39:19 +00:00
for m := range messages {
2017-09-25 15:58:44 +00:00
for _, r := range strings.Split(xr, ",") {
2017-09-26 12:39:19 +00:00
xc.Out <- xmpp.Message{
To: r,
Body: []xmpp.MessageBody{
xmpp.MessageBody{
Value: m,
},
},
2017-09-25 15:58:44 +00:00
}
}
}
}()
2017-09-26 12:39:19 +00:00
// initialize handler for grafana alerts
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
2017-09-25 15:58:44 +00:00
// listen for requests
2017-09-26 12:39:19 +00:00
http.ListenAndServe(":4321", nil)
2017-09-24 18:44:39 +00:00
}