36 lines
1012 B
Go
36 lines
1012 B
Go
|
package messaging
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
|
"git.simponic.xyz/simponic/phoneof/utils"
|
||
|
)
|
||
|
|
||
|
func SendNtfy(topic string, ntfyEndpoint string) Continuation {
|
||
|
return func(message Message) ContinuationChain {
|
||
|
return func(success Continuation, failure Continuation) ContinuationChain {
|
||
|
log.Println(message)
|
||
|
if message.FrenName != "ntfy" {
|
||
|
log.Printf("fren name for message %v is not ntfy so we wont send it there", message)
|
||
|
return success(message)
|
||
|
}
|
||
|
encodedMsg := fmt.Sprintf(`{"message": "%s", "topic": "%s"}`, utils.Quote(message.Message), utils.Quote(topic))
|
||
|
|
||
|
url := ntfyEndpoint
|
||
|
payload := strings.NewReader(encodedMsg)
|
||
|
|
||
|
req, _ := http.NewRequest("PUT", url, payload)
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
res, err := http.DefaultClient.Do(req)
|
||
|
if err != nil || res.StatusCode/100 != 2 {
|
||
|
log.Printf("got err sending message send req %s %v %s", encodedMsg, res, err)
|
||
|
return failure(message)
|
||
|
}
|
||
|
return success(message)
|
||
|
}
|
||
|
}
|
||
|
}
|