/
bot.go
54 lines (45 loc) · 1.09 KB
/
bot.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
package bot
import (
"encoding/json"
"io/ioutil"
"math/rand"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/shirase-aoi/log"
)
type Data struct {
Post string
Week string
Time int
}
type Random struct {
PostData []Data
}
func RegistRandomPost(post string) {
rp := Random{}
rjs, err := ioutil.ReadFile("../data/random.json")
if err == nil {
err = json.Unmarshal(rjs, &rp)
log.Terminate(err, "../log/error.json")
}
d := Data{Post: post}
datas := rp.PostData
datas = append(datas, d)
rp.PostData = datas
bin, _ := json.Marshal(rp)
ioutil.WriteFile("../data/random.json", bin, 0644)
}
func RandomPost() {
rand.Seed(time.Now().UnixNano())
c := LoadConfigure("")
rp := Random{}
rjs, err := ioutil.ReadFile("../data/random.json")
log.Terminate(err, "../log/posterr.json")
err = json.Unmarshal(rjs, &rp)
log.Terminate(err, "../log/posterr.json")
p := rand.Intn(len(rp.PostData))
anaconda.SetConsumerKey(c.ConsumerKey)
anaconda.SetConsumerSecret(c.ConsumerSelect)
api := anaconda.NewTwitterApi(c.AccessToken, c.AccessTokenSelect)
api.PostTweet(rp.PostData[p].Post, nil)
}