Skip to content

Redirect SMS and other messages to Telegram via Webhook

License

Notifications You must be signed in to change notification settings

AlexIII/messageredir

Repository files navigation

Message Redir: Redirect Messages to Telegram via Webhook

This app serves a webhook that forwards messages to a Telegram bot.

Designed to work with SMS to URL Forwarder (Android app).

Other potential use cases include automated notifications from server monitoring systems, Grafana, etc.

This bot is multi-user, meaning you can connect with different Telegram accounts, and forward messages separately.

Security Considerations

  • Do not use other people's servers or bots. They will have access to your messages.
  • Configure HTTPS! Either with an Nginx reverse-proxy or use the built-in HTTPS support. Even without a valid certificate, it offers partial security.

Usage

  1. Run the server

Run the server in a single docker command

Prebuilt image is available on dockerhub: https://hub.docker.com/r/alex3iii/messageredir

docker run -d --name messageredir-inst -e MREDIR_TG_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN" -v "$(pwd)/messageredir.db:/root/app/messageredir.db" -p 8089:8080 alex3iii/messageredir:latest

This will serve HTTP by default. You can pass MREDIR_TLS_CERT_FILE and MREDIR_TLS_KEY_FILE environment variables to enable HTTPS.

Or use docker compose (recommended)

i. Copy docker-compose.yaml to an empty directory on your server.

wget https://github.com/AlexIII/messageredir/raw/refs/heads/main/docker-compose.yaml -O docker-compose.yaml

ii. Create an .env file in the same directory and set your configuration there. Example:

MREDIR_TG_BOT_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN>
MREDIR_LOG_USER_MESSAGES=true
MREDIR_TLS_CERT_FILE=tls/tls.crt # Self-signed certificate is generated by Docker Compose. When MREDIR_TLS_* are specified, the app will serve HTTPS.
MREDIR_TLS_KEY_FILE=tls/tls.key

iii. Run:

docker compose up -d

Remove -d on the first try to see the app log (errors, etc).

The default HTTP(S) port is 8089. Create docker-compose.override.yaml to override the port and/or other settings.

See logs with docker compose logs.

iv. Later, if you want to update to the latest version:

docker compose down
wget https://github.com/AlexIII/messageredir/raw/refs/heads/main/docker-compose.yaml -O docker-compose.yaml
docker pull alex3iii/messageredir:latest
docker compose up -d
  1. After the server is running, write to the bot from your Telegram account. The \start command will return an access token.

  2. Configure the SMS to URL Forwarder app to use this URL: http(s)://<SERVER_IP_OR_HOSTNAME>:<SERVER_PORT>/<TOKEN_THE_BOT_ISSUED_FOR_YOU>/smstourlforwarder When using HTTPS with a self-signed certificate, check the "ignore certificate" checkbox in the SMS to URL Forwarder app.

Configuration

All properties are optional, except tgBotToken / TG_BOT_TOKEN.

YAML Property Environment Variable Type Description
tgBotToken MREDIR_TG_BOT_TOKEN string Telegram bot token
dbFileName MREDIR_DB_FILE_NAME string Database file name
userTokenLength MREDIR_USER_TOKEN_LENGTH int User token length
logUserMessages MREDIR_LOG_USER_MESSAGES bool Log user messages
restApiPort MREDIR_REST_API_PORT int REST API port
tlsCertFile MREDIR_TLS_CERT_FILE string TLS certificate file
tlsKeyFile MREDIR_TLS_KEY_FILE string TLS key file
logFileName MREDIR_LOG_FILE_NAME string Log file name
MyHost MREDIR_MY_HOST string Hostname:Port

How to Post a Message to the Hook

HTTP POST http://<SERVER_IP_OR_HOSTNAME>:<SERVER_PORT>/<TOKEN_THE_BOT_ISSUED_FOR_YOU>/smstourlforwarder

Body format:

{
    "from": "%from%",
    "text": "%text%",
    "sentStamp": %sentStampMs%,
    "receivedStamp": %receivedStampMs%,
    "sim": "%sim%"
}

License

MIT License © github.com/AlexIII

About

Redirect SMS and other messages to Telegram via Webhook

Topics

Resources

License

Stars

Watchers

Forks