From 9b3bd92071692d997f91856e29b94503f53d579e Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 21 Jul 2022 00:45:26 +0200 Subject: [PATCH] devops: send Telegram messages via Node.js (#15804) --- .../checkout_build_archive_upload.sh | 11 +-- browser_patches/send_telegram_message.js | 78 +++++++++++++++++++ browser_patches/send_telegram_message.sh | 14 ---- 3 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 browser_patches/send_telegram_message.js delete mode 100644 browser_patches/send_telegram_message.sh diff --git a/browser_patches/checkout_build_archive_upload.sh b/browser_patches/checkout_build_archive_upload.sh index 0db4db3e74..e58dd766ea 100755 --- a/browser_patches/checkout_build_archive_upload.sh +++ b/browser_patches/checkout_build_archive_upload.sh @@ -494,9 +494,8 @@ function create_roll_into_playwright_pr { https://api.github.com/repos/microsoft/playwright/dispatches } -source ./send_telegram_message.sh BUILD_ALIAS="$BUILD_FLAVOR r$BUILD_NUMBER" -send_telegram_message "$BUILD_ALIAS -- started" +node send_telegram_message.js "$BUILD_ALIAS -- started" if generate_and_upload_browser_build 2>&1 | ./sanitize_and_compress_log.js $LOG_PATH; then # Report successful build. Note: MINGW might not have `du` command. @@ -504,7 +503,7 @@ if generate_and_upload_browser_build 2>&1 | ./sanitize_and_compress_log.js $LOG_ if command -v du >/dev/null && command -v awk >/dev/null; then UPLOAD_SIZE="$(du -h "$ZIP_PATH" | awk '{print $1}') " fi - send_telegram_message "$BUILD_ALIAS -- ${UPLOAD_SIZE}uploaded" + node send_telegram_message.js "$BUILD_ALIAS -- ${UPLOAD_SIZE}uploaded" # Check if we uploaded the last build. ( @@ -517,8 +516,7 @@ if generate_and_upload_browser_build 2>&1 | ./sanitize_and_compress_log.js $LOG_ fi done; LAST_COMMIT_MESSAGE=$(git log --format=%s -n 1 HEAD -- "./${BROWSER_NAME}/BUILD_NUMBER") - CHECKMARK_CHAR=$(printf '\xe2\x9c\x85') - send_telegram_message "${BROWSER_DISPLAY_NAME} r${BUILD_NUMBER} COMPLETE! ${CHECKMARK_CHAR} ${LAST_COMMIT_MESSAGE}" + node send_telegram_message.js "${BROWSER_DISPLAY_NAME} r${BUILD_NUMBER} COMPLETE! ✅ ${LAST_COMMIT_MESSAGE}" if [[ "${BROWSER_DISPLAY_NAME}" != "chromium-with-symbols" ]]; then create_roll_into_playwright_pr $BROWSER_NAME $BUILD_NUMBER fi @@ -544,8 +542,7 @@ else fi # Upload logs only in case of failure and report failure. ./upload.sh "${LOG_BLOB_PATH}" ${LOG_PATH} || true - CROSS_CHAR=$(printf '\xe2\x9d\x8c') - send_telegram_message "$BUILD_ALIAS -- ${FAILED_STEP} failed! ${CROSS_CHAR} ${LOG_BLOB_NAME} -- GitHub Action Logs" + node send_telegram_message.js "$BUILD_ALIAS -- ${FAILED_STEP} failed! ❌ ${LOG_BLOB_NAME} -- GitHub Action Logs" exit 1 fi diff --git a/browser_patches/send_telegram_message.js b/browser_patches/send_telegram_message.js new file mode 100644 index 0000000000..c7f3ad7862 --- /dev/null +++ b/browser_patches/send_telegram_message.js @@ -0,0 +1,78 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// @ts-check +const https = require('https'); + +const TELEGRAM_CHAT_ID = "-1001225613794"; + +(async () => { + const { TELEGRAM_BOT_KEY } = process.env; + if (!TELEGRAM_BOT_KEY) { + console.log('environment variable \'TELEGRAM_BOT_KEY\' is not set'); + return; + } + + const text = process.argv[2]; + if (!text) { + console.log('Text not set!'); + console.log('Usage: node send_telegram_message.js '); + return; + } + + await sendTelegramMessage(TELEGRAM_BOT_KEY, text); + console.log('Telegram message sent successfully!'); +})().catch(error => { + console.error(`Failed to send Telegram message. Error: ${error}`); +}) + +/** + * @param {string} apiKey + * @param {string} text + */ +async function sendTelegramMessage(apiKey, text) { + await new Promise((resolve, reject) => { + const request = https.request({ + hostname: 'api.telegram.org', + path: `/bot${apiKey}/sendMessage`, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }, res => { + let body = ''; + res.on('data', chunk => body += chunk.toString()); + res.on('end', () => { + if (res.statusCode !== 200) + reject(new Error(`Telegram API returned status code ${res.statusCode}. Body: ${body}`)); + else + resolve(JSON.parse(body)); + }); + res.on('error', err => { + reject(err); + }); + }); + request.on('error',reject); + request.write(JSON.stringify({ + disable_web_page_preview: true, + chat_id: TELEGRAM_CHAT_ID, + parse_mode: 'html', + text, + disable_notification: false, + })); + request.end(); + }); +} diff --git a/browser_patches/send_telegram_message.sh b/browser_patches/send_telegram_message.sh deleted file mode 100644 index 22155a0c3d..0000000000 --- a/browser_patches/send_telegram_message.sh +++ /dev/null @@ -1,14 +0,0 @@ -send_telegram_message() { - if [[ -z $TELEGRAM_BOT_KEY ]]; then - return; - fi - if ! command -v curl >/dev/null; then - return; - fi - local TEXT=${1//\"/\\\"} - curl --silent \ - -X POST \ - -H 'Content-Type: application/json' \ - -d '{"disable_web_page_preview": true, "chat_id": "-1001225613794", "parse_mode": "html", "text": "'"$TEXT"'", "disable_notification": false}' \ - https://api.telegram.org/bot"$TELEGRAM_BOT_KEY"/sendMessage >/dev/null -}