-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (69 loc) · 1.9 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
package main
import (
"botqepo/config"
"botqepo/file"
"botqepo/openai"
"botqepo/tweet"
"botqepo/zenquotes"
"log"
"strings"
"time"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
func main() {
cfg := *config.Init()
config := oauth1.NewConfig(cfg.TwitterConsumerKey, cfg.TwitterConsumerSecret)
token := oauth1.NewToken(cfg.TwitterAccessToken, cfg.TwitterAccessSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
openaiCfg := openai.CfgOpenai{
BaseUrl: cfg.OpenaiBaseUrl,
Model: cfg.OpenaiModel,
Token: cfg.OpenaiToken,
}
zenquotesCfg := zenquotes.CfgZenquotes{
BaseUrl: cfg.ZenquotesBaseUrl,
}
for {
tweets, err := tweet.SearchTweets(client)
if err != nil {
if err.Error() == "twitter: 88 Rate limit exceeded" {
log.Println("Rate limit exceeded, waiting for 15 minutes")
time.Sleep(time.Second * 60 * 15)
}
}
if tweets.ID != 0 {
var answers string
if cfg.IsOpenAIEnable {
openaiResp := openai.Completions(openaiCfg, strings.Replace(tweets.Text, "@botqepo", "", -1))
answers = strings.Replace(openaiResp.Choices[0].Text, "\n", " ", -1)
answers = strings.Replace(answers, "A:", "", -1)
}
err = tweet.PostOpenaiTweet(client, answers, tweets)
if err != nil {
log.Println("Error PostTweet: ", err)
} else {
file.WriteLatestID(tweets.ID)
}
}
if cfg.IsZenQuotesEnable {
latestQuote := file.ReadLatestQuote()
// 21600 is 6 hours in seconds
if latestQuote == 0 || time.Now().Unix()-latestQuote > 21600 {
quote, err := zenquotes.GetQuote(zenquotesCfg)
if err != nil {
log.Println("Error GetQuote: ", err)
}
if quote.Q != "" {
err = tweet.PostZenquotesTweet(client, quote)
if err != nil {
log.Println("Error PostTweet: ", err)
}
file.WriteLatestQuote(time.Now().Unix())
}
}
}
time.Sleep(time.Second * 40)
}
}