mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 15:44:28 +01:00
reworked groups support
This commit is contained in:
25
README.md
25
README.md
@@ -53,17 +53,27 @@ Sample REST API calls:
|
|||||||
|
|
||||||
```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'```
|
||||||
|
|
||||||
* Send message to multiple recipients
|
* Send a message to multiple recipients
|
||||||
|
|
||||||
```curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v1/send'```
|
```curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v2/send'```
|
||||||
|
|
||||||
e.g:
|
e.g:
|
||||||
|
|
||||||
```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "+431212131491291", "recipients": ["+4354546464654", "+4912812812121"]}' 'http://127.0.0.1:8080/v1/send'```
|
```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "+431212131491291", "recipients": ["+4354546464654", "+4912812812121"]}' 'http://127.0.0.1:8080/v2/send'```
|
||||||
|
|
||||||
* Send a message (+ base64 encoded attachment) to multiple recipients
|
* Send a message (+ base64 encoded attachment) to multiple recipients
|
||||||
|
|
||||||
```curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "base64_attachment": "<base64 encoded attachment>", "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v1/send'```
|
```curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "base64_attachments": ["<base64 encoded attachment>"], "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v2/send'```
|
||||||
|
|
||||||
|
* Send a message to a group
|
||||||
|
|
||||||
|
The group id can be obtained via the "List groups" REST call.
|
||||||
|
|
||||||
|
```curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "number": "<number>", "recipients": ["<group id>"]}' 'http://127.0.0.1:8080/v2/send'```
|
||||||
|
|
||||||
|
e.g:
|
||||||
|
|
||||||
|
```curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello World!", "number": "+431212131491291", "recipients": ["group.ckRzaEd4VmRzNnJaASAEsasa", "+4912812812121"]}' 'http://127.0.0.1:8080/v2/send'```
|
||||||
|
|
||||||
* Receive messages
|
* Receive messages
|
||||||
|
|
||||||
@@ -103,12 +113,11 @@ Sample REST API calls:
|
|||||||
|
|
||||||
```curl -X DELETE -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/groups/+431212131491291/ckRzaEd4VmRzNnJaASAEsasa'```
|
```curl -X DELETE -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/groups/+431212131491291/ckRzaEd4VmRzNnJaASAEsasa'```
|
||||||
|
|
||||||
* Send a message to a group
|
|
||||||
|
|
||||||
```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:
|
|
||||||
|
|
||||||
```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'```
|
The following REST API endpoints are **deprecated and no longer maintained!**
|
||||||
|
|
||||||
|
```/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**
|
||||||
|
|||||||
57
src/main.go
57
src/main.go
@@ -16,6 +16,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const groupPrefix = "group."
|
||||||
|
|
||||||
type GroupEntry struct {
|
type GroupEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
@@ -133,7 +135,7 @@ func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
|
|||||||
idIdx := strings.Index(line, " Name: ")
|
idIdx := strings.Index(line, " Name: ")
|
||||||
idPair := line[:idIdx]
|
idPair := line[:idIdx]
|
||||||
groupEntry.InternalId = strings.TrimPrefix(idPair, "Id: ")
|
groupEntry.InternalId = strings.TrimPrefix(idPair, "Id: ")
|
||||||
groupEntry.Id = base64.StdEncoding.EncodeToString([]byte(groupEntry.InternalId))
|
groupEntry.Id = groupPrefix + base64.StdEncoding.EncodeToString([]byte(groupEntry.InternalId))
|
||||||
lineWithoutId := strings.TrimLeft(line[idIdx:], " ")
|
lineWithoutId := strings.TrimLeft(line[idIdx:], " ")
|
||||||
|
|
||||||
nameIdx := strings.Index(lineWithoutId, " Active: ")
|
nameIdx := strings.Index(lineWithoutId, " Active: ")
|
||||||
@@ -239,7 +241,7 @@ func main() {
|
|||||||
err := json.Unmarshal(buf.Bytes(), &req)
|
err := json.Unmarshal(buf.Bytes(), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Couldn't register number: ", err.Error())
|
log.Error("Couldn't register number: ", err.Error())
|
||||||
c.JSON(400, "Couldn't process request - invalid request.")
|
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request."})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -247,7 +249,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if number == "" {
|
if number == "" {
|
||||||
c.JSON(400, "Please provide a number")
|
c.JSON(400, gin.H{"error": "Please provide a number"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +261,7 @@ func main() {
|
|||||||
|
|
||||||
_, err := runSignalCli(command)
|
_, err := runSignalCli(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, err.Error())
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(201, nil)
|
c.JSON(201, nil)
|
||||||
@@ -270,7 +272,7 @@ func main() {
|
|||||||
token := c.Param("token")
|
token := c.Param("token")
|
||||||
|
|
||||||
if number == "" {
|
if number == "" {
|
||||||
c.JSON(400, "Please provide a number")
|
c.JSON(400, gin.H{"error": "Please provide a number"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,7 +300,7 @@ func main() {
|
|||||||
var req Request
|
var req Request
|
||||||
err := c.BindJSON(&req)
|
err := c.BindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, "Couldn't process request - invalid request")
|
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,17 +318,48 @@ 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"`
|
||||||
IsGroup bool `json:"is_group"`
|
|
||||||
}
|
}
|
||||||
var req Request
|
var req Request
|
||||||
err := c.BindJSON(&req)
|
err := c.BindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, "Couldn't process request - invalid request")
|
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments, req.IsGroup)
|
if len(req.Recipients) == 0 {
|
||||||
|
c.JSON(400, gin.H{"error": "Couldn't process request - please provide at least one recipient"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
groups := []string{}
|
||||||
|
recipients := []string{}
|
||||||
|
|
||||||
|
for _, recipient := range req.Recipients {
|
||||||
|
if strings.HasPrefix(recipient, groupPrefix) {
|
||||||
|
groups = append(groups, strings.TrimPrefix(recipient, groupPrefix))
|
||||||
|
} else {
|
||||||
|
recipients = append(recipients, recipient)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(recipients) > 0 && len(groups) > 0 {
|
||||||
|
c.JSON(400, gin.H{"error": "Signal Messenger Groups and phone numbers cannot be specified together in one request! Please split them up into multiple REST API calls."})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(groups) > 1 {
|
||||||
|
c.JSON(400, gin.H{"error": "A signal message cannot be sent to more than one group at once! Please use multiple REST API calls for that."})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, group := range groups {
|
||||||
|
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, []string{group}, req.Base64Attachments, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(recipients) > 0 {
|
||||||
|
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, recipients, req.Base64Attachments, false)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/v1/receive/:number", func(c *gin.Context) {
|
router.GET("/v1/receive/:number", func(c *gin.Context) {
|
||||||
@@ -335,7 +368,7 @@ func main() {
|
|||||||
command := []string{"--config", *signalCliConfig, "-u", number, "receive", "-t", "1", "--json"}
|
command := []string{"--config", *signalCliConfig, "-u", number, "receive", "-t", "1", "--json"}
|
||||||
out, err := runSignalCli(command)
|
out, err := runSignalCli(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, err.Error())
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +398,7 @@ func main() {
|
|||||||
var req Request
|
var req Request
|
||||||
err := c.BindJSON(&req)
|
err := c.BindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, "Couldn't process request - invalid request")
|
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -404,7 +437,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
groupId, err := base64.StdEncoding.DecodeString(base64EncodedGroupId)
|
groupId, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(base64EncodedGroupId, groupPrefix))
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user