diff options
| author | la-ninpre <leobrekalini@gmail.com> | 2022-10-17 12:41:20 +0300 |
|---|---|---|
| committer | la-ninpre <leobrekalini@gmail.com> | 2022-10-17 13:21:18 +0300 |
| commit | 35e839782be9df360365efb527770615965184c0 (patch) | |
| tree | dae948fc9e212af72cd904a937a0764d2020ba2b | |
| parent | ca7eca93e8e9ff2f8f448b34de61471eae48fc70 (diff) | |
| download | telebonk-35e839782be9df360365efb527770615965184c0.tar.gz telebonk-35e839782be9df360365efb527770615965184c0.zip | |
improve comments and compact if-statements
| -rw-r--r-- | main.go | 66 |
1 files changed, 32 insertions, 34 deletions
@@ -52,28 +52,28 @@ func (c *Config) Check() error { var config = &Config{} -// A Honk is a honk's honk object with most fields omitted. +// A Honk is a post from honk. type Honk struct { - ID int - What string // to filter out honkbacks - Oondle string - Oonker string - XID string - RID string - Date time.Time - Precis string // danger zone and all that - Noise string - Onts []string // to filter out #notg - Donks []*Donk - - messID int - replyToID int + ID int // unique id of a post + What string // type of an action (post, repost, reply) + Oondle string // e-mail style handle of the original author of a post + Oonker string // url of the original author of a post + XID string // url of a post, also unique + RID string // url of a post that current post is replying to + Date time.Time // datetime of a post + Precis string // post summary + Noise string // contents of a post + Onts []string // a slice of tags in a post + Donks []*Donk // a slice of attachments to a post + + messID int // telegram message_id + replyToID int // telegram message_id of a message to reply to } // A Donk stores metadata for media files. type Donk struct { URL string - Media string + Media string // mime-type of an attachment Desc string } @@ -128,12 +128,12 @@ func (h *Honk) Decide() honkAction { return honkSend } -// save records Honk to a honkMap +// save records a Honk to a honkMap func (h *Honk) save(t *TelegramResponse) { honkMap[h.XID] = honkInfo{h: h, t: t} } -// forget removes Honk from a honkMap +// forget removes a Honk from a honkMap func (h *Honk) forget() { delete(honkMap, h.XID) } @@ -154,14 +154,14 @@ type Mess struct { MessageID int `json:"message_id,omitempty"` ReplyToMessageID int `json:"reply_to_message_id,omitempty"` - // Donks - Document string `json:"document,omitempty"` - Photo string `json:"photo,omitempty"` - Caption string `json:"caption,omitempty"` - kind messKind // messHonk, messDonkPht or messDonkDoc + Document string `json:"document,omitempty"` + Photo string `json:"photo,omitempty"` + Caption string `json:"caption,omitempty"` + + kind messKind } -// A TelegramResponse is for handling possible errors with Telegram API. +// A TelegramResponse is a response from Telegram API. type TelegramResponse struct { Ok bool Description string @@ -304,6 +304,7 @@ func checkTgAPI() error { return nil } +// Telegram Bot API methods const ( tgGetMe = "getMe" tgSendMessage = "sendMessage" @@ -340,7 +341,7 @@ func getHonks(after int) ([]*Honk, error) { } honks := honkJunk["honks"] - // honk.ID monotonically increases, so it can be used to sort them + // honk.ID monotonically increases, so it can be used to sort honks sort.Slice(honks, func(i, j int) bool { return honks[i].ID < honks[j].ID }) return honks, nil @@ -362,7 +363,7 @@ var ( const emptyNoise = "<p></p>\n" -// calmNoise erases htm tags that are not supported by Telegram. +// calmNoise erases and rewrites html tags that are not supported by Telegram. func calmNoise(noise string) string { // TODO: check length of a honk @@ -392,12 +393,11 @@ func init() { flag.Parse() - err := config.Check() - if err != nil { + if err := config.Check(); err != nil { log.Fatal("config:", err) // fail } - err = checkTgAPI() - if err != nil { + + if err := checkTgAPI(); err != nil { log.Fatal(err) // fail } } @@ -428,8 +428,7 @@ func main() { case honkIgnore: continue case honkSend, honkEdit: - err := honk.Check() - if err != nil { + if err := honk.Check(); err != nil { log.Print(err) // error continue } @@ -445,8 +444,7 @@ func main() { // remember only the first mess' response honk.save(resp) for _, mess := range messes[1:] { - _, err := mess.Send() - if err != nil { + if _, err := mess.Send(); err != nil { log.Print(err) // error continue HonkLoop } |
