diff --git a/README.md b/README.md index 66a00e7..82c6554 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,10 @@ Sample REST API calls: * Send a message to a group - ```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "", "group_id": ""}' 'http://127.0.0.1:8080/v1/send'``` + ```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "", "recipients": [""], "is_group": true}' 'http://127.0.0.1:8080/v1/send'``` e.g: - ```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "+431212131491291", "group_id": "ckRzaEd4VmRzNnJaASAEsasa"}' 'http://127.0.0.1:8080/v1/send'``` + ```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "+431212131491291", "recipients": ["ckRzaEd4VmRzNnJaASAEsasa"], "is_group": true}' 'http://127.0.0.1:8080/v1/send'``` In case you need more functionality, please **file a ticket** or **create a PR** diff --git a/doc/HOMEASSISTANT.md b/doc/HOMEASSISTANT.md index 071085c..cdffbc1 100644 --- a/doc/HOMEASSISTANT.md +++ b/doc/HOMEASSISTANT.md @@ -46,5 +46,33 @@ e.g: ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/register/+431212131491291/verify/123-456'``` + +## Sending messages to Signal Messenger groups + +The `signal-cli-rest-api` docker container is also capable of sending messages to a Signal Messenger group. + +Requirements: + + * Home Assistant Version >= 0.109 + * signal-cli-rest-api build-nr >= 2 + The build number can be checked with: `curl -X GET -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/about` + * your phone number needs to be properly registered (see the "Register phone number" section above on how to do that) + +A new Signal Messenger group can be created with the following REST API request: + +```curl -X POST -H "Content-Type: application/json" -d '{"name": "", "members": ["", ""]}' 'http://127.0.0.1:8080/v1/groups/'``` + +e.g: + +This creates a new Signal Messenger group called `my group` with the members `+4354546464654` and `+4912812812121`. + +```curl -X POST -H "Content-Type: application/json" -d '{"name": "my group", "members": ["+4354546464654", "+4912812812121"]}' 'http://127.0.0.1:8080/v1/groups/+431212131491291'``` + +Next, use the following endpoint to obtain the group id: + +```curl -X GET -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/groups/'``` + +The group id then needs to be added to the Home Assistant `configuration.yaml` file (see [here](https://www.home-assistant.io/integrations/signal_messenger/) for details) + # Troubleshooting In case you've problems with the `signal-cli-rest-api` container, have a look [here](TROUBLESHOOTING.md) diff --git a/src/main.go b/src/main.go index 5a744d4..2ab0a5c 100644 --- a/src/main.go +++ b/src/main.go @@ -32,18 +32,23 @@ func cleanupTmpFiles(paths []string) { } func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string, - recipients []string, base64Attachments []string, base64EncodedGroupId string) { + recipients []string, base64Attachments []string, isGroup bool) { cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message} - if base64EncodedGroupId != "" && len(recipients) > 0 { - c.JSON(400, gin.H{"error": "Please specify either a group id or recipient(s) - but not both"}) + if len(recipients) == 0 { + c.JSON(400, gin.H{"error": "Please specify at least one recipient"}) return } - if len(recipients) > 0 { + if !isGroup { cmd = append(cmd, recipients...) } else { - groupId, err := base64.StdEncoding.DecodeString(base64EncodedGroupId) + if len(recipients) > 1 { + c.JSON(400, gin.H{"error": "More than one recipient is currently not allowed"}) + return + } + + groupId, err := base64.StdEncoding.DecodeString(recipients[0]) if err != nil { c.JSON(400, gin.H{"error": "Invalid group id"}) return @@ -212,7 +217,7 @@ func main() { router.GET("/v1/about", func(c *gin.Context) { type About struct { SupportedApiVersions []string `json:"versions"` - BuildNr int `json:"build"` + BuildNr int `json:"build"` } about := About{SupportedApiVersions: []string{"v1", "v2"}, BuildNr: 2} @@ -288,7 +293,7 @@ func main() { Recipients []string `json:"recipients"` Message string `json:"message"` Base64Attachment string `json:"base64_attachment"` - GroupId string `json:"group_id"` + IsGroup bool `json:"is_group"` } var req Request err := c.BindJSON(&req) @@ -302,7 +307,7 @@ func main() { base64Attachments = append(base64Attachments, req.Base64Attachment) } - send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments, req.GroupId) + send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments, req.IsGroup) }) router.POST("/v2/send", func(c *gin.Context) { @@ -311,7 +316,7 @@ func main() { Recipients []string `json:"recipients"` Message string `json:"message"` Base64Attachments []string `json:"base64_attachments"` - GroupId string `json:"group_id"` + IsGroup bool `json:"is_group"` } var req Request err := c.BindJSON(&req) @@ -321,7 +326,7 @@ func main() { return } - send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments, req.GroupId) + send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments, req.IsGroup) }) router.GET("/v1/receive/:number", func(c *gin.Context) {