Merge branch 'master' of github.com:bbernhard/signal-cli-rest-api

This commit is contained in:
Bernhard B
2023-09-08 19:55:01 +02:00
5 changed files with 33 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
ARG SIGNAL_CLI_VERSION=0.12.0 ARG SIGNAL_CLI_VERSION=0.12.1
ARG LIBSIGNAL_CLIENT_VERSION=0.30.0 ARG LIBSIGNAL_CLIENT_VERSION=0.30.0
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.12.0-1 ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.12.1-1
ARG SWAG_VERSION=1.6.7 ARG SWAG_VERSION=1.6.7
ARG GRAALVM_JAVA_VERSION=17 ARG GRAALVM_JAVA_VERSION=17

View File

@@ -82,7 +82,7 @@ This launches a instance of the REST service accessible under http://localhost:9
> :warning: This setting is only needed in normal/native mode! > :warning: This setting is only needed in normal/native mode!
[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). [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).
**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. **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.
@@ -127,6 +127,8 @@ There are a bunch of environmental variables that can be set inside the docker c
* `SWAGGER_IP`: The IP that's used in the Swagger UI for the interactive examples. Defaults to the container ip. * `SWAGGER_IP`: The IP that's used in the Swagger UI for the interactive examples. Defaults to the container ip.
* `PORT`: Defaults to port `8080` unless this env var is set to tell it otherwise.
## Clients & Libraries ## Clients & Libraries
| Name | Client | Library | Language | Maintainer | | Name | Client | Library | Language | Maintainer |

View File

@@ -473,6 +473,7 @@ func StringToBool(input string) bool {
// @Param timeout query string false "Receive timeout in seconds (default: 1)" // @Param timeout query string false "Receive timeout in seconds (default: 1)"
// @Param ignore_attachments query string false "Specify whether the attachments of the received message should be ignored" (default: false)" // @Param ignore_attachments query string false "Specify whether the attachments of the received message should be ignored" (default: false)"
// @Param ignore_stories query string false "Specify whether stories should be ignored when receiving messages" (default: false)" // @Param ignore_stories query string false "Specify whether stories should be ignored when receiving messages" (default: false)"
// @Param max_messages query string false "Specify the maximum number of messages to receive (default: unlimited)". Not available in json-rpc mode.
// @Router /v1/receive/{number} [get] // @Router /v1/receive/{number} [get]
func (a *Api) Receive(c *gin.Context) { func (a *Api) Receive(c *gin.Context) {
number := c.Param("number") number := c.Param("number")
@@ -496,6 +497,13 @@ func (a *Api) Receive(c *gin.Context) {
return return
} }
maxMessages := c.DefaultQuery("max_messages", "0")
maxMessagesInt, err := strconv.ParseInt(maxMessages, 10, 32)
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - max_messages needs to be numeric!"})
return
}
ignoreAttachments := c.DefaultQuery("ignore_attachments", "false") ignoreAttachments := c.DefaultQuery("ignore_attachments", "false")
if ignoreAttachments != "true" && ignoreAttachments != "false" { if ignoreAttachments != "true" && ignoreAttachments != "false" {
c.JSON(400, Error{Msg: "Couldn't process request - ignore_attachments parameter needs to be either 'true' or 'false'"}) c.JSON(400, Error{Msg: "Couldn't process request - ignore_attachments parameter needs to be either 'true' or 'false'"})
@@ -508,7 +516,7 @@ func (a *Api) Receive(c *gin.Context) {
return return
} }
jsonStr, err := a.signalClient.Receive(number, timeoutInt, StringToBool(ignoreAttachments), StringToBool(ignoreStories)) jsonStr, err := a.signalClient.Receive(number, timeoutInt, StringToBool(ignoreAttachments), StringToBool(ignoreStories), maxMessagesInt)
if err != nil { if err != nil {
c.JSON(400, Error{Msg: err.Error()}) c.JSON(400, Error{Msg: err.Error()})
return return

View File

@@ -83,10 +83,9 @@ func (s *CliClient) Execute(wait bool, args []string, stdin string) (string, err
cmd.Stdin = strings.NewReader(stdin) cmd.Stdin = strings.NewReader(stdin)
} }
if wait { if wait {
var errBuffer bytes.Buffer var combinedOutput bytes.Buffer
var outBuffer bytes.Buffer cmd.Stdout = &combinedOutput
cmd.Stderr = &errBuffer cmd.Stderr = &combinedOutput
cmd.Stdout = &outBuffer
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
@@ -106,11 +105,11 @@ func (s *CliClient) Execute(wait bool, args []string, stdin string) (string, 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(combinedOutput.String())
} }
} }
return outBuffer.String(), nil return combinedOutput.String(), nil
} else { } else {
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {

View File

@@ -610,7 +610,7 @@ func (s *SignalClient) SendV2(number string, message string, recps []string, bas
return &timestamps, nil return &timestamps, nil
} }
func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments bool, ignoreStories bool) (string, error) { func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments bool, ignoreStories bool, maxMessages int64) (string, error) {
if s.signalCliMode == JsonRpc { if s.signalCliMode == JsonRpc {
return "", errors.New("Not implemented") return "", errors.New("Not implemented")
} else { } else {
@@ -624,6 +624,11 @@ func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments b
command = append(command, "--ignore-stories") command = append(command, "--ignore-stories")
} }
if maxMessages > 0 {
command = append(command, "--max-messages")
command = append(command, strconv.FormatInt(maxMessages, 10))
}
out, err := s.cliClient.Execute(true, command, "") out, err := s.cliClient.Execute(true, command, "")
if err != nil { if err != nil {
return "", err return "", err