Add zulip webhook

This commit is contained in:
2025-10-10 20:43:09 +02:00
parent 75b1045983
commit cdc896c5f0
3 changed files with 91 additions and 1 deletions

66
webhook/zulip.go Normal file
View File

@@ -0,0 +1,66 @@
// Package webhook provides Zulip webhook implementation
package webhook
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
logger "git.site.quack-lab.dev/dave/cylogger"
)
// ZulipWebhook implements WebhookInterface for Zulip
type ZulipWebhook struct {
url string
email string
token string
client *http.Client
}
// NewZulipWebhook creates a new Zulip webhook client
func NewZulipWebhook(url, email, token string) *ZulipWebhook {
logger.Info("Zulip webhook client initialized with email: %s", email)
return &ZulipWebhook{
url: url,
email: email,
token: token,
client: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// Post sends a message to Zulip
func (z *ZulipWebhook) Post(channel, topic, message string) error {
logger.Debug("Sending Zulip message to channel: %s, topic: %s", channel, topic)
data := url.Values{}
data.Set("type", "stream")
data.Set("to", channel)
data.Set("topic", topic)
data.Set("content", message)
req, err := http.NewRequest("POST", z.url, strings.NewReader(data.Encode()))
if err != nil {
return fmt.Errorf("failed to create zulip request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(z.email, z.token)
resp, err := z.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send zulip message: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("zulip request failed with status: %d", resp.StatusCode)
}
logger.Info("Zulip message sent successfully to channel: %s, topic: %s", channel, topic)
return nil
}