signal-cli-rest-api

repository·master·Indexed 25 days ago

https://github.com/bbernhard/signal-cli-rest-api

A Dockerized REST API wrapper around signal-cli that enables developers to interact with Signal Messenger via HTTP requests. It supports sending messages, managing groups, and receiving messages. The service offers multiple execution modes (normal, native, json-rpc, and json-rpc-native) to balance latency and memory usage, and includes a plugin system using Lua for extending API functionality, such as the Persistence Plugin for storing messages in a SQLite3 database.

Tokens
14.5K
Snippets
26
Records
107
Agent score
82%

What's inside signal-cli-rest-api

  1. Link an existing Signal account as a secondary device

    master

    To use the REST API with an existing mobile Signal account, use the QR code linking method. This allows the API to act as a secondary device on your behalf.

    1. Request a QR code link by calling the /v1/qrcodelink endpoint with a device_name.
    2. If the response warns about binary output, save the response to a file using --output img.jpg.
    3. Scan the resulting QR code image with your primary Signal mobile app.

    Note: The QR code changes with every request for security reasons.

    Troubleshooting Binary Output: If curl returns a warning about binary output, use the --output flag to save the image.

    curl -X GET -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/qrcodelink?device_name=HomeAssistant' --output img.jpg
  2. Configure plugin files in Docker

    master

    A plugin consists of a definition file (.def) and a matching Lua script (.lua) with the same filename. These files must be stored in a plugins folder on your host and bind-mounted into the /plugins directory inside the Docker container.

    services:
      signal-cli-rest-api:
        image: bbernhard/signal-cli-rest-api:latest
      environment:
        - MODE=json-rpc
        - ENABLE_PLUGINS=true
      volumes:
        - "./signal-cli-config:/home/.local/share/signal-cli"
        - "./plugins:/plugins" #map "plugins" folder on host system into docker container.
  3. Run the API documentation using Docker Compose

    master

    The recommended way to view the API documentation is via Docker Compose. This builds the documentation and starts the web server.

    1. Build the docs: docker compose build
    2. Serve the docs: docker compose up
    3. Access the documentation at http://127.0.0.1:8080/swagger/index.html.

    Note: If you encounter a Network error, replace 127.0.0.1 with the Docker internal IP (e.g., http://172.18.0.2:8080/swagger/index.html).

    docker compose build
    docker compose up
  4. Run ARM64 Docker containers on x86-64 systems

    master

    To run an ARM64 version of the signal-cli-rest-api image on an x86-64 host, you must first enable multi-architecture support using QEMU.

    1. Register the QEMU user static binaries: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    2. Pull the specific ARM64 image using its SHA256 hash (found on Docker Hub).
    3. Run the container using that same SHA256 hash.
  5. Enable the Plugins feature

    master

    To use custom plugins, you must enable the feature by setting the ENABLE_PLUGINS environment variable to true. This is typically done in your docker-compose.yml file.

    services:                                                                                                                                                                                                        
      signal-cli-rest-api:
        image: bbernhard/signal-cli-rest-api:latest
        environment:
          - MODE=json-rpc #supported modes: json-rpc, native, normal
          - ENABLE_PLUGINS=true #enable plugins
  6. Create a Signal Messenger group

    master

    You can create new Signal groups via the API.

    1. Create Group: Send a POST request to /v1/groups/<number> with a JSON body containing the name and an array of members (phone numbers).
    2. Get Group ID: After creation, perform a GET request to /v1/groups/<number> to retrieve the group_id. This ID is required for integrations like Home Assistant.

    Troubleshooting Empty Group Lists: If groups do not appear in the list, ensure you are using json-rpc mode and try calling the /v1/receive/<number> endpoint to force the API to fetch data from Signal servers.

  7. Enable the Persistence Plugin

    master

    The Persistence Plugin writes every received message to a SQLite3 database. To enable it, follow these steps:

    1. Prepare Plugin Files: Download persist-message.def, persist-message.lua, query-message.def, and query-message.lua and place them in a plugins folder on your host filesystem.
    2. Prepare Storage: Create a persistence folder on your host system where the messages.db will be stored.
    3. Configure Docker: Update your docker-compose.yml to enable plugins, map the plugin and persistence directories, and register the internal webhook endpoint.
    4. Restart: Restart your Docker container.

    Once active, every received message is written to messages.db inside your persistence folder.

    services:                                                                                                                                                                                                        
      signal-cli-rest-api:
        image: bbernhard/signal-cli-rest-api:latest
        environment:
          - MODE=json-rpc #supported modes: json-rpc, native, normal (choose the mode you want; the plugin works with all modes)
          - ENABLE_PLUGINS=true # enable plugins
          - "./plugins:/plugins" #map "plugins" folder from the host system into the docker container
          - "./persistence;/persistence" #map "persistence" folder from the host system into the docker container
          - RECEIVE_WEBHOOK_URL=http://127.0.0.1:8080/v1/plugins/persistence/persist-message #register an internal webhook endpoint
  8. Generate API documentation locally

    master

    To generate documentation locally, you must have Go installed. Follow these steps:

    1. Navigate to the src directory: cd src
    2. Generate the docs using swag. You can use one of the following three methods:

    Option 1: Via Go

    go run github.com/swaggo/swag/cmd/swag@v1.16.6 init --requiredByDefault --outputTypes "go,json"

    Option 2: Directly with swag

    swag init --requiredByDefault --outputTypes "go,json"

    Option 3: Via Docker

    docker run --rm -v $(pwd):/code ghcr.io/swaggo/swag:latest init --requiredByDefault --outputTypes "go,json"
    1. After generation, navigate to the docs directory: cd docs
    2. Add the signal-cli receive V1 schemas:
      • Download the signal-cli-x.y.z-json-schemas.tar.gz schema files from the signal-cli releases.
      • Extract the files.
      • Run the schema addition script: go run add_v1_receive_schemas.go ./path-to-signal-cli-json-schema-folder
    cd src
    go run github.com/swaggo/swag/cmd/swag@v1.16.6 init --requiredByDefault --outputTypes "go,json"
    cd docs
    go run add_v1_receive_schemas.go ./path-to-signal-cli-json-schema-folder
  9. Register a new Signal phone number

    master

    You can register a new phone number (including land-lines) via the REST API.

    1. Initiate Registration: Send a POST request to /v1/register/<number>. Use the use_voice: true parameter if registering a land-line to receive the verification token via a phone call instead of SMS.
    2. Verify Registration: Once you receive the token, send a POST request to /v1/register/<number>/verify/<verification code> to complete the process.

    Handling Captchas: If the API returns {"error":"Captcha required for verification (null)\n"}, you must generate a captcha at https://signalcaptchas.org/registration/generate.html, solve it, and copy the signalcaptcha:// link to use in the captcha field of your registration request.

  10. Debug signal-cli commands by isolating them from the REST API

    master
    Once debug mode is enabled, execute the API command you are troubleshooting (e.g., /v2/send). Check the docker container logs to find the exact signal-cli command being executed. The logs will provide a step-by-step guide to running that command manually inside the container. This allows you to bypass the REST API wrapper to verify if the issue persists with signal-cli directly.
  11. Build libsignal-client using cross

    master

    To avoid long build times during Docker image creation, you can manually build libsignal-client using the cross tool for cross-compilation.

    Prerequisites:

    1. Rust Installation: You must have Rust installed via rustup. Failure to do so may result in a toolchain is not fully qualified error.
    2. Docker Permissions: Your user must have permission to communicate with the Docker daemon. It is recommended to add your user to the docker group to avoid running commands as root.

    Build Steps:

    1. Download a new release from the libsignal-client releases page.
    2. Unzip the release and enter the directory.
    3. Clean previous build artifacts: rm -rf target.
    4. Copy the Cross.toml file from this repository into the downloaded libsignal-client folder.
    5. Run the appropriate cross build command for your target architecture(s).

    The resulting libraries will be located in the target/<architecture>/release folder.