mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-30 15:24:15 +00:00
Merge branch 'multiple_attachments'
This commit is contained in:
commit
82ac097854
12
README.md
12
README.md
@ -65,4 +65,14 @@ Sample REST API calls:
|
|||||||
|
|
||||||
```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_attachment": "<base64 encoded attachment>", "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v1/send'```
|
||||||
|
|
||||||
In case you need more functionality, please **create a pull request**
|
* Receive messages
|
||||||
|
|
||||||
|
Fetch all new messages in the inbox of the specified number.
|
||||||
|
|
||||||
|
```curl -X GET -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/receive/<number>'```
|
||||||
|
|
||||||
|
e.g:
|
||||||
|
|
||||||
|
```curl -X GET -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/receive/+431212131491291'```
|
||||||
|
|
||||||
|
In case you need more functionality, please **file a ticket** or **create a PR**
|
||||||
|
|||||||
193
src/main.go
193
src/main.go
@ -13,16 +13,89 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runSignalCli(args []string) error {
|
func cleanupTmpFiles(paths []string) {
|
||||||
|
for _, path := range paths {
|
||||||
|
os.Remove(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string, recipients []string, base64Attachments []string) {
|
||||||
|
cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
|
||||||
|
cmd = append(cmd, recipients...)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
attachmentTmpPaths := []string{}
|
||||||
|
for _, base64Attachment := range base64Attachments {
|
||||||
|
u, err := uuid.NewV4()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dec, err := base64.StdEncoding.DecodeString(base64Attachment)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fType, err := filetype.Get(dec)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
attachmentTmpPath := attachmentTmpDir + u.String() + "." + fType.Extension
|
||||||
|
attachmentTmpPaths = append(attachmentTmpPaths, attachmentTmpPath)
|
||||||
|
|
||||||
|
f, err := os.Create(attachmentTmpPath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if _, err := f.Write(dec); err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := f.Sync(); err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(attachmentTmpPaths) > 0 {
|
||||||
|
cmd = append(cmd, "-a")
|
||||||
|
cmd = append(cmd , attachmentTmpPaths...)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := runSignalCli(cmd)
|
||||||
|
if err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(201, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
var outBuffer bytes.Buffer
|
||||||
cmd.Stderr = &errBuffer
|
cmd.Stderr = &errBuffer
|
||||||
|
cmd.Stdout = &outBuffer
|
||||||
|
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
@ -33,15 +106,15 @@ func runSignalCli(args []string) error {
|
|||||||
case <-time.After(60 * time.Second):
|
case <-time.After(60 * time.Second):
|
||||||
err := cmd.Process.Kill()
|
err := cmd.Process.Kill()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
return errors.New("process killed as timeout reached")
|
return "", errors.New("process killed as timeout reached")
|
||||||
case err := <-done:
|
case err := <-done:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(errBuffer.String())
|
return "", errors.New(errBuffer.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return outBuffer.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -52,6 +125,15 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
log.Info("Started Signal Messenger REST API")
|
log.Info("Started Signal Messenger REST API")
|
||||||
|
|
||||||
|
router.GET("/v1/about", func(c *gin.Context) {
|
||||||
|
type About struct {
|
||||||
|
SupportedApiVersions []string `json:"versions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
about := About{SupportedApiVersions: []string{"v1", "v2"}}
|
||||||
|
c.JSON(200, about)
|
||||||
|
})
|
||||||
|
|
||||||
router.POST("/v1/register/:number", func(c *gin.Context) {
|
router.POST("/v1/register/:number", func(c *gin.Context) {
|
||||||
number := c.Param("number")
|
number := c.Param("number")
|
||||||
|
|
||||||
@ -85,7 +167,7 @@ func main() {
|
|||||||
command = append(command, "--voice")
|
command = append(command, "--voice")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := runSignalCli(command)
|
_, err := runSignalCli(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, err.Error())
|
c.JSON(400, err.Error())
|
||||||
return
|
return
|
||||||
@ -108,7 +190,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
err := runSignalCli([]string{"--config", *signalCliConfig, "-u", number, "verify", token})
|
_, err := runSignalCli([]string{"--config", *signalCliConfig, "-u", number, "verify", token})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
@ -130,60 +212,55 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := []string{"--config", *signalCliConfig, "-u", req.Number, "send", "-m", req.Message}
|
base64Attachments := []string{}
|
||||||
cmd = append(cmd, req.Recipients...)
|
if req.Base64Attachment != "" {
|
||||||
|
base64Attachments = append(base64Attachments, req.Base64Attachment)
|
||||||
attachmentTmpPath := ""
|
|
||||||
if(req.Base64Attachment != "") {
|
|
||||||
u, err := uuid.NewV4()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dec, err := base64.StdEncoding.DecodeString(req.Base64Attachment)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fType, err := filetype.Get(dec)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
attachmentTmpPath := *attachmentTmpDir + u.String() + "." + fType.Extension
|
|
||||||
|
|
||||||
f, err := os.Create(attachmentTmpPath)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
if _, err := f.Write(dec); err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := f.Sync(); err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = append(cmd, "-a")
|
|
||||||
cmd = append(cmd , attachmentTmpPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = runSignalCli(cmd)
|
send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.POST("/v2/send", func(c *gin.Context) {
|
||||||
|
type Request struct{
|
||||||
|
Number string `json:"number"`
|
||||||
|
Recipients []string `json:"recipients"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Base64Attachments []string `json:"base64_attachments"`
|
||||||
|
}
|
||||||
|
var req Request
|
||||||
|
err := c.BindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if attachmentTmpPath != "" {
|
c.JSON(400, "Couldn't process request - invalid request")
|
||||||
os.Remove(attachmentTmpPath)
|
log.Error(err.Error())
|
||||||
}
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(201, nil)
|
|
||||||
|
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.GET("/v1/receive/:number", func(c *gin.Context) {
|
||||||
|
number := c.Param("number")
|
||||||
|
|
||||||
|
command := []string{"--config", *signalCliConfig, "-u", number, "receive", "-t", "1", "--json"}
|
||||||
|
out, err := runSignalCli(command)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out = strings.Trim(out, "\n")
|
||||||
|
lines := strings.Split(out, "\n")
|
||||||
|
|
||||||
|
jsonStr := "["
|
||||||
|
for i, line := range lines {
|
||||||
|
jsonStr += line
|
||||||
|
if i != (len(lines) - 1) {
|
||||||
|
jsonStr += ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonStr += "]"
|
||||||
|
|
||||||
|
c.String(200, jsonStr)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.Run()
|
router.Run()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user