mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-21 13:54:18 +00:00
added support for groups
* added possibility to add groups, list groups, delete groups and send a messages to a groups. see #12
This commit is contained in:
parent
a70f0a9d44
commit
5d0eb28772
161
src/main.go
161
src/main.go
@ -16,16 +16,42 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type GroupEntry struct{
|
||||||
|
Name string `json:"name"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
InternalId string `json:"internal_id"`
|
||||||
|
Members []string `json:"members"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
Blocked bool `json:"blocked"`
|
||||||
|
}
|
||||||
|
|
||||||
func cleanupTmpFiles(paths []string) {
|
func cleanupTmpFiles(paths []string) {
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
os.Remove(path)
|
os.Remove(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string, recipients []string, base64Attachments []string) {
|
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string,
|
||||||
|
recipients []string, base64Attachments []string, base64EncodedGroupId string) {
|
||||||
cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
|
cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
|
||||||
cmd = append(cmd, recipients...)
|
|
||||||
|
if base64EncodedGroupId != "" && len(recipients) > 0 {
|
||||||
|
c.JSON(400, gin.H{"error": "Please specify either a group id or recipient(s) - but not both"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(recipients) > 0 {
|
||||||
|
cmd = append(cmd, recipients...)
|
||||||
|
} else {
|
||||||
|
groupId, err := base64.StdEncoding.DecodeString(base64EncodedGroupId)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "Invalid group id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = append(cmd, []string{"-g", string(groupId)}...)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
attachmentTmpPaths := []string{}
|
attachmentTmpPaths := []string{}
|
||||||
@ -86,6 +112,66 @@ func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, numbe
|
|||||||
c.JSON(201, nil)
|
c.JSON(201, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
|
||||||
|
groupEntries := []GroupEntry{}
|
||||||
|
|
||||||
|
out, err := runSignalCli([]string{"--config", signalCliConfig, "-u", number, "listGroups", "-d"})
|
||||||
|
if err != nil {
|
||||||
|
return groupEntries, err
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(out, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
var groupEntry GroupEntry
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
idIdx := strings.Index(line, " Name: ")
|
||||||
|
idPair := line[:idIdx]
|
||||||
|
groupEntry.InternalId = strings.TrimPrefix(idPair, "Id: ")
|
||||||
|
groupEntry.Id = base64.StdEncoding.EncodeToString([]byte(groupEntry.InternalId))
|
||||||
|
lineWithoutId := strings.TrimLeft(line[idIdx:], " ")
|
||||||
|
|
||||||
|
nameIdx := strings.Index(lineWithoutId, " Active: ")
|
||||||
|
namePair := lineWithoutId[:nameIdx]
|
||||||
|
groupEntry.Name = strings.TrimRight(strings.TrimPrefix(namePair, "Name: "), " ")
|
||||||
|
lineWithoutName := strings.TrimLeft(lineWithoutId[nameIdx:], " ")
|
||||||
|
|
||||||
|
activeIdx := strings.Index(lineWithoutName, " Blocked: ")
|
||||||
|
activePair := lineWithoutName[:activeIdx]
|
||||||
|
active := strings.TrimPrefix(activePair, "Active: ")
|
||||||
|
if active == "true" {
|
||||||
|
groupEntry.Active = true
|
||||||
|
} else {
|
||||||
|
groupEntry.Active = false
|
||||||
|
}
|
||||||
|
lineWithoutActive := strings.TrimLeft(lineWithoutName[activeIdx:], " ")
|
||||||
|
|
||||||
|
blockedIdx := strings.Index(lineWithoutActive, " Members: ")
|
||||||
|
blockedPair := lineWithoutActive[:blockedIdx]
|
||||||
|
blocked := strings.TrimPrefix(blockedPair, "Blocked: ")
|
||||||
|
if blocked == "true" {
|
||||||
|
groupEntry.Blocked = true
|
||||||
|
} else {
|
||||||
|
groupEntry.Blocked = false
|
||||||
|
}
|
||||||
|
lineWithoutBlocked := strings.TrimLeft(lineWithoutActive[blockedIdx:], " ")
|
||||||
|
|
||||||
|
membersPair := lineWithoutBlocked
|
||||||
|
members := strings.TrimPrefix(membersPair, "Members: ")
|
||||||
|
trimmedMembers := strings.TrimRight(strings.TrimLeft(members, "["), "]")
|
||||||
|
trimmedMembersList := strings.Split(trimmedMembers, ",")
|
||||||
|
for _, member := range trimmedMembersList {
|
||||||
|
groupEntry.Members = append(groupEntry.Members, strings.Trim(member, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
groupEntries = append(groupEntries, groupEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupEntries, nil
|
||||||
|
}
|
||||||
|
|
||||||
func runSignalCli(args []string) (string, error) {
|
func runSignalCli(args []string) (string, error) {
|
||||||
cmd := exec.Command("signal-cli", args...)
|
cmd := exec.Command("signal-cli", args...)
|
||||||
var errBuffer bytes.Buffer
|
var errBuffer bytes.Buffer
|
||||||
@ -204,6 +290,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"`
|
||||||
}
|
}
|
||||||
var req Request
|
var req Request
|
||||||
err := c.BindJSON(&req)
|
err := c.BindJSON(&req)
|
||||||
@ -217,7 +304,7 @@ func main() {
|
|||||||
base64Attachments = append(base64Attachments, req.Base64Attachment)
|
base64Attachments = append(base64Attachments, req.Base64Attachment)
|
||||||
}
|
}
|
||||||
|
|
||||||
send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments)
|
send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments, req.GroupId)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/v2/send", func(c *gin.Context) {
|
router.POST("/v2/send", func(c *gin.Context) {
|
||||||
@ -226,6 +313,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"`
|
||||||
}
|
}
|
||||||
var req Request
|
var req Request
|
||||||
err := c.BindJSON(&req)
|
err := c.BindJSON(&req)
|
||||||
@ -235,7 +323,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments)
|
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments, req.GroupId)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/v1/receive/:number", func(c *gin.Context) {
|
router.GET("/v1/receive/:number", func(c *gin.Context) {
|
||||||
@ -263,5 +351,70 @@ func main() {
|
|||||||
c.String(200, jsonStr)
|
c.String(200, jsonStr)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.POST("/v1/groups/:number", func(c *gin.Context) {
|
||||||
|
number := c.Param("number")
|
||||||
|
|
||||||
|
type Request struct{
|
||||||
|
Name string `json:"name"`
|
||||||
|
Members []string `json:"members"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var req Request
|
||||||
|
err := c.BindJSON(&req)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, "Couldn't process request - invalid request")
|
||||||
|
log.Error(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
cmd := []string{"--config", *signalCliConfig, "-u", number, "updateGroup", "-n", req.Name, "-m"}
|
||||||
|
cmd = append(cmd, req.Members...)
|
||||||
|
log.Info(cmd)
|
||||||
|
out, err := runSignalCli(cmd)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info(out)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
router.GET("/v1/groups/:number", func(c *gin.Context) {
|
||||||
|
number := c.Param("number")
|
||||||
|
|
||||||
|
groups, err := getGroups(number, *signalCliConfig)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, groups)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
router.DELETE("/v1/groups/:number/:groupid", func(c *gin.Context) {
|
||||||
|
base64EncodedGroupId := c.Param("groupid")
|
||||||
|
number := c.Param("number")
|
||||||
|
|
||||||
|
if base64EncodedGroupId == "" {
|
||||||
|
c.JSON(400, gin.H{"error": "Please specify a group id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
groupId, err := base64.StdEncoding.DecodeString(base64EncodedGroupId)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "Invalid group id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = runSignalCli([]string{"--config", *signalCliConfig, "-u", number, "quitGroup", "-g", string(groupId)})
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
router.Run()
|
router.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user