Skip to content

Commit 1ba1418

Browse files
author
Bernhard B
committed
Merge branch 'master' of github.com:bbernhard/signal-cli-rest-api
2 parents a5cbb4c + 06744e9 commit 1ba1418

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
ARG SIGNAL_CLI_VERSION=0.12.0
1+
ARG SIGNAL_CLI_VERSION=0.12.1
22
ARG LIBSIGNAL_CLIENT_VERSION=0.30.0
3-
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.12.0-1
3+
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.12.1-1
44

55
ARG SWAG_VERSION=1.6.7
66
ARG GRAALVM_JAVA_VERSION=17

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ This launches a instance of the REST service accessible under http://localhost:9
8282

8383
> :warning: This setting is only needed in normal/native mode!
8484
85-
[signal-cli](https://github.com/AsamK/signal-cli), which this REST API wrapper is based on, recommends to call `receive` on a regular basis. So, if you are not already calling the `receive` endpoint regularily, it is recommended to set the `AUTO_RECEIVE_SCHEDULE` parameter in the docker-compose.yml file. The `AUTO_RECEIVE_SCHEDULE` accepts cron schedule expressions and automatically calls the `receive` endpoint at the given time. e.g: `0 22 * * *` calls `receive` daily at 10pm. If you are not familiar with cron schedule expressions, you can use this [website](https://crontab.guru).
85+
[signal-cli](https://github.com/AsamK/signal-cli), which this REST API wrapper is based on, recommends to call `receive` on a regular basis. So, if you are not already calling the `receive` endpoint regularly, it is recommended to set the `AUTO_RECEIVE_SCHEDULE` parameter in the docker-compose.yml file. The `AUTO_RECEIVE_SCHEDULE` accepts cron schedule expressions and automatically calls the `receive` endpoint at the given time. e.g: `0 22 * * *` calls `receive` daily at 10pm. If you are not familiar with cron schedule expressions, you can use this [website](https://crontab.guru).
8686

8787
**WARNING** Calling `receive` will fetch all the messages for the registered Signal number from the Signal Server! So, if you are using the REST API for receiving messages, it's _not_ a good idea to use the `AUTO_RECEIVE_SCHEDULE` parameter, as you might lose some messages that way.
8888

@@ -127,6 +127,8 @@ There are a bunch of environmental variables that can be set inside the docker c
127127

128128
* `SWAGGER_IP`: The IP that's used in the Swagger UI for the interactive examples. Defaults to the container ip.
129129

130+
* `PORT`: Defaults to port `8080` unless this env var is set to tell it otherwise.
131+
130132
## Clients & Libraries
131133

132134
| Name | Client | Library | Language | Maintainer |

src/api/api.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ func StringToBool(input string) bool {
473473
// @Param timeout query string false "Receive timeout in seconds (default: 1)"
474474
// @Param ignore_attachments query string false "Specify whether the attachments of the received message should be ignored" (default: false)"
475475
// @Param ignore_stories query string false "Specify whether stories should be ignored when receiving messages" (default: false)"
476+
// @Param max_messages query string false "Specify the maximum number of messages to receive (default: unlimited)". Not available in json-rpc mode.
476477
// @Router /v1/receive/{number} [get]
477478
func (a *Api) Receive(c *gin.Context) {
478479
number := c.Param("number")
@@ -496,19 +497,26 @@ func (a *Api) Receive(c *gin.Context) {
496497
return
497498
}
498499

500+
maxMessages := c.DefaultQuery("max_messages", "0")
501+
maxMessagesInt, err := strconv.ParseInt(maxMessages, 10, 32)
502+
if err != nil {
503+
c.JSON(400, Error{Msg: "Couldn't process request - max_messages needs to be numeric!"})
504+
return
505+
}
506+
499507
ignoreAttachments := c.DefaultQuery("ignore_attachments", "false")
500508
if ignoreAttachments != "true" && ignoreAttachments != "false" {
501-
c.JSON(400, Error {Msg: "Couldn't process request - ignore_attachments parameter needs to be either 'true' or 'false'"})
509+
c.JSON(400, Error{Msg: "Couldn't process request - ignore_attachments parameter needs to be either 'true' or 'false'"})
502510
return
503511
}
504512

505513
ignoreStories := c.DefaultQuery("ignore_stories", "false")
506514
if ignoreStories != "true" && ignoreStories != "false" {
507-
c.JSON(400, Error {Msg: "Couldn't process request - ignore_stories parameter needs to be either 'true' or 'false'"})
515+
c.JSON(400, Error{Msg: "Couldn't process request - ignore_stories parameter needs to be either 'true' or 'false'"})
508516
return
509517
}
510518

511-
jsonStr, err := a.signalClient.Receive(number, timeoutInt, StringToBool(ignoreAttachments), StringToBool(ignoreStories))
519+
jsonStr, err := a.signalClient.Receive(number, timeoutInt, StringToBool(ignoreAttachments), StringToBool(ignoreStories), maxMessagesInt)
512520
if err != nil {
513521
c.JSON(400, Error{Msg: err.Error()})
514522
return

src/client/cli.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ func (s *CliClient) Execute(wait bool, args []string, stdin string) (string, err
8383
cmd.Stdin = strings.NewReader(stdin)
8484
}
8585
if wait {
86-
var errBuffer bytes.Buffer
87-
var outBuffer bytes.Buffer
88-
cmd.Stderr = &errBuffer
89-
cmd.Stdout = &outBuffer
86+
var combinedOutput bytes.Buffer
87+
cmd.Stdout = &combinedOutput
88+
cmd.Stderr = &combinedOutput
9089

9190
err := cmd.Start()
9291
if err != nil {
@@ -106,11 +105,11 @@ func (s *CliClient) Execute(wait bool, args []string, stdin string) (string, err
106105
return "", errors.New("process killed as timeout reached")
107106
case err := <-done:
108107
if err != nil {
109-
return "", errors.New(errBuffer.String())
108+
return "", errors.New(combinedOutput.String())
110109
}
111110
}
112111

113-
return outBuffer.String(), nil
112+
return combinedOutput.String(), nil
114113
} else {
115114
stdout, err := cmd.StdoutPipe()
116115
if err != nil {

src/client/client.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ func (s *SignalClient) send(number string, message string,
474474

475475
func (s *SignalClient) About() About {
476476
about := About{
477-
SupportedApiVersions: []string{"v1", "v2"},
478-
BuildNr: 2,
479-
Mode: getSignalCliModeString(s.signalCliMode),
480-
Version: utils.GetEnv("BUILD_VERSION", "unset"),
481-
Capabilities: map[string][]string{"v2/send": []string{"quotes", "mentions"}},
482-
}
477+
SupportedApiVersions: []string{"v1", "v2"},
478+
BuildNr: 2,
479+
Mode: getSignalCliModeString(s.signalCliMode),
480+
Version: utils.GetEnv("BUILD_VERSION", "unset"),
481+
Capabilities: map[string][]string{"v2/send": []string{"quotes", "mentions"}},
482+
}
483483
return about
484484
}
485485

@@ -610,7 +610,7 @@ func (s *SignalClient) SendV2(number string, message string, recps []string, bas
610610
return &timestamps, nil
611611
}
612612

613-
func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments bool, ignoreStories bool) (string, error) {
613+
func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments bool, ignoreStories bool, maxMessages int64) (string, error) {
614614
if s.signalCliMode == JsonRpc {
615615
return "", errors.New("Not implemented")
616616
} else {
@@ -624,6 +624,11 @@ func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments b
624624
command = append(command, "--ignore-stories")
625625
}
626626

627+
if maxMessages > 0 {
628+
command = append(command, "--max-messages")
629+
command = append(command, strconv.FormatInt(maxMessages, 10))
630+
}
631+
627632
out, err := s.cliClient.Execute(true, command, "")
628633
if err != nil {
629634
return "", err

0 commit comments

Comments
 (0)