diff --git a/.github/workflows/joining-tweet.yaml b/.github/workflows/joining-tweet.yaml deleted file mode 100644 index eb8d73b8..00000000 --- a/.github/workflows/joining-tweet.yaml +++ /dev/null @@ -1,38 +0,0 @@ -name: Joining Tweet - -on: - schedule: - - cron: "20 16 * * 6" # Every Saturday at 4:20 PM UTC (i.e., 09:50 PM IST) - workflow_dispatch: - -jobs: - send-joining-tweet: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - persist-credentials: false - - - name: Set environment variable - run: | - sudo apt-get update - sudo apt-get install nodejs - catchup_number=$(node util/get-next-catchup-number.js) - echo "CATCHUP_NUMBER=${catchup_number}" >> ${GITHUB_ENV} - - - name: Send joining Tweet - uses: Eomm/why-don-t-you-tweet@v1 - with: - tweet-message: | - Going live in 10 mins! - - Join us for the ${{ env.CATCHUP_NUMBER }} #OTCCatchUp, an informal open-to-all Tech discussion. - - Join in at any time! 👇 - https://catchup.ourtech.community/attend - env: - TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_API_KEY }} - TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_API_KEY_SECRET }} - TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} - TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} diff --git a/.github/workflows/reminder-tweet.yaml b/.github/workflows/reminder-tweet.yaml deleted file mode 100644 index bfced24f..00000000 --- a/.github/workflows/reminder-tweet.yaml +++ /dev/null @@ -1,49 +0,0 @@ -name: Reminder Tweet - -on: - schedule: - - cron: "30 11 * * 6" # Every Saturday at 11:30 AM UTC (i.e., 5 PM IST) - workflow_dispatch: - -jobs: - send-reminder-tweet: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - persist-credentials: false - - - name: Set environment variables - run: | - sudo apt-get update - sudo apt-get install nodejs - - upcoming_catchup_number=$(node util/get-next-catchup-number.js) - echo "UPCOMING_CATCHUP_NUMBER=${upcoming_catchup_number}" >> ${GITHUB_ENV} - - prev_catchup_number=$(node util/get-latest-catchup-number.js) - echo "PREV_CATCHUP_NUMBER=${prev_catchup_number}" >> ${GITHUB_ENV} - - catchup_date=$(date +"%b %d") - echo "CATCHUP_DATE=${catchup_date}" >> ${GITHUB_ENV} - - - name: Send reminder Tweet - uses: Eomm/why-don-t-you-tweet@v1 - with: - tweet-message: | - Reminder! 🚨 - - The ${{ env.UPCOMING_CATCHUP_NUMBER }} OTC CatchUp session will be held today (${{ env.CATCHUP_DATE }}) from 10:30 PM IST. - - Join us for an informal open-to-all Tech discussion! - - Previous session details 👇 - https://catchup.ourtech.community/summary/${{ env.PREV_CATCHUP_NUMBER }} - - #OTC #OTCCatchUp #Community #Tech - env: - TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_API_KEY }} - TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_API_KEY_SECRET }} - TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} - TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} diff --git a/.github/workflows/send-messages.yaml b/.github/workflows/send-messages.yaml new file mode 100644 index 00000000..ee7bcd06 --- /dev/null +++ b/.github/workflows/send-messages.yaml @@ -0,0 +1,129 @@ +name: Reminder Tweet + +on: + schedule: + # Schedule job 10 minutes before time to account for delays + # Every Saturday at 11:20 AM UTC (05:00 PM IST - 10 min) + - cron: "20 11 * * SAT" + # Every Saturday at 04:30 PM UTC (10:10 PM IST - 10 min) + - cron: "30 16 * * SAT" + workflow_dispatch: + inputs: + message-template: + type: choice + required: true + default: automatic + options: + - automatic + - reminder + - joining + description: | + Which message template to use. + if set to `automatic`, resolves to: + `joining`, if time is after 10pm on Saturday (IST) + `reminder`, otherwise + +jobs: + send-messages: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + persist-credentials: false + + - name: Set environment variables + run: | + upcoming_catchup_number=$(node util/get-next-catchup-number.js) + echo "UPCOMING_CATCHUP_NUMBER=${upcoming_catchup_number}" >> ${GITHUB_ENV} + + prev_catchup_number=$(node util/get-latest-catchup-number.js) + echo "PREV_CATCHUP_NUMBER=${prev_catchup_number}" >> ${GITHUB_ENV} + + catchup_date=$(date +"%b %d") + echo "CATCHUP_DATE=${catchup_date}" >> ${GITHUB_ENV} + + message_template=${{ inputs.message-template }} + if [ "${message_template}" = "automatic" ]; then + message_template=$(node util/get-current-message-template.js) + fi + echo "MESSAGE_TEMPLATE=${message_template}" >> ${GITHUB_ENV} + + # Reminder messages + - name: Wait until 17:00 IST + if: ${{ env.MESSAGE_TEMPLATE }} == 'reminder' + run: sleep $(node util\get-seconds-until-ist-time.js 17:00) + + - name: Send reminder Telegram message + if: ${{ env.MESSAGE_TEMPLATE }} == 'reminder' + run: | + export TELEGRAM_MESSAGE="Reminder! 🚨 + + The ${{ env.UPCOMING_CATCHUP_NUMBER }} OTC CatchUp session will be held today (${{ env.CATCHUP_DATE }}) from 10:30 PM IST. + + Join us for an informal open-to-all Tech discussion! + + Joining link 👇 + https://catchup.ourtech.community/attend + + Previous session details 👇 + https://catchup.ourtech.community/summary/${{ env.PREV_CATCHUP_NUMBER }}" + + BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} node util/post-telegram-message.js + + - name: Send reminder Tweet + if: ${{ env.MESSAGE_TEMPLATE }} == 'reminder' + uses: Eomm/why-don-t-you-tweet@v1 + with: + tweet-message: | + Reminder! 🚨 + + The ${{ env.UPCOMING_CATCHUP_NUMBER }} OTC CatchUp session will be held today (${{ env.CATCHUP_DATE }}) from 10:30 PM IST. + + Join us for an informal open-to-all Tech discussion! + + Previous session details 👇 + https://catchup.ourtech.community/summary/${{ env.PREV_CATCHUP_NUMBER }} + + #OTC #OTCCatchUp #Community #Tech + env: + TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_API_KEY }} + TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_API_KEY_SECRET }} + TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} + TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} + + # Joining messages + - name: Wait until 22:10 IST + if: ${{ env.MESSAGE_TEMPLATE }} == 'joining' + run: sleep $(node util/get-seconds-until-ist-time.js 22:10) + + - name: Send joining Telegram message + if: ${{ env.MESSAGE_TEMPLATE }} == 'joining' + run: | + export TELEGRAM_MESSAGE="${{ env.UPCOMING_CATCHUP_NUMBER }} OTC CatchUp + + An informal open-to-all Tech discussion! + + We start at 10:30 PM IST. + + Join in at any time! 👇 + https://catchup.ourtech.community/attend" + + BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} node util/post-telegram-message.js + + - name: Send joining Tweet + if: ${{ env.MESSAGE_TEMPLATE }} == 'joining' + uses: Eomm/why-don-t-you-tweet@v1 + with: + tweet-message: | + Going live in 30 mins! + + Join us for the ${{ env.UPCOMING_CATCHUP_NUMBER }} #OTCCatchUp, an informal open-to-all Tech discussion. + + Join in at any time! 👇 + https://catchup.ourtech.community/attend + env: + TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_API_KEY }} + TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_API_KEY_SECRET }} + TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} + TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} diff --git a/.github/workflows/tg-joining-msg.yaml b/.github/workflows/tg-joining-msg.yaml deleted file mode 100644 index d80e5945..00000000 --- a/.github/workflows/tg-joining-msg.yaml +++ /dev/null @@ -1,38 +0,0 @@ -name: Telegram joining message - -on: - schedule: - - cron: "30 16 * * 6" # Every Saturday at 4:30 PM UTC (i.e., 10:00 PM IST) - workflow_dispatch: - -jobs: - send-joining-msg: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - persist-credentials: false - - - name: Set environment variable - run: | - sudo apt-get update - sudo apt-get install nodejs - catchup_number=$(node util/get-next-catchup-number.js) - echo "CATCHUP_NUMBER=${catchup_number}" >> ${GITHUB_ENV} - - - name: Send joining message on OTC's Telegram group - uses: appleboy/telegram-action@master - with: - to: ${{ secrets.TELEGRAM_CHAT_ID }} - token: ${{ secrets.TELEGRAM_BOT_TOKEN }} - format: markdown - message: | - **${{ env.CATCHUP_NUMBER }} OTC CatchUp** - - An informal open-to-all Tech discussion! - - We start at 10:30 PM IST. - - Join in at any time! 👇 - https://catchup.ourtech.community/attend diff --git a/.github/workflows/tg-reminder-msg.yaml b/.github/workflows/tg-reminder-msg.yaml deleted file mode 100644 index 9eaa0155..00000000 --- a/.github/workflows/tg-reminder-msg.yaml +++ /dev/null @@ -1,49 +0,0 @@ -name: Telegram reminder message - -on: - schedule: - - cron: "30 11 * * 6" # Every Saturday at 11:30 AM UTC (i.e., 5 PM IST) - workflow_dispatch: - -jobs: - send-reminder-message: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - persist-credentials: false - - - name: Set environment variables - run: | - sudo apt-get update - sudo apt-get install nodejs - - upcoming_catchup_number=$(node util/get-next-catchup-number.js) - echo "UPCOMING_CATCHUP_NUMBER=${upcoming_catchup_number}" >> ${GITHUB_ENV} - - prev_catchup_number=$(node util/get-latest-catchup-number.js) - echo "PREV_CATCHUP_NUMBER=${prev_catchup_number}" >> ${GITHUB_ENV} - - catchup_date=$(date +"%b %d") - echo "CATCHUP_DATE=${catchup_date}" >> ${GITHUB_ENV} - - - name: Send reminder message on OTC's Telegram group - uses: appleboy/telegram-action@master - with: - to: ${{ secrets.TELEGRAM_CHAT_ID }} - token: ${{ secrets.TELEGRAM_BOT_TOKEN }} - format: markdown - disable_web_page_preview: true - message: | - Reminder! 🚨 - - The ${{ env.UPCOMING_CATCHUP_NUMBER }} OTC CatchUp session will be held today (${{ env.CATCHUP_DATE }}) from 10:30 PM IST. - - Join us for an informal open-to-all Tech discussion! - - Joining link 👇 - https://catchup.ourtech.community/attend - - Previous session details 👇 - https://catchup.ourtech.community/summary/${{ env.PREV_CATCHUP_NUMBER }} diff --git a/util/get-current-message-template.js b/util/get-current-message-template.js new file mode 100644 index 00000000..a71e58b0 --- /dev/null +++ b/util/get-current-message-template.js @@ -0,0 +1,19 @@ +function getCurrentMessageTemplate(date = new Date()) { + let istDate = new Date( + date.toLocaleString("en-US", { + timeZone: "Asia/Kolkata" + }) + ); + + // if time is after 10pm on saturday return joining, + // else return reminder + // 6 is Saturday + if (istDate.getDay() === 6 && istDate.getHours() >= 22) return "joining"; + else return "reminder"; +} + +module.exports = { + getCurrentMessageTemplate +}; + +if (require.main) console.log(getCurrentMessageTemplate()); diff --git a/util/get-seconds-until-ist-time.js b/util/get-seconds-until-ist-time.js new file mode 100644 index 00000000..f2a89fd6 --- /dev/null +++ b/util/get-seconds-until-ist-time.js @@ -0,0 +1,18 @@ +const time = process.argv[2]; +const [hours, minutes] = time.split(":"); +if (!time || !hours || !minutes) { + console.error(`invalid timestamp: ${time}`); + process.exit(1); +} + +let now = new Date( + new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata" + }) +); + +let date = new Date(now); + +let seconds = Math.floor((date.getTime() - now.getTime()) / 1000); +if (seconds < 0) seconds = 0; +console.log(seconds); diff --git a/util/post-telegram-message.js b/util/post-telegram-message.js new file mode 100644 index 00000000..a3e3b8f7 --- /dev/null +++ b/util/post-telegram-message.js @@ -0,0 +1,93 @@ +const https = require("https"); + +async function fetch(url, options = {}) { + return new Promise((resolve, reject) => { + let req = https.request(url, options, (res) => { + let data = []; + res.on("data", (d) => data.push(d)); + res.on("end", (e) => { + let rawData = Buffer.concat(data).toString(); + resolve({ + headers: res.headers, + status: res.statusCode, + text: async () => rawData, + json: async () => JSON.parse(rawData) + }); + }); + }); + + req.on("error", reject); + + if (options.body && options.method != "GET" && options.method != "HEAD") + req.write(options.body); + + req.end(); + }); +} + +let BOT_TOKEN; + +const BASE_URL = "https://api.telegram.org"; +async function sendApiRequestRaw(method, body = {}) { + let url = `${BASE_URL}/bot${BOT_TOKEN}/${method}`; + return fetch(url, { + method: "POST", + headers: { + "content-type": "application/json" + }, + body: JSON.stringify(body) + }); +} + +async function sendApiRequest(method, body) { + return sendApiRequestRaw(method, body).then((e) => e.json()); +} + +async function sendMessageToChat(chat_id, text) { + return sendApiRequest("sendMessage", { + chat_id, + text, + parse_mode: "html", + disable_web_page_preview: true + }); +} + +async function sendAndPinMessageToChat(chat_id, text) { + let { message_id } = await sendMessageToChat(text); + return sendApiRequest("pinChatMessage", { + chat_id, + message_id + }); +} + +async function main() { + BOT_TOKEN = process.env.BOT_TOKEN; + if (typeof BOT_TOKEN !== "string") { + console.error("missing value for env variable: BOT_TOKEN"); + return 1; + } + + let chat_id = process.env.TELEGRAM_CHAT_ID; + if (typeof chat_id !== "string") { + console.error("missing value for env variable: TELEGRAM_CHAT_ID"); + return 1; + } + + let message = process.env.TELEGRAM_MESSAGE; + if (typeof message !== "string") { + console.error("missing value for env variable: TELEGRAM_MESSAGE"); + return 1; + } + + await sendAndPinMessageToChat(chat_id, message); +} + +if (require.main) + main() + .then((exitCode) => { + if (typeof exitCode == "number") process.exit(exitCode); + }) + .catch((e) => { + console.error(e); + process.exit(2); + });