diff options
| author | Christine Dodrill <me@christine.website> | 2017-05-06 12:45:39 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2017-05-06 12:45:39 -0700 |
| commit | 8116a07a406c483fe6b367d6e0769c7891d24f66 (patch) | |
| tree | 716c05783b692a47a97c18e88dfb3a179af0a0cf | |
| parent | 14acf2af528c09b9373f0e109e3bc24da4eaf42d (diff) | |
| download | x-8116a07a406c483fe6b367d6e0769c7891d24f66.tar.xz x-8116a07a406c483fe6b367d6e0769c7891d24f66.zip | |
Add mastodon
| -rw-r--r-- | tg/meirl/.gitignore | 1 | ||||
| -rw-r--r-- | tg/meirl/main.go | 27 |
2 files changed, 24 insertions, 4 deletions
diff --git a/tg/meirl/.gitignore b/tg/meirl/.gitignore index 4c49bd7..5f118ff 100644 --- a/tg/meirl/.gitignore +++ b/tg/meirl/.gitignore @@ -1 +1,2 @@ .env +meirl
\ No newline at end of file diff --git a/tg/meirl/main.go b/tg/meirl/main.go index b2a2146..0be348f 100644 --- a/tg/meirl/main.go +++ b/tg/meirl/main.go @@ -7,6 +7,7 @@ import ( "runtime" "time" + "github.com/McKael/madon" "github.com/Xe/ln" "github.com/caarlos0/env" _ "github.com/joho/godotenv/autoload" @@ -25,6 +26,11 @@ type config struct { TelegramToken string `env:"TELEGRAM_TOKEN,required"` TelegramAdmin string `env:"TELEGRAM_ADMIN,required"` TelegramChannelID int64 `env:"TELEGRAM_CHANNEL_ID,required"` + + MastodonToken string `env:"MASTODON_TOKEN,required"` + MastodonClientSecret string `env:"MASTODON_CLIENT_SECRET,required"` + MastodonClientID string `env:"MASTODON_CLIENT_ID,required"` + MastodonInstance string `env:"MASTODON_INSTANCE,required"` } func main() { @@ -55,9 +61,15 @@ func main() { ln.Log(ln.F{"action": "reddit_connected", "user_agent": userAgent}) + md, err := madon.RestoreApp("me_irl", cfg.MastodonInstance, cfg.MastodonClientID, cfg.MastodonClientSecret, &madon.UserToken{AccessToken: cfg.MastodonToken}) + if err != nil { + ln.Fatal(ln.F{"err": err, "action": "madon.RestoreApp"}) + } + a := &announcer{ cfg: &cfg, tg: tg, + md: md, } stop, wait, err := graw.Scan(a, rd, graw.Config{Subreddits: cfg.Subreddits, Logger: log.New(os.Stderr, "", log.LstdFlags)}) @@ -75,17 +87,24 @@ func main() { type announcer struct { cfg *config tg *tgbotapi.BotAPI + md *madon.Client } func (a *announcer) Post(post *reddit.Post) error { - msg := tgbotapi.NewMessage(a.cfg.TelegramChannelID, fmt.Sprintf("me irl\n%s\n(https://reddit.com%s by /u/%s)", post.URL, post.Permalink, post.Author)) - _, err := a.tg.Send(msg) + txt := fmt.Sprintf("me irl\n%s\n(https://reddit.com%s by /u/%s)", post.URL, post.Permalink, post.Author) + + msg := tgbotapi.NewMessage(a.cfg.TelegramChannelID, txt) + tmsg, err := a.tg.Send(msg) + if err != nil { + return err + } + + toot, err := a.md.PostStatus(txt, 0, nil, false, "", "public") if err != nil { - ln.Error(err, ln.F{"err": err, "action": "telegram_post"}) return err } - ln.Log(ln.F{"action": "new_post", "url": post.URL, "permalink": post.Permalink, "redditor": post.Author}) + ln.Log(ln.F{"action": "new_post", "url": post.URL, "permalink": post.Permalink, "redditor": post.Author, "toot_id": toot.ID, "tg_id": tmsg.MessageID}) return nil } |
