Skip to content

How to send message to Telegram group

February 15, 2021 | 01:00 PM

Create a Bot

Send message /newbot to BotFather

create_bot

Create a group chat and add Bot to group

create_group

Grant permission to Bot

grant_permission

Get chat group id

Using curl:

curl --location --request GET 'https://api.telegram.org/bot<BOT_TOKEN>/getUpdates'

response will be like:

"chat": {
            "id": -1234567,
            "title": "MyTestZK Group",
            "type": "group",
            "all_members_are_administrators": true
    },

CHAT_ID will be -1234567

Send message to group as bot

Using curl:

curl --location --request POST 'https://api.telegram.org/bot<BOT_TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=<MESSAGE>'

or by code in Golang:

func sendMessageToTelegram(msg string) error {
	botToken := os.Getenv("BOT_TOKEN")
	chatID := os.Getenv("CHAT_ID")
	postURL := fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage?chat_id=%v&text=%v", botToken, chatID, msg)
	client := &http.Client{}
	req, _ := http.NewRequest("POST", postURL, nil)
	res, _ := client.Do(req)
	if res.StatusCode != http.StatusOK {
		return fmt.Errorf("Received bad status code: %v", res.StatusCode)
	}
	return nil
}