package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) var KEY string = os.Getenv("TELEGRAM_TOKEN") var UID string = os.Getenv("TELEGRAM_USERID") var VERBOSE bool = os.Getenv("VERBOSE") != "" var BASE_URL string = "https://api.telegram.org/bot" + KEY type InfluxAlert struct { CheckID string `json:"_check_id"` CheckName string `json:"_check_name"` Level string `json:"_level"` Measurement string `json:"_measurement"` Message string `json:"_message"` NotificationEndpointID string `json:"_notification_endpoint_id"` NotificationEndpointName string `json:"_notification_endpoint_name"` NotificationRuleID string `json:"_notification_rule_id"` NotificationRuleName string `json:"_notification_rule_name"` SourceMeasurement string `json:"_source_measurement"` SourceTimestamp float64 `json:"_source_timestamp"` Start string `json:"_start"` StatusTimestamp float64 `json:"_status_timestamp"` Stop string `json:"_stop"` Time string `json:"_time"` Type string `json:"_type"` Version float64 `json:"_version"` } type TelegramMessage struct { ChatID string `json:"chat_id"` Text string `json:"text"` } func handler(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } dat, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "Error reading request", http.StatusInternalServerError) return } incoming := InfluxAlert{} err = json.Unmarshal(dat, &incoming) if err != nil { fmt.Println("Got Error unmarshaling") fmt.Printf("Error: %T\n", err) fmt.Println(err) //http.Error return } w.WriteHeader(http.StatusOK) w.Write(nil) if VERBOSE { fmt.Println(string(dat)) // parsed, err := json.MarshalIndent(incoming, "", " ") // if err == nil { // fmt.Println(string(parsed)) // } } fmt.Print(incoming.Time + ": ") fmt.Println(incoming.Message) // KEY := os.Getenv("TELEGRAM_TOKEN") // if KEY == "" { // fmt.Println("Error: Can not send message - no bot token set") // return // } // UID := os.Getenv("TELEGRAM_USERID") // if UID == "" { // fmt.Println("Error: Can not send message - no recepient user id set") // return // } URL := BASE_URL + "/sendMessage" message := TelegramMessage{UID, incoming.Message} outgoing, err := json.Marshal(message) if err != nil { fmt.Println("Error marshaling telegram json") return } //http.Post(URL, "application/json", json.Marshal(outgoing)) req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(outgoing)) //defer resp.Body.Close(); if err != nil { fmt.Println("Error making request to telegram") fmt.Printf("%T: ", err) fmt.Println(err) return } req.Header.Add("Content-Type", "application/json") res, err := http.DefaultClient.Do(req) if err != nil { fmt.Println("Error posting message to telegram") fmt.Printf("%T: ", err) fmt.Println(err) return } defer res.Body.Close() resBody, err := io.ReadAll(res.Body) if err != nil { fmt.Println("Error getting response from telegram") fmt.Printf("%T: ", err) fmt.Println(err) return } // fmt.Print(res.StatusCode) print := false resdata := map[string]interface{}{} json.Unmarshal(resBody, &resdata) if resdata["ok"] != true { fmt.Println("Error telegram responded with error") print = true return } if VERBOSE || print { pretty, err := json.MarshalIndent(resdata, "", " ") if err != nil { return } fmt.Println(string(pretty)) } } func main() { //TODO check if uid is valid // Test KEY if KEY == "" { fmt.Println("Error: Can not send message - no bot token set") return } if UID == "" { fmt.Println("Error: Can not send message - no recepient user id set") return } resp, err := http.Get(BASE_URL + "/getMe") if err != nil { fmt.Println("Error could not validate bot token") fmt.Printf("%T: ", err) fmt.Println(err) return } defer resp.Body.Close() me := map[string]interface{}{} data, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error could not validate bot token") fmt.Printf("%T: ", err) fmt.Println(err) return } json.Unmarshal(data, &me) if me["ok"] != true { fmt.Println("Error could not validate bot token") //fmt.Printf("%T\n", me["ok"]) pretty, err := json.MarshalIndent(me, "", " ") if err != nil { return } fmt.Println(string(pretty)) return } fmt.Println("Validated bot token") if VERBOSE { pretty, err := json.MarshalIndent(me, "", " ") if err != nil { return } fmt.Println(string(pretty)) } http.HandleFunc("/", handler) fmt.Println("server started at localhost:8080") err = http.ListenAndServe(":8080", nil) if err != nil { fmt.Println("!!! ERROR !!!") fmt.Printf("%T: ", err) fmt.Println(err) panic(err) } }