phoneof/adapters/messaging/ntfy.go

44 lines
1.2 KiB
Go
Raw Normal View History

2025-01-05 18:16:26 -05:00
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)
}
2025-01-05 18:35:51 -05:00
content := strings.SplitN(message.Message, " ", 2)
if len(content) < 2 {
log.Printf("no topic %s", content)
return failure(message)
}
topic := content[0]
msg := content[1]
encodedMsg := fmt.Sprintf(`{"message": "%s", "topic": "%s"}`, utils.Quote(msg), utils.Quote(topic))
2025-01-05 18:16:26 -05:00
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)
}
}
}