improved groups support

This commit is contained in:
Bernhard B
2020-04-11 20:31:27 +02:00
parent dc85213195
commit 602843a6ed
3 changed files with 45 additions and 12 deletions

View File

@@ -105,10 +105,10 @@ Sample REST API calls:
* Send a message to a group * Send a message to a group
```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "<number>", "group_id": "<group id>"}' 'http://127.0.0.1:8080/v1/send'``` ```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "<number>", "recipients": ["<group id>"], "is_group": true}' 'http://127.0.0.1:8080/v1/send'```
e.g: 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** In case you need more functionality, please **file a ticket** or **create a PR**

View File

@@ -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'``` ```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": "<name of the group>", "members": ["<member1>", "<member2>"]}' 'http://127.0.0.1:8080/v1/groups/<number>'```
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/<number>'```
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 # Troubleshooting
In case you've problems with the `signal-cli-rest-api` container, have a look [here](TROUBLESHOOTING.md) In case you've problems with the `signal-cli-rest-api` container, have a look [here](TROUBLESHOOTING.md)

View File

@@ -32,18 +32,23 @@ func cleanupTmpFiles(paths []string) {
} }
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message 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} cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
if base64EncodedGroupId != "" && len(recipients) > 0 { if len(recipients) == 0 {
c.JSON(400, gin.H{"error": "Please specify either a group id or recipient(s) - but not both"}) c.JSON(400, gin.H{"error": "Please specify at least one recipient"})
return return
} }
if len(recipients) > 0 { if !isGroup {
cmd = append(cmd, recipients...) cmd = append(cmd, recipients...)
} else { } 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 { if err != nil {
c.JSON(400, gin.H{"error": "Invalid group id"}) c.JSON(400, gin.H{"error": "Invalid group id"})
return return
@@ -288,7 +293,7 @@ func main() {
Recipients []string `json:"recipients"` Recipients []string `json:"recipients"`
Message string `json:"message"` Message string `json:"message"`
Base64Attachment string `json:"base64_attachment"` Base64Attachment string `json:"base64_attachment"`
GroupId string `json:"group_id"` IsGroup bool `json:"is_group"`
} }
var req Request var req Request
err := c.BindJSON(&req) err := c.BindJSON(&req)
@@ -302,7 +307,7 @@ func main() {
base64Attachments = append(base64Attachments, req.Base64Attachment) 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) { router.POST("/v2/send", func(c *gin.Context) {
@@ -311,7 +316,7 @@ func main() {
Recipients []string `json:"recipients"` Recipients []string `json:"recipients"`
Message string `json:"message"` Message string `json:"message"`
Base64Attachments []string `json:"base64_attachments"` Base64Attachments []string `json:"base64_attachments"`
GroupId string `json:"group_id"` IsGroup bool `json:"is_group"`
} }
var req Request var req Request
err := c.BindJSON(&req) err := c.BindJSON(&req)
@@ -321,7 +326,7 @@ func main() {
return 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) { router.GET("/v1/receive/:number", func(c *gin.Context) {