package cmd import ( "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "strings" "time" "github.com/kf5grd/keybasebot" "github.com/teris-io/shortid" "samhofi.us/x/keybase/v2/types/chat1" ) var baseURL = "https://keybase.io/_/api/1.0/user/lookup.json?fields=basics&username=" type outer struct { Status status `json:"status"` Them them `json:"them"` } type status struct { Code int `json:"code"` Name string `json:"name"` } type them struct { Id string `json:"id"` User keybaseUser `json:"basics"` } type keybaseUser struct { Username string `json:"username"` Ctime int64 `json:"ctime"` Mtime int64 `json:"mtime"` ID_Version int64 `json:"id_version"` Track_Version int `json:"track_version"` Last_ID_Change int64 `json:"last_id_change"` Username_Cased string `json:"username_cased"` Status int `json:"status"` Salt string `json:"salt"` } var AgeAd = chat1.UserBotCommandInput{ Name: "age", Usage: "@username", Description: "Gets the account age of a keybase user", } func Age(m chat1.MsgSummary, b *keybasebot.Bot) (bool, error) { fields := strings.Fields(strings.TrimSpace(strings.Replace(m.Content.Text.Body, "!age", "", 1))) var username string if len(fields) > 1 { return true, fmt.Errorf("Too many parameters. Please specify one username, or none for your own account age.") } else if len(fields) < 1 { username = m.Sender.Username } else { username = strings.Replace(fields[0], "@", "", 1) } queryURL := baseURL + url.QueryEscape(username) res, err := http.Get(queryURL) if err != nil { eid := shortid.MustGenerate() b.Logger.Error("%s: %+v", eid, err) b.KB.ReactByConvID(m.ConvID, m.Id, "Error: %s", eid) return true, nil } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { eid := shortid.MustGenerate() b.Logger.Error("%s: %+v", eid, err) b.KB.ReactByConvID(m.ConvID, m.Id, "Error: %s", eid) return true, nil } var qres outer if err := json.Unmarshal(body, &qres); err != nil { eid := shortid.MustGenerate() b.Logger.Error("%s: %+v", eid, err) b.KB.ReactByConvID(m.ConvID, m.Id, "Error: %s", eid) return true, nil } var result string switch qres.Status.Code { case 0: // success t := time.Unix(qres.Them.User.Ctime, 0) result = fmt.Sprintf("Account %s created at %s", username, t.Format("January 02, 2006 - 15:04:05")) case 205: result = fmt.Sprintf("User %s not found.", username) case 100: result = "An input error occured" default: result = "Unable to complete request" } b.KB.SendMessageByConvID(m.ConvID, result) return true, nil }