From 636ddd8c289b0761ab0c0f857dc3be57060faa18 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Tue, 3 Dec 2024 23:41:06 -0600 Subject: [PATCH] Added bot name info to output --- tbotsend.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tbotsend.go b/tbotsend.go index 23095be..f4e4f58 100644 --- a/tbotsend.go +++ b/tbotsend.go @@ -139,6 +139,13 @@ func main() { os.Exit(2) } + // Get Bot information + botName, botUsername, err := getBotInfo(config.Token) + if err != nil { + printError(fmt.Sprintf("Failed to get bot information: %v", err)) + os.Exit(2) + } + // Ensure message is provided if message == "" { printError("No message provided") @@ -195,6 +202,8 @@ func main() { // Print the required information fmt.Printf("\033[1mConfig Used:\033[0m %s\n", configUsed) + fmt.Printf("\033[1mBot Name:\033[0m %s\n", botName) + fmt.Printf("\033[1mBot Username:\033[0m @%s\n", botUsername) fmt.Printf("\033[1mChat ID:\033[0m %s\n", config.ChatID) fmt.Printf("\033[1mSilent:\033[0m %s\n", strconv.FormatBool(silent)) fmt.Printf("\033[1mMessage:\033[0m %s\n", message) @@ -221,3 +230,43 @@ func stripANSI(input string) string { re := regexp.MustCompile(ansiPattern) return re.ReplaceAllString(input, "") } + +func getBotInfo(token string) (name string, username string, err error) { + urlStr := fmt.Sprintf("https://api.telegram.org/bot%s/getMe", token) + resp, err := http.Get(urlStr) + if err != nil { + return "", "", err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", "", err + } + + type GetMeResponse struct { + Ok bool `json:"ok"` + Result struct { + ID int64 `json:"id"` + IsBot bool `json:"is_bot"` + FirstName string `json:"first_name"` + Username string `json:"username"` + } `json:"result"` + Description string `json:"description"` + } + + var getMeResp GetMeResponse + err = json.Unmarshal(body, &getMeResp) + if err != nil { + return "", "", err + } + + if !getMeResp.Ok { + return "", "", fmt.Errorf("getMe failed: %s", getMeResp.Description) + } + + name = getMeResp.Result.FirstName + username = getMeResp.Result.Username + + return name, username, nil +}