-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
168 lines (150 loc) · 4.27 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
package main
import (
"log"
"encoding/base64"
"strings"
"net/http"
"time"
"os"
"github.com/tucnak/telebot"
"github.com/bitly/go-simplejson"
)
var dict map[string][]string = make(map[string][]string)
var bot *telebot.Bot
var Tags []string
func main() {
token := os.Getenv("TELEBOT_TOKEN")
if len(token) > 0 {
log.Printf("Telegram Bot Token: %v\n", token)
} else {
log.Fatalln("Please set 'TELEBOT_TOKEN' from environment variable")
}
startInline()
var err error
bot, err = telebot.NewBot(token)
if err != nil {
log.Fatalln(err)
}
bot.Messages = make(chan telebot.Message, 1000)
bot.Queries = make(chan telebot.Query, 1000)
go messages()
go queries()
bot.Start(1*time.Second)
}
func startInline() {
resp, err := http.Get("https://raw.githubusercontent.com/guo-yu/o3o/master/yan.json")
if err != nil {
log.Fatalln(err)
}
js, err := simplejson.NewFromReader(resp.Body)
if err != nil {
log.Fatalln(err)
}
i := 0
for {
Tag, err := js.Get("list").GetIndex(i).Get("tag").String()
if err != nil {
break
}
Tags = append(Tags, Tag)
j := 0
for {
Yan, err := js.Get("list").GetIndex(i).Get("yan").GetIndex(j).String()
if err != nil {
break
}
dict[Tag] = append(dict[Tag], Yan)
j++
}
i++
}
log.Println("--- Dictionary initialized. ---")
}
func messages() {
//read groups information
for message := range bot.Messages {
resp, err := http.Get("https://raw.githubusercontent.com/livc/GroupHub_Bot/master/groups.json")
if err != nil {
log.Fatalln(err)
}
js, err := simplejson.NewFromReader(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Printf("--- new message ---")
log.Printf("Received a message from %s with the text: %s\n", message.Sender.Username, message.Text)
switch message.Text {
case "/start":
sta := "GroupHub致力于收录tg中文圈优质群组。\n"+
"项目地址: https://github.com/livc/GroupHub_Bot\n\n"+
"Bot: @GroupHub_bot\n"+
"广播站: @GroupHub\n"+
"交流群: @GroupHub_Chat\n"+
"群组收录更新: 将【群组名、分类、链接】发送到https://github.com/livc/GroupHub_Bot\n"+
"BUG提交/功能建议: https://github.com/livc/GroupHub_Bot/issues\n\n"+
"/groups 查询群组"
bot.SendMessage(message.Chat, sta, nil)
case "/groups":
bot.SendMessage(message.Chat, "请选择分类:", &telebot.SendOptions{
ReplyMarkup: telebot.ReplyMarkup{
ForceReply: true,
Selective: true,
CustomKeyboard: [][]string{
[]string{"ACG", "软件", "科学上网"},
[]string{"linux", "社区", "Geek"},
[]string{"编程", "城市", "书影音"},
[]string{"政治", "资源", "其他"},
},
},
},
)
case "ACG", "软件", "科学上网", "linux", "社区", "Geek", "编程", "城市", "书影音", "政治", "资源", "其他":
deal(message, js)
}
}
}
func deal(message telebot.Message, js *simplejson.Json) {
var all string
i := 0
for {
text, err := js.Get(message.Text).GetIndex(i).Get("TEXT").String()
if err != nil {
break
}
decodedT, err := base64.StdEncoding.DecodeString(text)
text = string(decodedT)
// text = strings.Replace(text, "_", "\\_", -1) //solve the confilc of markdown "_"
// 文本中带有下划线的需手动在下划线前面加'\'解析 部分群组邀请链接中也含有下划线导致无法进行替换
all = all+"\n"+text
i++
}
// log.Println(all)
bot.SendMessage(message.Chat, all, &telebot.SendOptions{ParseMode:"Markdown"})
}
func queries() {
for query := range bot.Queries {
log.Println("---new query---")
log.Println("from:", query.From.Username)
log.Println("text:", query.Text)
results := make([]telebot.InlineQueryResult, 0, 19)
R: for _, tag := range Tags {
if strings.Contains(tag, query.Text) {
for _, yan := range dict[tag] {
results = append(results, &telebot.InlineQueryResultArticle{Title: yan, Description: tag, Text: yan})
if len(results) >= cap(results) {
break R
}
}
}
}
// Build a response object to answer the query
response := telebot.QueryResponse{
Results: results,
IsPersonal: true,
}
// And finally send the response.
if err := bot.AnswerInlineQuery(&query, &response); err != nil {
log.Println("Failed to respond to query:", err)
}
}
}