-
Notifications
You must be signed in to change notification settings - Fork 0
/
masto2slack.go
114 lines (97 loc) · 2.77 KB
/
masto2slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"encoding/json"
"fmt"
"log"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/mattn/go-mastodon"
"github.com/slack-go/slack"
config "github.com/spf13/viper"
)
var verbose = true
func getConfig() {
config.SetConfigName("config")
config.SetConfigType("yaml")
config.AddConfigPath("$HOME/.config/masto2slack")
err := config.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
func PostStatusToSlack(status *mastodon.Status, webhookURL string) {
// Statuses that are reblogs ("boosts") have no content, so we need to
// use the "Reblog" field to get the actual content
s := status
//isReblog := false
if s.Content == "" {
s = status.Reblog
//isReblog = true
}
if s.Content == "" {
if verbose {
fmt.Printf("Status %s has no content or reblog (boost) ?\n", s.ID)
}
return
}
htmlToMdConverter := md.NewConverter("", true, nil)
mdMsg, err := htmlToMdConverter.ConvertString(s.Content)
if err != nil {
log.Fatal(err)
}
attachment := slack.Attachment{
Text: mdMsg,
AuthorName: s.Account.DisplayName,
AuthorSubname: s.Account.Username,
AuthorLink: s.URL,
Ts: json.Number(fmt.Sprint(s.CreatedAt.Unix())),
}
slackMsg := &slack.WebhookMessage{Attachments: []slack.Attachment{attachment}}
err = slack.PostWebhook(webhookURL, slackMsg)
if err != nil {
log.Fatal(err)
}
}
func main() {
getConfig()
slackWebhookURL := config.GetString("slack.webhook_url")
c := mastodon.NewClient(&mastodon.Config{
Server: config.GetString("mastodon.server"),
// ClientID and ClientSecret are not required for reading public statuses
//ClientID: config.GetString("mastodon.client_id"),
//ClientSecret: config.GetString("mastodon.client_secret"),
AccessToken: config.GetString("mastodon.access_token"),
})
lastPostedID := config.GetString("last_status_id")
if verbose {
fmt.Printf("last_status_id: %s\n", lastPostedID)
}
// Get usr account info
uacct, err := c.GetAccountCurrentUser(context.Background())
if err != nil {
log.Fatal(err)
}
// Get toots for logged in user, only those since we last checked, earliest first
pg := mastodon.Pagination{
SinceID: mastodon.ID(lastPostedID),
}
timeline, err := c.GetAccountStatuses(context.Background(), uacct.ID, &pg)
if err != nil {
log.Fatal(err)
}
for i := len(timeline) - 1; i >= 0; i-- {
if timeline[i].ID == mastodon.ID(lastPostedID) {
break
}
if verbose {
fmt.Println("----")
fmt.Printf("time: %s\n", timeline[i].CreatedAt.String())
fmt.Printf("ID: %s\n", timeline[i].ID)
fmt.Printf("webhook: %s\n", slackWebhookURL)
}
PostStatusToSlack(timeline[i], slackWebhookURL)
config.Set("last_status_id", timeline[i].ID)
}
// update recorded last_status_id
config.WriteConfig()
}