-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
289 lines (238 loc) · 6.26 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
// "github.com/BurntSushi/toml"
"github.com/himanshujaju/localdb"
"github.com/k0kubun/pp/v3"
"github.com/multiplay/go-ts3"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
"github.com/pelletier/go-toml/v2"
cron "github.com/robfig/cron/v3"
)
type account struct {
Name string
Rg string
RoomGroup roomgroup
// IncReplies bool
}
type roomgroup struct {
Name string
Rooms []int
}
type Config struct {
CRON_SCHEDULE string
TEAMSPEAK_IP string
TEAMSPEAK_API_USER string
TEAMSPEAK_API_PASSWORD string
TEAMSPEAK_SERVER_ID int
TEAMSPEAK_BOT_USERNAME string
TWITTER_URL string
TWITTER_CONSUMER_KEY string
TWITTER_CONSUMER_SECRET string
TWITTER_ACCESS_TOKEN string
TWITTER_ACCESS_SECRET string
// ROOMGROUPS [][]string
ROOMGROUPS []roomgroup
ACCOUNTS []account
}
func getRoomGroup(rgs []roomgroup, name string) roomgroup {
for _, rg := range rgs {
if rg.Name == name {
return rg
}
}
return roomgroup{}
}
func genTwitterUserSearch(accs []account) (string, error) {
if len(accs) == 0 {
return "", errors.New("Missing accounts to query twitter")
}
str := strings.Builder{}
str.WriteString("(")
for i, a := range accs {
str.WriteString(fmt.Sprintf("from:%s", a.Name))
// If its not the last
if i != len(accs)-1 {
str.WriteString(", OR ")
}
}
str.WriteString(")")
return str.String(), nil
}
func writeTweet(cfg Config, t twitter.Tweet) string {
out := strings.Builder{}
out.WriteString("🐦")
bbUser := fmt.Sprintf("[b][u]%s[/u][/b]", t.User.ScreenName)
out.WriteString(bbUser)
out.WriteString(" - ")
out.WriteString(t.Text)
out.WriteString(" - ")
url := fmt.Sprintf(cfg.TWITTER_URL, t.User.ScreenName, t.ID)
// bburl := fmt.Sprintf("[url=%s]link[/url]", url)
bburl := fmt.Sprintf("[url=%s]%s[/url]", url, url)
out.WriteString(bburl)
return out.String()
}
func RunScript(cfg Config) {
// Prepare the tweet container
outTweets := make(map[int][]string, 0)
outTweetsCnt := 0
for _, rg := range cfg.ROOMGROUPS {
for _, roomID := range rg.Rooms {
outTweets[roomID] = []string{}
}
}
// -----------------------------------------------------
// -----------------------------------------------------
// -----------------------------------------------------
// Build the twitter User query
twitterQuery, err := genTwitterUserSearch(cfg.ACCOUNTS)
if err != nil {
log.Fatal(err.Error())
}
// pp.Println(twitterQuery)
// Get the twitter cleint
config := oauth1.NewConfig(cfg.TWITTER_CONSUMER_KEY, cfg.TWITTER_CONSUMER_SECRET)
token := oauth1.NewToken(cfg.TWITTER_ACCESS_TOKEN, cfg.TWITTER_ACCESS_SECRET)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
// Get the local database
database := localdb.CreateDB(".lastTweets")
// Query Twitter
search, _, err := client.Search.Tweets(&twitter.SearchTweetParams{
Query: twitterQuery,
})
if err != nil {
log.Print(err)
}
// Go through the Search results backwards
for i := len(search.Statuses) - 1; i >= 0; i-- {
t := search.Statuses[i]
// Disallow replies
if t.InReplyToUserID != 0 {
continue
}
lastTweetID_Str, err := database.Get(t.User.ScreenName)
lastTweetId_Int, _ := strconv.Atoi(lastTweetID_Str)
processTweet := false
// Do we have a previous tweet on record
// If not, then we can process this tweet
if err != nil {
processTweet = true
}
// Is the current tweet were looking at newer than the last on record
// If so then we can process this tweet
if t.ID > int64(lastTweetId_Int) {
processTweet = true
}
// We can't process the tweet so move on
if processTweet != true {
continue
}
// Get the matching account
var acc account
for _, v := range cfg.ACCOUNTS {
if v.Name == t.User.ScreenName {
acc = v
}
}
// Get the accounts rooms
// Add this tweet to write to these rooms
for _, roomId := range acc.RoomGroup.Rooms {
outTweets[roomId] = append(outTweets[roomId], writeTweet(cfg, t))
outTweetsCnt += 1
}
// Finally set the users last tweet on record to be the current tweet
database.Set(t.User.ScreenName, strconv.FormatInt(t.ID, 10))
}
// If we have some tweets to post to TS then process them
if outTweetsCnt > 0 {
tsProcessMessages(cfg, outTweets)
} else {
fmt.Println("No tweets to process")
// No tweets to process")
}
}
func tsProcessMessages(cfg Config, outTweets map[int][]string) {
// #############################################
c, err := ts3.NewClient(cfg.TEAMSPEAK_IP)
if err != nil {
log.Fatal(err)
}
defer c.Close()
if err := c.Login(cfg.TEAMSPEAK_API_USER, cfg.TEAMSPEAK_API_PASSWORD); err != nil {
log.Fatal(err)
}
if v, err := c.Version(); err != nil {
log.Fatal(err)
} else {
log.Println("server is running:", v)
}
c.Use(cfg.TEAMSPEAK_SERVER_ID) // Main room
c.SetNick(cfg.TEAMSPEAK_BOT_USERNAME)
tsclient, err := c.Whoami()
if err != nil {
log.Fatal("err", err.Error())
}
for roomId, tweets := range outTweets {
log.Println("Moving client to:", roomId)
cmd := ts3.NewCmd("clientmove")
cmd.WithArgs(
ts3.NewArg("clid", tsclient.ClientID),
ts3.NewArg("cid", roomId),
)
out, err := c.ExecCmd(cmd)
_ = out
if err != nil {
log.Fatal("err", err.Error())
}
for _, tStr := range tweets {
cmd = ts3.NewCmd("sendtextmessage")
cmd.WithArgs(
// Client is 1, Channel is 2, Server is 3
ts3.NewArg("targetmode", 2),
// ServerId, ChannelId, ClientId
ts3.NewArg("target", roomId),
ts3.NewArg("msg", tStr),
)
out1, err := c.ExecCmd(cmd)
if err != nil {
log.Fatal("err", err.Error())
} else {
pp.Println(fmt.Sprintf("Msged: %d - %s", roomId, tStr))
_ = out1
}
}
}
}
func main() {
doc, err := ioutil.ReadFile("config.toml")
if err != nil {
log.Fatal(err)
}
var cfg Config
err1 := toml.Unmarshal(doc, &cfg)
if err != nil {
log.Fatal(err1)
}
for i, acc := range cfg.ACCOUNTS {
cfg.ACCOUNTS[i].RoomGroup = getRoomGroup(cfg.ROOMGROUPS, acc.Rg)
}
pp.Println(cfg.ACCOUNTS)
// Rooms
RunScript(cfg)
jobs := cron.New(cron.WithSeconds())
// jobs.AddFunc("20 */1 * * * *", func() {
jobs.AddFunc(cfg.CRON_SCHEDULE, func() {
RunScript(cfg)
})
jobs.Start()
finished := make(chan bool)
<-finished
}