SMS Gateway for Android

repository·master·Indexed 26 days ago

https://github.com/capcom6/android-sms-gateway

A programmable SMS gateway that turns an Android smartphone into a gateway for sending and receiving SMS, MMS, and Data SMS via APIs, CLI tools, and webhooks. It supports Local Server and Cloud Server modes, end-to-end encryption, and real-time event notifications for 2FA, transactional messaging, and IoT commands. Requires Android 5.0 or above.

Tokens
2.4K
Snippets
5
Records
11
Agent score
88%

What's inside android-sms-gateway

  1. Overview of SMS Gateway for Android™

    master
    SMS Gateway turns an Android smartphone into an SMS gateway. It allows you to send and receive SMS, MMS, and Data SMS messages programmatically via an API or CLI tool. It supports real-time webhooks for incoming messages and provides end-to-end encryption for message content and recipient phone numbers.
  2. Choose the correct Build Variant

    master

    The project offers two pre-built variants:

    • Secure (release) build: Recommended for production deployments. It includes strict security configurations.
    • Insecure build: Intended only for development and testing. It allows cleartext traffic. Never use this build in public environments.
  3. Configure and use Webhooks for messaging events

    master

    Webhooks allow you to receive POST notifications for messaging events (SMS/MMS) directly from the device.

    Supported Events

    • sms:received: SMS message received
    • sms:sent: SMS message sent
    • sms:delivered: SMS message delivered
    • sms:failed: SMS message failed to send
    • sms:data-received: Data SMS received
    • mms:received: MMS notification received (before download)
    • mms:downloaded: MMS fully downloaded with body and attachments
    • system:ping: Periodic heartbeat

    Setup Steps

    1. Prepare an HTTP server with a valid SSL certificate.
    2. Register the webhook via a POST request to the /webhooks endpoint (use http://<device_local_ip>:8080/webhooks for Local mode or https://api.sms-gate.app/3rdparty/v1/webhooks for Cloud mode).
    3. To deregister, send a DELETE request to /webhooks/<unique-id>.

    Note: The device must have an outgoing internet connection to dispatch webhooks.

    # Register a webhook
    curl -X POST -u <username>:<password> \
      -H "Content-Type: application/json" \
      -d '{ "id": "unique-id", "url": "https://webhook.site/<your-uuid>", "event": "sms:received" }' \
      http://<device_local_ip>:8080/webhooks
    
    # Deregister a webhook
    curl -X DELETE -u <username>:<password> \
      http://<device_local_ip>:8080/webhooks/unique-id
  4. Install SMS Gateway via APK

    master

    To install the application on an Android device:

    1. Download the latest APK from the Releases page.
    2. Transfer the APK to your Android device.
    3. Enable Unknown sources in your device's Settings > Security (or Privacy).
    4. Use a file manager to locate the APK and tap it to install.
    5. Follow the on-screen prompts to complete the installation.
    https://github.com/capcom6/android-sms-gateway/releases
  5. Use Cloud Server mode to send SMS

    master

    Cloud Server mode is used when dealing with dynamic or shared device IP addresses.

    1. Launch the app on your device.
    2. Toggle the Cloud Server switch to "on".
    3. Tap the Online button to connect to the cloud server.
    4. Retrieve the basic authentication credentials (<username> and <password>) from the Cloud Server section in the app.

    To send a message, use a curl command or the CLI tool targeting the cloud API endpoint.

    curl -X POST -u <username>:<password> \
      -H "Content-Type: application/json" \
      -d '{ "textMessage": { "text": "Hello, doctors!" }, "phoneNumbers": ["+19162255887", "+19162255888"] }' \
      https://api.sms-gate.app/3rdparty/v1/message
  6. Use Local Server mode to send SMS via local network

    master

    Local Server mode is ideal for sending messages from within a local network.

    1. Launch the app on your device.
    2. Toggle the Local Server switch to "on".
    3. Tap the Offline button to activate the server.
    4. Retrieve the device's local IP address and basic authentication credentials (<username> and <password>) from the Local Server section in the app.

    To send a message, use a curl command or the CLI tool targeting the device's local IP on port 8080.

    curl -X POST -u <username>:<password> \
      -H "Content-Type: application/json" \
      -d '{ "textMessage": { "text": "Hello, doctors!" }, "phoneNumbers": ["+19162255887", "+19162255888"] }' \
      http://<device_local_ip>:8080/message
  7. Prerequisites and Permissions for SMS Gateway

    master

    Prerequisites

    • An Android device running Android 5.0 (Lollipop) or above.

    Required and Optional Permissions

    To function correctly, the app requires or benefits from the following permissions:

    PermissionRequirementPurpose
    SEND_SMSRequiredRequired to send SMS messages.
    READ_PHONE_STATEOptionalAllows selecting specific SIM cards.
    READ_SMSOptionalAllows reading previously received SMS messages.
    RECEIVE_SMSOptionalRequired to trigger webhooks on incoming SMS.
    RECEIVE_MMSOptionalRequired to trigger webhooks on incoming MMS.
    RECEIVE_WAP_PUSHOptionalRequired to trigger webhooks on incoming MMS.
  8. Reference: Webhook Payload Format

    master

    When a webhook event is triggered, the application dispatches a POST request with a JSON payload. Example for sms:received:

    {
      "event": "sms:received",
      "payload": {
        "messageId": "msg_12345abcde",
        "message": "Received SMS text",
        "phoneNumber": "+19162255887",
        "simNumber": 1,
        "receivedAt": "2024-06-07T11:41:31.000+07:00"
      }
    }
  9. Represent received messages with InboxMessage

    master

    The InboxMessage sealed class is the base type for all incoming messages. It contains common properties for any received message: address (sender), date (timestamp), and an optional subscriptionId (SIM card identifier).

    Depending on the message type, you can use one of the following subclasses:

    • Text: For standard SMS text messages. Contains a text string.
    • Data: For binary data messages. Contains a data byte array.
    • MmsHeaders: Metadata for an MMS message. Contains messageId, transactionId, subject, size, and contentClass.
    • MMS: For Multimedia messages. Contains messageId, body, subject, and a list of Attachment objects.
  10. SmsEventPayload webhook payload structures

    master

    When consuming webhooks from the Android SMS Gateway, SMS event notifications are delivered using one of several specialized payload classes derived from SmsEventPayload. These payloads provide details about the lifecycle of an SMS message (sent, delivered, failed, received, etc.).

    Common fields across all payloads include:

    • messageId: Unique identifier for the message.
    • sender: The sender's phone number.
    • recipient: The recipient's phone number.
    • simNumber: The index of the SIM card used (if applicable).
    • phoneNumber: The primary phone number associated with the event (either sender or recipient depending on the event type).
    // Available SmsEventPayload subclasses:
    
    class SmsSent(
        messageId: String,
        sender: String?,
        recipient: String,
        simNumber: Int?,
        val partsCount: Int,
        val sentAt: Date
    )
    
    class SmsDelivered(
        messageId: String,
        sender: String?,
        recipient: String,
        simNumber: Int?,
        val deliveredAt: Date
    )
    
    class SmsFailed(
        messageId: String,
        sender: String?,
        recipient: String,
        simNumber: Int?,
        val failedAt: Date,
        val reason: String
    )
    
    class SmsReceived(
        messageId: String,
        sender: String,
        recipient: String?,
        simNumber: Int?,
        val message: String,
        val receivedAt: Date
    )
    
    class SmsDataReceived(
        messageId: String,
        sender: String,
        recipient: String?,
        simNumber: Int?,
        val data: String,
        val receivedAt: Date
    )
    
    class SmsCancelled(
        messageId: String,
        sender: String?,
        recipient: String,
        simNumber: Int?,
        val cancelledAt: Date
    )
  11. Access MMS attachments via MMS.Attachment

    master

    When handling an MMS type InboxMessage, you can access its multimedia components through the attachments list. Each Attachment object provides details about a specific part of the MMS:

    • partId: Unique identifier for the part.
    • contentType: The MIME type of the attachment (e.g., image/jpeg).
    • name: The filename of the attachment.
    • size: The size of the attachment in bytes.
    • data: The attachment content represented as a string.