display chat info in logs

botname
Aaron Johnon 6 months ago
parent 636ddd8c28
commit 4905537bf6

@ -146,6 +146,13 @@ func main() {
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")
@ -204,6 +211,10 @@ func main() {
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)
@ -270,3 +281,56 @@ func getBotInfo(token string) (name string, username string, err error) {
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