Sending to Telegram group
This page describes how to send messages into Telegram group if merchant wants to implement this functionality as part of API integration.
Step 1 – create Telegram group
You can reuse existing group or create new from scratch (recommended). We recommend to name it appropriately and set up a group picture.
Step 2 – create a bot and get its token
DM @BotFather, and create a new bot. You'll get bot token that looks like
11111111111:XXXXXXXXXXXXXXXXXXXXXXXXxxxx
We recommend to name bot appropriately like "Subscriptions bot" or "Payments bot", whatever you like. We also recommend to set up bot's picture (open bot profile -> Edit) to make it look better in the group.
Step 3 – add bot into the group
This is pretty self-explanatory, just add this bot into the group you created on step 1.
Step 4 – get chat id
In the browser (or via CURL) open the URL
https://api.telegram.org/bot<token>/getUpdates
where you get latest bot's activity. Visually scan the updates for text like
"sender_chat":{"id":-1003057777777 , .... }
here "id" is the group id you need (a big negative number).
Step 5 – send message to group
This code in TypeScript is self-explanatory, Telegram's sendMessage method is used, and lots of options are available. Here is the simplest possible case presented.
import axios from "axios";
export const sendTelegramMessage = (botToken: string, chatId: number, msg: string) => {
return axios.get(
`https://api.telegram.org/bot${botToken}/sendMessage`,
{
params: {
parse_mode: "HTML",
chat_id: chatId,
text: msg,
disable_web_page_preview: true,
},
}
);
};
In this case HTML markup is used, (see https://core.telegram.org/bots/api#formatting-options), so you can format the message any way you want.
Last updated