all repos — telebonk @ 4f479f90c06b24f57a4b28f7f1e8a2550da713cf

reposter from honk to telegram

rename go module root
la-ninpre aaoth@aaoth.xyz
Sat, 02 Dec 2023 13:06:12 +0300
commit

4f479f90c06b24f57a4b28f7f1e8a2550da713cf

parent

cd0118a677e8f1f3b1f6a4a89311a3fae9d7c349

2 files changed, 27 insertions(+), 62 deletions(-)

jump to
M go.modgo.mod

@@ -1,3 +1,3 @@

-module git.aaoth.xyz/la-ninpre/telebonk +module aaoth.xyz/la-ninpre/telebonk go 1.19
M main.gomain.go

@@ -15,48 +15,9 @@ "sort"

"strconv" "strings" "time" -) - -// A Config is holding configuration of telebonk. -type Config struct { - TgBotToken string - TgChatID string - TgApiURL string - HonkAuthToken string - HonkPage string - HonkURL string -} -// Check makes sure that no Config fields are set to empty strings. -func (c *Config) Check() error { - var what string - if c.TgBotToken == "" { - what = "bot_token" - } - if c.TgChatID == "" { - what = "chat_id" - } - if c.TgApiURL == "" { - what = "tgapi_url" - } - if c.HonkAuthToken == "" { - what = "honk_token" - } - if c.HonkURL == "" { - what = "honk_url" - } - switch c.HonkPage { - case "atme", "longago", "home", "myhonks": - default: - return fmt.Errorf("bad page type: %s", c.HonkPage) - } - if what != "" { - return fmt.Errorf("'%s' shouldn't be empty", what) - } - return nil -} - -var config = &Config{} + "git.aaoth.xyz/la-ninpre/telebonk/config" +) // A Honk is a post from honk. type Honk struct {

@@ -133,7 +94,7 @@ }

// Decide sets the Action of a Honk. // -// It sets HonkIgnore to those honks that are: 1) old; 2) already sent and not edits. +// It sets HonkIgnore to those honks that are: 1) old; 2) already sent and aren't edits. func (h *Honk) Decide() { oldhonk, ok := honkMap[h.XID] if ok {

@@ -198,10 +159,10 @@ }

} // NewMess creates and populates a new Mess with default values. -func NewMess(parseMode string) *Mess { +func NewMess(parseMode, chatID string) *Mess { return &Mess{ ParseMode: parseMode, - ChatID: config.TgChatID, + ChatID: chatID, kind: messHonk, } }

@@ -314,8 +275,9 @@ messDonkPht

messDonkDoc ) -func botAPIMethod(method string) string { - return fmt.Sprintf("%s/bot%s/%s", config.TgApiURL, config.TgBotToken, method) +// XXX: figure out how to retreive these args without passing config object thousand times +func botAPIMethod(url, token, method string) string { + return fmt.Sprintf("%s/bot%s/%s", url, token, method) } func checkTgAPI() error {

@@ -341,18 +303,18 @@ tgSendDocument = "sendDocument"

) // getHonks receives and unmarshals some honks from a Honk instance. -func getHonks(page string, after int) ([]*Honk, error) { +func getHonks(from, page, token string, after int) ([]*Honk, error) { query := url.Values{} query.Set("action", "gethonks") query.Set("page", page) query.Set("after", strconv.Itoa(after)) - apiurl := config.HonkURL + "/api?" + query.Encode() + apiurl := from + "/api?" + query.Encode() req, err := http.NewRequest("GET", apiurl, nil) if err != nil { return nil, err } - req.Header.Add("Authorization", "Bearer " + config.HonkAuthToken) + req.Header.Add("Authorization", "Bearer "+token) resp, err := client.Do(req) if err != nil {

@@ -364,7 +326,7 @@ // honk outputs junk like `{ "honks": [ ... ] }`, need to get into the list

var honkJunk map[string][]*Honk err = json.NewDecoder(resp.Body).Decode(&honkJunk) if err != nil { - // FIXME: honk tokens last for a week or so. when one expires, shouldn't this say something meaningful instead of `unexpected v in blah-blah'? + // XXX: honk tokens last for a week or so. when one expires, shouldn't this say something meaningful instead of `unexpected v in blah-blah'? log.Print("gethonks: ", resp.Status) return nil, err }

@@ -432,27 +394,30 @@ b.WriteString(with)

return b.String() } +// XXX: global and mutable +var conf = &config.Config{} + func init() { - flag.StringVar(&config.TgBotToken, "bot_token", "", "Telegram bot token") - flag.StringVar(&config.TgChatID, "chat_id", "", "Telegram chat_id") - flag.StringVar(&config.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL") - flag.StringVar(&config.HonkAuthToken, "honk_token", "", "Honk auth token") - flag.StringVar(&config.HonkPage, "honk_page", "myhonks", "Page to get honks from. Should be one of [atme, longago, home, myhonks]") - flag.StringVar(&config.HonkURL, "honk_url", "", "URL of a Honk instance") + flag.StringVar(&conf.TgBotToken, "bot_token", "", "Telegram bot token") + flag.StringVar(&conf.TgChatID, "chat_id", "", "Telegram chat_id") + flag.StringVar(&conf.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL") + flag.StringVar(&conf.HonkAuthToken, "honk_token", "", "Honk auth token") + flag.StringVar(&conf.HonkPage, "honk_page", "myhonks", "Page to get honks from. Should be one of [atme, longago, home, myhonks]") + flag.StringVar(&conf.HonkURL, "honk_url", "", "URL of a Honk instance") flag.Parse() - if err := config.Check(); err != nil { - log.Fatal("config:", err) // fail + if err := conf.Check(); err != nil { + log.Fatal("conf:", err) // fail } - config.TgApiURL = strings.TrimRight(config.TgApiURL, "/") + conf.TgApiURL = strings.TrimRight(conf.TgApiURL, "/") if err := checkTgAPI(); err != nil { log.Fatal("tgAPI:", err) // fail } } var client = http.DefaultClient -var honkMap = make(map[string]*Honk) // FIXME: not safe for use by multiple goroutines! +var honkMap = make(map[string]*Honk) // XXX: not safe for use by multiple goroutines! var now = time.Now() func main() {

@@ -460,7 +425,7 @@ var retry = 5

log.Print("starting telebonk") // info for { - honks, err := getHonks(config.HonkPage, 0) + honks, err := getHonks(conf.HonkURL, conf.HonkPage, conf.HonkAuthToken, 0) if err != nil { log.Print("gethonks:", err) // error retry--