diff --git a/README.md b/README.md new file mode 100644 index 0000000..42fc7e9 --- /dev/null +++ b/README.md @@ -0,0 +1,174 @@ + + + +[![License][license-shield]][license-url] +[![Forks][forks-shield]][forks-url] +[![Stargazers][stars-shield]][stars-url] +[![Issues][issues-shield]][issues-url] + + +
+

Example Google Sheets Integration

+ +

+ Google Apps Script integration to automatically log incoming SMS messages to Google Sheets +
+
+ About The Project + ยท + Getting Started + ยท + Usage + ยท + Contributing +

+
+ + +- [๐ŸŒ About The Project](#-about-the-project) + - [๐Ÿ› ๏ธ Built With](#๏ธ-built-with) +- [๐Ÿš€ Getting Started](#-getting-started) + - [๐Ÿ“‹ Prerequisites](#-prerequisites) + - [โš™๏ธ Installation](#๏ธ-installation) +- [๐Ÿ’ป Usage](#-usage) + - [๐Ÿ“จ Webhook Payload Structure](#-webhook-payload-structure) + - [๐Ÿ”ง Troubleshooting](#-troubleshooting) +- [๐Ÿค Contributing](#-contributing) +- [๐Ÿ“œ License](#-license) + + + +## ๐ŸŒ About The Project + +This project provides a Google Apps Script integration for SMSGate that automatically logs incoming SMS messages to a Google Sheet. When SMS messages are received through the app, a webhook is sent to this script which parses the message data and appends it to your Google Sheet with the following columns: + +- **Timestamp**: When the message was received +- **From**: The phone number that sent the message +- **Message**: The SMS message content +- **DeviceID**: The device ID +- **MessageID**: The unique message ID + +### ๐Ÿ› ๏ธ Built With + +- [Google Apps Script](https://developers.google.com/apps-script) +- [Google Sheets](https://sheets.google.com) + +

back to top

+ + +## ๐Ÿš€ Getting Started + +Follow these instructions to set up the SMSGate Google Sheets integration. + +### ๐Ÿ“‹ Prerequisites + +- A Google account +- Access to Google Sheets +- An SMSGate app on your phone + +### โš™๏ธ Installation + +1. **Create a new Google Sheet** + - Go to [sheets.google.com](https://sheets.google.com) + - Create a new blank spreadsheet + - Name the spreadsheet "SMS Messages" or your preferred name + - Rename the first sheet to "Messages" (this is required by the script) + - Add the following headers to row 1: + + ``` + Timestamp | From | Message | DeviceID | MessageID + ``` + +2. **Open the Apps Script Editor** + - In your Google Sheet, click on `Extensions` > `Apps Script` + - This will open the Apps Script editor in a new tab + +3. **Deploy the Script** + - Remove any existing code in the editor + - Copy the code from [`code.gs`](code.gs) and paste it into the Apps Script editor + - Click the "Save project" icon (floppy disk) + +4. **Deploy the Web App** + - Click on "Deploy" > "New deployment" + - Click on the "Select type" dropdown and choose "Web app" + - Under "Execute as", select "Me (your account)" + - Under "Who has access", select "Anyone" + - Click "Deploy" + - Authorize the script when prompted (you may need to select your Google account) + - Copy the "Web app URL", e.g. + + ``` + https://script.google.com/macros/s/AKfycbx.../exec + ``` + +5. **Configure sms-gate.app Webhook** + - [Register a new webhook](https://docs.sms-gate.app/features/webhooks/#step-2-register-your-webhook-endpoint) with the URL from step 4 + +6. **Test the Setup** + - Send a test SMS message to your phone + - Check your Google Sheet - the message should appear automatically + - Verify all columns are populated correctly + +

back to top

+ + +## ๐Ÿ’ป Usage + +### ๐Ÿ“จ Webhook Payload Structure + +The script expects webhook payloads from SMSGate in the following format: + +```json +{ + "deviceId": "your-device-id", + "payload": { + "messageId": "unique-message-id", + "message": "Hello, this is a test message", + "phoneNumber": "+1234567890", + "receivedAt": "2024-12-01T10:30:00Z" + } +} +``` + +### ๐Ÿ”ง Troubleshooting + +- **Sheet not found**: Ensure your sheet is named exactly "Messages" +- **Data not appearing**: Check that your webhook URL is correct and accessible +- **Authorization errors**: Re-deploy the web app with proper permissions +- **Column mismatch**: Verify your headers match exactly: "Timestamp", "From", "Message", "DeviceID", "MessageID" + +

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 with the tag "enhancement". +Don't forget to give the project a star! Thanks again! + +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 License 2.0. See `LICENSE` for more information. + +

back to top

+ + + +[license-shield]: https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=for-the-badge +[license-url]: https://opensource.org/licenses/Apache-2.0 +[forks-shield]: https://img.shields.io/github/forks/android-sms-gateway/example-webhooks-google-sheets.svg?style=for-the-badge +[forks-url]: https://github.com/android-sms-gateway/example-webhooks-google-sheets/network/members +[stars-shield]: https://img.shields.io/github/stars/android-sms-gateway/example-webhooks-google-sheets.svg?style=for-the-badge +[stars-url]: https://github.com/android-sms-gateway/example-webhooks-google-sheets/stargazers +[issues-shield]: https://img.shields.io/github/issues/android-sms-gateway/example-webhooks-google-sheets.svg?style=for-the-badge +[issues-url]: https://github.com/android-sms-gateway/example-webhooks-google-sheets/issues diff --git a/code.gs b/code.gs new file mode 100644 index 0000000..27e2459 --- /dev/null +++ b/code.gs @@ -0,0 +1,56 @@ +/** + * Google Apps Script to receive incoming SMS messages from sms-gate.app webhooks + * and append them to a Google Sheet. + */ +function doPost(e) { + try { + // Get the active sheet by name + const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Messages'); + if (!sheet) throw new Error("Sheet 'Messages' not found"); + + // Parse incoming JSON body + const body = JSON.parse(e.postData.contents); + + // Extract fields from the webhook payload according to sms-gate.app docs + const { + deviceId, + payload, + } = body; + + if (!payload) { + throw new Error("Missing required field: payload"); + } + + const { + messageId, + message, + phoneNumber, + receivedAt, + } = payload; + + if (!message || !phoneNumber) { + throw new Error("Missing required fields: message or phoneNumber"); + } + + // Append to sheet + sheet.appendRow([ + new Date(receivedAt || Date.now()), + phoneNumber || '', + message || '', + deviceId || '', + messageId || '' + ]); + + // Return success response + return ContentService + .createTextOutput(JSON.stringify({ status: 'ok' })) + .setMimeType(ContentService.MimeType.JSON); + + } catch (err) { + // Handle errors gracefully + Logger.log("Webhook error: " + err.message); + return ContentService + .createTextOutput(JSON.stringify({ status: 'error', message: err.message })) + .setMimeType(ContentService.MimeType.JSON); + } +}