Create a Bot
Send message /newbot
to BotFather
Create a group chat and add Bot to group
Grant permission to Bot
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
}