Enviar mensajes a través de Telegram resulta muy útil sobretodo cuando se programan scripts de check o bakcups, de este modo podemos ser notificados sobre ciertas alarmas o errores.
En este tutorial vamos a ver un sencillo caso de uso:
vi telegram.go
package main
import (
"bytes"
"fmt"
"net/http"
"encoding/json"
)
func send_telegram(text string) {
bot := "botTOKEN"
chat_id := "CHATID"
request_url := "https://api.telegram.org/" + bot + "/sendMessage"
client := &http.Client{}
values := map[string]string{"text": text, "chat_id": chat_id }
json_paramaters, _ := json.Marshal(values)
req, _:= http.NewRequest("POST", request_url, bytes.NewBuffer(json_paramaters))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if(err != nil){
fmt.Println(err)
} else {
fmt.Println(res.Status)
defer res.Body.Close()
}
}
func main() {
send_telegram("AlfaExploit test")
}
Lo ejecutamos:
go run telegram.go
Deberíamos de recibir el mensaje en Telegram.