Skip to content

android-sms-gateway/example-telegram-forwarder-fn

Repository files navigation

License Issues Stars


Example Telegram Forwarder Function

Zero-dependency cloud function to forward SMS messages to Telegram
Explore the repo »

Report Bug · Request Feature

About The Project

This repository contains a zero-dependency cloud function that forwards incoming SMS messages received through SMSGate App webhooks to a specified Telegram chat. The entire implementation is a single Node.js file with no external packages.

(back to top)

Built With

  • Node.js — native fetch, zero dependencies
  • Telegram — Bot API (sendMessage)

(back to top)

Getting Started

Prerequisites

  • Node.js 18+ (required for native fetch)
  • SMSGate App installed on your device in Local, Cloud, or Private mode
  • A Telegram bot and its token — create one via BotFather
  • A Telegram chat ID where messages will be forwarded

Setup

  1. Clone the repository
    git clone https://github.com/android-sms-gateway/example-telegram-forwarder-fn.git
  2. No dependencies to install — the function uses only Node.js built-in APIs. Run npm install only if you add external packages.
  3. Set up your environment variables (see Environment Variables)
  4. Deploy to your preferred cloud provider (AWS Lambda, Google Cloud Functions, etc.). The handler uses the standard (event, context) FaaS signature. You may need to adapt the entry point — consult your provider's documentation.
  5. Register a webhook in SMSGate App with the URL of your deployed function, appending ?apiKey=YOUR_API_KEY as a query parameter.

Environment Variables

Variable Description
API_KEY Random secret for webhook authentication, passed as the apiKey query parameter
TELEGRAM_BOT_TOKEN Token of your Telegram bot (from BotFather)
TELEGRAM_CHAT_ID ID of the Telegram chat where messages will be forwarded

(back to top)

Usage

The function is triggered by incoming webhook events from SMSGate App. Upon receiving an SMS, it:

  1. Validates the apiKey query parameter against API_KEY (returns 401 Unauthorized on mismatch)
  2. Parses the webhook JSON payload — expects payload.phoneNumber and payload.message
  3. Escapes HTML special characters in the message body
  4. Sends a formatted message to Telegram via the Bot API (sendMessage) with HTML parse mode
  5. Returns 200 OK on success

(back to top)

Testing

A requests.http file is provided for manual testing with the REST Client extension for VS Code. It includes:

  • Endpoints to verify your Telegram bot (getMe, getUpdates, getWebhookInfo)
  • A sample POST request against a deployed Lambda URL with a realistic SMS webhook payload

For local testing with the REST Client extension: create a .env file (use .env.example as a template) — the $dotenv placeholders in requests.http read from it automatically. Then open requests.http and click Send Request above each block. For cloud or hosted deployments, set these variables through your provider's environment configuration (e.g., AWS Lambda environment variables, Google Cloud Functions --set-env-vars) instead of a .env file.

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue. Don't forget to give the project a star!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the Apache-2.0 License. See LICENSE for more information.

(back to top)

Contact

Project Link: https://github.com/android-sms-gateway/example-telegram-forwarder-fn

(back to top)

Acknowledgments

(back to top)

About

A Cloud Function to seamlessly forward SMS messages from SMS Gateway for Android™ to a Telegram chat using webhooks.

Topics

Resources

License

Stars

3 stars

Watchers

1 watching

Forks

Contributors