Create Bot for Telegram To Send Notifications using Google Apps Script

September 20, 2022 Twinkle Kapoor How To

Create bot for Telegram: Do you want to get instant alerts whenever a new Google Form is submitted? With a Telegram bot, you can do just that! Google Forms is a great tool for collecting data from friends, family, and customers. But what if you could get alerts from Google Forms anytime a new form response is submitted in your Telegram messenger? With a little help from Google Apps Script, you can easily create a bot that can communicate with groups in Telegram. In this article, you’ll discover how to use Google Apps Script to quickly develop a new Telegram bot that can communicate with groups.

Creating a Telegram bot is quick and easy with Google Apps Script. In just a few minutes, you can have a new bot that can communicate with groups. Apps Script is the best way to develop a bot because it’s quick and easy. Plus, you can launch your bot in minutes. In this post, you’ll learn how to use Google Apps Script to develop a new Telegram bot. We’ll go over everything you need to get started, including how to create a script and how to deploy it. By the end, you’ll have a fully functioning Telegram bot that can communicate with groups.

How to Create Bot for Telegram?

How to create bot for telegram? Start with opening the Telegram app on your PC or mobile phone and search for @BotFather.

Note: To create and manage your own private bots, you can interact with this official Telegram bot.

  1. Once you open the chat session with @BotFather, click the start button and type in the command /newbot to create a new Telegram bot.
  2. Give your Telegram bot a short name of your choice and then provide a username for your bot.

Fact: The best names have already been taken.

  • Now you’ll come across an API token given by the Telegram, note them down as we will need it in the later steps.

Voila! You are done creating your own private Telegram bot. The next step is marked with high importance as in this step you need to interact with this bot from your own Telegram account.

Execute this by opening your bot link- t.me/username_bot and click the Start button. Type Hello Bot! or any text of your choice to warm up your bot.

Also Read: Guide to Chromecast Google Duo (Via Phone and PC)

Posting to Telegram Group

Now if you want to use this bot to send messages to your Telegram group, you first need to add this bot as a group member and then assign the bot as the admin of the group. Kick Start your conversation by posting a warmup message in that group from your own account.

Posting to Telegram Channel

Finally, you can post messages to a Telegram channel using the bot, the bot must be added as a member of the channel and promoted as an admin. Now, send a good warmup message in the channel from your own account.

Also Read: Activate ABC iview At Abc.net.au: Guide to Register And Sign Up

Fetching List of Telegram Channels and Groups

After adding the bot to the Telegram groups and channels, we can use Google Apps Script to get a list of all places where the bot has access to write messages.

Proceed with opening the Google Apps Script editor, and run the below-mentioned code. Remember to replace the BOT_TAKEN with your bot’s own token.

// Returns Object of chat_id and names

const getTelegramGroupsAndChannels = () => {

// Type your Telegram Bot token here
const BOT_TOKEN = “1986321029:AAF09NbQfA9wdCyLAHsjpoSC43ai0P0VEh4”;

const TELEGRAM_API = https://api.telegram.org/bot${BOT_TOKEN}/getUpdates;

const response = UrlFetchApp.fetch(TELEGRAM_API);

const { ok, result = [] } = JSON.parse(response);

if (!ok) {

throw new Error(“Please check your API token again!”);

}

if (result.length === 0) {

throw new Error(“Please add this bot to a Telegram group or channel!”);

}

const telegramBotList = {};

result.forEach((e) => {
const { message, my_chat_member, channel_post } = e;
const { chat } = { …message, …my_chat_member, …channel_post };
const { title, id, username } = chat;
telegramBotList[id] = { chat_id: ${id}, title: title || username };
});

Logger.log(Object.values(telegramBotList));


/* Prints an array of groups and channels known to your bot

{chat_id=300816220, title=labnol},
{chat_id=-595214405, title=Telegram Group},
{chat_id=-10547249514, title=Telegram Channel} */

};

Post Messages to Telegram

Once you have the list of Telegram groups and channels ready with you, where the bot has the permit to post messages, you can easily thrust send a message to that group with the Telegram API. 

You now just need the unique chat_id of the group or channel and your text message that may also contain emoticons. Remember to escape the message string using encodeURIcomponent, if you have a multi-line message. This will replace newline characters \n with %0A and so on.

const postMessageToTelegram = () => {
// Provide the Id of your Telegram group or channel
const chatId = “-59521405”;

// Enter your message here
const message = “How are you 💕”;

const BOT_TOKEN = “1986321029:AAF09NbQfA9wdCyLAHsjpoSC43ai0P0VEh4”;
const TELEGRAM_API = https://api.telegram.org/bot${BOT_TOKEN}/sendMessage;
const text = encodeURIComponent(message);
const url = ${TELEGRAM_API}?chat_id=${chatId}&text=${text};
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
const { ok, description } = JSON.parse(response);

if (ok !== true) {

Logger.log(Error: ${description});
}
};

Also Read: Login to Discord Account: Guide to Create Your Discord Account

Sending Rich Text Notifications with Telegram

Additionally, you can also post rich text messages styled with either HTML or the Markdown format. Depending on the format of the input text, you need to set the parse_mode to either HTML or MarkdownV2.

Here’s the sendMessage API

const postRichHTMLToTelegram = () => {
// Chat Id of the Telegram user, group or channel
const chatId = “-5954105”;

// Rich text with HTML tags and entities
const message = Telegram supports different <a href="https://w3.org">HTML5 tags</a>. These include classic tags like <b>bold</b>, <em>emphasis</em>, <strong>strong</strong>, <s>strikethrough</s>, <u>underlines</u>, and <code>preformatted code</code>.;

const BOT_TOKEN = “1986321029:AAF09NbQfA9wdCyLAHsjpoSC43ai0P0VEh4”;

const TELEGRAM_API = https://api.telegram.org/bot${BOT_TOKEN}/sendMessage;
// Escape the input text

const text = encodeURIComponent(message);
const url = ${TELEGRAM_API}?chat_id=${chatId}&text=${text}&parse_mode=HTML;
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
const { ok, description } = JSON.parse(response);

if (ok !== true) {
Logger.log(Error: ${description});
}
};

Note: If an HTML tag is not supported by Telegram, for example, <H1> or <TABLE>, your message would be rejected.

Check this complete list of HTML tags supported by Telegram.

Author

Leave a Reply

Your email address will not be published. Required fields are marked *