Compare commits

...

2 Commits

Author SHA1 Message Date
Aaron Johnon 4905537bf6 display chat info in logs
6 months ago
Aaron Johnon 636ddd8c28 Added bot name info to output
9 months ago

@ -139,6 +139,20 @@ 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)
}
// Get Chat information
chatName, chatUsername, err := getChatInfo(config.ChatID, config.Token)
if err != nil {
printError(fmt.Sprintf("Failed to get chat information: %v", err))
os.Exit(2)
}
// Ensure message is provided
if message == "" {
printError("No message provided")
@ -195,6 +209,12 @@ 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 Name:\033[0m %s\n", chatName)
if chatUsername != "" {
fmt.Printf("\033[1mChat Username:\033[0m @%s\n", chatUsername)
}
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 +241,96 @@ 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
}
func getChatInfo(chatID string, token string) (name string, username string, err error) {
urlStr := fmt.Sprintf("https://api.telegram.org/bot%s/getChat?chat_id=%s", token, url.QueryEscape(chatID))
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 GetChatResponse struct {
Ok bool `json:"ok"`
Result struct {
ID int64 `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
} `json:"result"`
Description string `json:"description"`
}
var getChatResp GetChatResponse
err = json.Unmarshal(body, &getChatResp)
if err != nil {
return "", "", err
}
if !getChatResp.Ok {
return "", "", fmt.Errorf("getChat failed: %s", getChatResp.Description)
}
chatType := getChatResp.Result.Type
switch chatType {
case "private":
name = getChatResp.Result.FirstName + " " + getChatResp.Result.LastName
username = getChatResp.Result.Username
case "group", "supergroup", "channel":
name = getChatResp.Result.Title
username = getChatResp.Result.Username
default:
name = "Unknown Chat"
}
name = strings.TrimSpace(name)
return name, username, nil
}

Loading…
Cancel
Save