From 70cd5bbaa4c143201ea52e2e8401eed3e520d6f4 Mon Sep 17 00:00:00 2001 From: TangMo Date: Fri, 13 Jan 2023 20:15:44 +0000 Subject: [PATCH] Add Google Apps Script version --- Google Apps Script version/README.md | 21 ++++++++++++++++ .../TangMo_fxtwitter_bot.gs | 25 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Google Apps Script version/README.md create mode 100644 Google Apps Script version/TangMo_fxtwitter_bot.gs diff --git a/Google Apps Script version/README.md b/Google Apps Script version/README.md new file mode 100644 index 0000000..075c99c --- /dev/null +++ b/Google Apps Script version/README.md @@ -0,0 +1,21 @@ +# Tangmo FxTwitter Bot (Google Apps Script version) + +Telegram bot that monitor for a message with just Twitter link without preview and reply with [FxTwitter](https://github.com/FixTweet/FixTweet) link + +1. Register Telegram bot +1. Setup Google Apps Script + 1. Create a Google Sheets + 1. Click Extensions > Apps Script. + 1. Paste the code from this repo, click "Save" + 1. Go to Project Settings tab + 1. Scroll to the bottom, "Script Properties", and add a `TELEGRAM_BOT_TOKEN` property + 1. At the top right of the script project, click Deploy > New deployment > Web app. Run as me, anyone can access + 1. Copy the webapp URL. It should look like https://script.google.com/macros/s/some-long-id/exec +1. Register Web hook + ```sh + curl --request POST \ + --url https://api.telegram.org/bot123:bottokenhere/setWebhook \ + --header 'Content-Type: application/json' \ + --data '{"url": "https://script.google.com/macros/s/some-long-id/exec"}' + ``` +1. It should work now diff --git a/Google Apps Script version/TangMo_fxtwitter_bot.gs b/Google Apps Script version/TangMo_fxtwitter_bot.gs new file mode 100644 index 0000000..b87d642 --- /dev/null +++ b/Google Apps Script version/TangMo_fxtwitter_bot.gs @@ -0,0 +1,25 @@ +function doPost(e) { + const contents = JSON.parse(e.postData.contents); + if (contents.message?.media_group_id || !contents.message?.text) { + return; + } + + const found = contents.message.text.match(/https?:\/\/(?:mobile\.)?twitter\.com\/(\w+)\/status\/(\d+)(\/photo\/\d)?/); + if (found) { + // Feel free to hardcode bot token, reducing PropertiesService usage quota + UrlFetchApp.fetch(`https://api.telegram.org/bot${PropertiesService.getScriptProperties().getProperty('TELEGRAM_BOT_TOKEN')}/`, { + method: "post", + payload: { + method: "sendMessage", + chat_id: String(contents.message.chat.id), + text: `https://fxtwitter.com/${found[1]}/status/${found[2]}${found[3] || ''}`, + reply_to_message_id: String(contents.message.message_id), + disable_notification: true, + } + }); + } +} + +function doGet() { + return HtmlService.createHtmlOutput('Webhook Endpoint for @TangMo_fxtwitter_bot Telegram bot'); +}