devops: send Telegram messages via Node.js (#15804)

This commit is contained in:
Max Schmitt 2022-07-21 00:45:26 +02:00 committed by GitHub
parent ba3c8ffa5c
commit 9b3bd92071
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 21 deletions

View file

@ -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 "<b>${BROWSER_DISPLAY_NAME} r${BUILD_NUMBER} COMPLETE! ${CHECKMARK_CHAR}</b> ${LAST_COMMIT_MESSAGE}"
node send_telegram_message.js "<b>${BROWSER_DISPLAY_NAME} r${BUILD_NUMBER} COMPLETE! ✅</b> ${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} <a href='https://playwright.azureedge.net/builds/${LOG_BLOB_PATH}'>${LOG_BLOB_NAME}</a> -- <a href='$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID'>GitHub Action Logs</a>"
node send_telegram_message.js "$BUILD_ALIAS -- ${FAILED_STEP} failed! ❌ <a href='https://playwright.azureedge.net/builds/${LOG_BLOB_PATH}'>${LOG_BLOB_NAME}</a> -- <a href='$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID'>GitHub Action Logs</a>"
exit 1
fi

View file

@ -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 <text>');
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();
});
}

View file

@ -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
}