From 94cf8100bada94b2241e64a410859e128c1c6342 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Sun, 29 Jun 2025 16:41:38 +0200 Subject: [PATCH] added possibility to call a webhook when a message is received --- src/client/client.go | 6 ++++-- src/client/jsonrpc2.go | 35 ++++++++++++++++++++++++++++++++++- src/main.go | 7 ++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/client/client.go b/src/client/client.go index a56d956..35077b3 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -370,10 +370,11 @@ type SignalClient struct { signalCliApiConfigPath string signalCliApiConfig *utils.SignalCliApiConfig cliClient *CliClient + receiveWebhookUrl string } func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string, signalCliMode SignalCliMode, - jsonRpc2ClientConfigPath string, signalCliApiConfigPath string) *SignalClient { + jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string) *SignalClient { return &SignalClient{ signalCliConfig: signalCliConfig, attachmentTmpDir: attachmentTmpDir, @@ -382,6 +383,7 @@ func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpD jsonRpc2ClientConfigPath: jsonRpc2ClientConfigPath, jsonRpc2Clients: make(map[string]*JsonRpc2Client), signalCliApiConfigPath: signalCliApiConfigPath, + receiveWebhookUrl: receiveWebhookUrl, } } @@ -411,7 +413,7 @@ func (s *SignalClient) Init() error { return err } - go s.jsonRpc2Clients[number].ReceiveData(number) //receive messages in goroutine + go s.jsonRpc2Clients[number].ReceiveData(number, s.receiveWebhookUrl) //receive messages in goroutine } } else { s.cliClient = NewCliClient(s.signalCliMode, s.signalCliApiConfig) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 6df5c4b..b2fae23 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -7,6 +7,9 @@ import ( "net" "sync" "time" + "net/http" + "bytes" + "strconv" "github.com/bbernhard/signal-cli-rest-api/utils" uuid "github.com/gofrs/uuid" @@ -176,7 +179,30 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{ return string(resp.Result), nil } -func (r *JsonRpc2Client) ReceiveData(number string) { +func postMessageToWebhook(webhookUrl string, data []byte) error { + r, err := http.NewRequest("POST", webhookUrl, bytes.NewBuffer(data)) + if err != nil { + return err + } + + r.Header.Add("Content-Type", "application/json") + + client := &http.Client{} + res, err := client.Do(r) + if err != nil { + return err + } + + defer res.Body.Close() + + log.Info(res.StatusCode) + if res.StatusCode != 200 && res.StatusCode != 201 { + return errors.New("Unexpected status code returned (" + strconv.Itoa(res.StatusCode) + ")") + } + return nil +} + +func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { connbuf := bufio.NewReader(r.conn) for { str, err := connbuf.ReadString('\n') @@ -190,6 +216,13 @@ func (r *JsonRpc2Client) ReceiveData(number string) { } log.Debug("json-rpc received data: ", str) + if receiveWebhookUrl != "" { + err = postMessageToWebhook(receiveWebhookUrl, []byte(str)) + if err != nil { + log.Error("Couldn't post data to webhook: ", err) + } + } + var resp1 JsonRpc2ReceivedMessage json.Unmarshal([]byte(str), &resp1) if resp1.Method == "receive" { diff --git a/src/main.go b/src/main.go index 64e1f8d..6398132 100644 --- a/src/main.go +++ b/src/main.go @@ -155,9 +155,14 @@ func main() { } } + webhookUrl := utils.GetEnv("RECEIVE_WEBHOOK_URL", "") + if webhookUrl != "" && signalCliMode != client.JsonRpc { + log.Fatal("Env variable RECEIVE_WEBHOOK_URL can't be used with mode json-rpc!") + } + jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml" signalCliApiConfigPath := *signalCliConfig + "/api-config.yml" - signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath) + signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl) err = signalClient.Init() if err != nil { log.Fatal("Couldn't init Signal Client: ", err.Error())