-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbot.go
More file actions
183 lines (160 loc) · 4.09 KB
/
bot.go
File metadata and controls
183 lines (160 loc) · 4.09 KB
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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
import "github.com/tgbot-collection/tgbot_ping"
const version = "0.0.1"
var (
h, v, f bool
c string
)
type config struct {
Username string `json:"username"`
Password string `json:"password"`
Url string `json:"url"`
Token string `json:"token"`
Uid int64 `json:"uid,string"`
Admin int `json:"admin"`
Tail string `json:"tail,omitempty"`
}
func init() {
flag.BoolVar(&h, "h", false, "this help")
flag.BoolVar(&v, "v", false, "show version and exit")
flag.BoolVar(&f, "f", false, "force to run even on http sites.")
flag.StringVar(&c, "c", "config.json", "set configuration `file`")
}
func getArgs() config {
flag.Parse()
if v {
fmt.Println(version)
os.Exit(0)
}
if h {
flag.Usage()
os.Exit(0)
}
var configFile = c
configData, err := readConfig(configFile)
if err != nil {
fmt.Printf("config file is corrupted or not found. \n%v\n", err)
flag.Usage()
os.Exit(2)
} else if !f && !strings.HasPrefix(configData.Url, "https://") {
fmt.Println("Your website is not https. Exit now.")
fmt.Println("Please use -f to force start.")
os.Exit(1)
} else {
fmt.Println("Okay let's roll😄")
}
return configData
}
func readConfig(cp string) (config, error) {
jsonFile, err := os.Open(cp)
if err != nil {
return config{}, err
}
defer jsonFile.Close()
var conf config
byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &conf)
return conf, err
}
func randomEmoji() string {
reasons := []string{"🙃", "🤨", "🤪", "😒", "🙂", "🥶", "🤔", "😶", "😐"}
rand.Seed(time.Now().Unix())
return reasons[rand.Intn(len(reasons))]
}
func replyComment(msg, reply string, conf config) (result string) {
type postFormat struct {
Author int `json:"author"`
AuthorUserAgent string `json:"author_user_agent"`
Content string `json:"content"`
Parent string `json:"parent"`
Post string `json:"post"`
}
reply += conf.Tail
g := strings.Split(strings.Split(msg, "id: ")[1], ",")
pid, cid := g[0], g[1]
post := postFormat{
Author: 1,
AuthorUserAgent: "Telegram Bot by BennyThink",
Content: reply,
Parent: pid,
Post: cid,
}
bytesData, err := json.Marshal(post)
if err != nil {
fmt.Println(err.Error())
}
data := bytes.NewReader(bytesData)
wpApi := conf.Url + "wp-json/wp/v2/comments"
request, err := http.NewRequest("POST", wpApi, data)
auth := []byte(fmt.Sprintf("%s:%s", conf.Username, conf.Password))
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(auth))
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
fmt.Println(err.Error())
}
var body struct {
Message string `json:"message"`
}
if resp.StatusCode == 201 {
result = "ok"
} else {
json.NewDecoder(resp.Body).Decode(&body)
result = body.Message
}
return
}
func bot(conf config) {
var owner = &tb.Chat{ID: conf.Uid}
b, err := tb.NewBot(tb.Settings{
Token: conf.Token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
b.Handle(tb.OnText, func(m *tb.Message) {
if m.Chat.ID != conf.Uid {
b.Notify(m.Sender, "Typing")
b.Send(m.Sender, randomEmoji())
b.Send(m.Sender, "拒绝调戏。有问题请联系@BennyThink")
} else if m.ReplyTo == nil {
b.Notify(m.Sender, "Typing")
b.Send(m.Sender, randomEmoji())
} else {
comment := m.ReplyTo.Text
reply := m.Text
b.Notify(m.Sender, "upload_document")
resp := replyComment(comment, reply, conf)
b.Notify(m.Sender, "typing")
b.Send(owner, resp)
}
})
b.Handle("/ping", func(m *tb.Message) {
_ = b.Notify(m.Chat, tb.Typing)
info := tgbot_ping.GetRuntime("botsrunner_wp-comments_1", "WordPress Comments Bot", "html")
_, _ = b.Send(m.Chat, info, &tb.SendOptions{ParseMode: tb.ModeHTML})
})
b.Start()
}
func main() {
c := getArgs()
bot(c)
}