added possibility to call a webhook when a message is received

This commit is contained in:
Bernhard B 2025-06-29 16:41:38 +02:00
parent cdf710544e
commit 94cf8100ba
3 changed files with 44 additions and 4 deletions

View File

@ -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)

View File

@ -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" {

View File

@ -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())