44 lines
1.2 KiB
Go
Raw Normal View History

2025-01-05 15:16:26 -08:00
package messaging
import (
"fmt"
"log"
"net/http"
"strings"
"git.simponic.xyz/simponic/phoneof/utils"
)
2025-01-05 16:23:56 -08:00
func SendNtfy(ntfyEndpoint string) Continuation {
2025-01-05 15:16:26 -08:00
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 15:35:51 -08: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 15:16:26 -08: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)
}
}
}