-
Notifications
You must be signed in to change notification settings - Fork 138
/
twitter.go
80 lines (74 loc) · 2.05 KB
/
twitter.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
package main
import (
"fmt"
"strings"
"time"
"github.com/acarl005/stripansi"
"github.com/dghubble/go-twitter/twitter" //nolint:staticcheck // library deprecated
"github.com/dghubble/oauth1"
)
var (
StartupTime = time.Now()
Client *twitter.Client
AllowTweet = true
)
func sendCurrentUsersTwitterMessage() {
if Integrations.Twitter == nil {
return
}
// TODO: count all users in all rooms
if len(MainRoom.users) == 0 {
return
}
if !AllowTweet {
return
}
AllowTweet = false
usersSnapshot := append(make([]*User, 0, len(MainRoom.users)), MainRoom.users...)
areUsersEqual := func(a []*User, b []*User) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if b[i] != a[i] {
return false
}
}
return true
}
go func() {
time.Sleep(time.Second * 60)
AllowTweet = true
if !areUsersEqual(MainRoom.users, usersSnapshot) {
return
}
Log.Println("Sending twitter update")
names := make([]string, 0, len(MainRoom.users))
for _, us := range MainRoom.users {
names = append(names, us.Name)
}
t, _, err := Client.Statuses.Update("People on Devzat rn: "+stripansi.Strip(fmt.Sprint(names))+"\nJoin em with \"ssh devzat.hackclub.com\"\nUptime: "+printPrettyDuration(time.Since(StartupTime)), nil)
if err != nil {
if !strings.Contains(err.Error(), "twitter: 187 Status is a duplicate.") {
Log.Println("Twitter error:", err)
}
Log.Println("Got twitter err", err)
return
}
MainRoom.broadcast(Devbot, "https\\://twitter.com/"+t.User.ScreenName+"/status/"+t.IDStr)
}()
}
func twitterInit() { // called by init() in config.go
if Integrations.Twitter == nil {
return
}
config := oauth1.NewConfig(Integrations.Twitter.ConsumerKey, Integrations.Twitter.ConsumerSecret)
token := oauth1.NewToken(Integrations.Twitter.AccessToken, Integrations.Twitter.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
Client = twitter.NewClient(httpClient)
_, _, err := Client.Accounts.VerifyCredentials(nil)
if err != nil {
Log.Println("Twitter auth failed:", err)
Integrations.Twitter = nil
}
}