Integration of Google scripts with Telegram

For those cases when you need a quick response to what happened in scripts — you want to have an alert in instant messengers.

Google’s examples contain code for integrating with Slack — https://developers.google.com/google-ads/scripts/docs/examples/slack. But at some point I had to write an integration with Telegram as well:

function main() {

	var CONFIG = {
		// The token must be obtained from BotFather by creating a new bot
		TOKEN: '0987654321:QQQWWWEEERRRTTTYYYUUUIIIOOOPPP111222333',

		// Write something in the chat to your bot, then follow the link https://api.telegram.org/bot<TOKEN>/getUpdates
		// find the line in the response text ..."chat":{"id":123456789,"first_name"... you need an id value.
		CHAT_ID: '123456789'
	};

	var message = 'Hello world!';
	sendTelegramMessage(message);

	function sendTelegramMessage(text) {
		var telegramUrl = `https://api.telegram.org/bot${CONFIG.TOKEN}/sendMessage?chat_id=${CONFIG.CHAT_ID}&text=`;
		var message = encodeURIComponent(text);
		var sendMessageUrl = telegramUrl + message;
		var options = {
			method: 'POST',
			contentType: 'application/json'
		};
		UrlFetchApp.fetch(sendMessageUrl, options);
	}
}