-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
191 lines (149 loc) · 5.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
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
/*
wikisourcebot, a simple Telegram bot which implements wikisource.org APIs
Copyright (C) 2020 Hearot
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"log"
"strconv"
"strings"
"time"
"cgt.name/pkg/go-mwclient"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
defaultLanguage = "en"
maximum = 15
message = "This bot can help you find and share links to Wikisource articles. It works automatically, no need to add it anywhere. Simply open any of your chats and type @Wikisource_bot + language code (en, es, etc.) + something in the message field. Then tap on a result to send.\n\nFor example, try typing <code>@Wikisource_bot en Divine Comedy</code> here."
template = "https://%s.wikisource.org/w/api.php"
timeout = 10
token = ""
)
var langs = [...]string{"aat", "ab", "ady", "ae", "af", "akk", "an", "ar", "arn", "arp", "as",
"ast", "az", "ba", "bal", "ban", "bar", "be", "bem", "bg", "bm", "bn",
"bo", "br", "brx", "bs", "ca", "cdo", "chr", "chu", "cnr", "co", "cop",
"cpx", "cs", "csb", "cu", "cv", "cy", "da", "de", "diq", "dsb", "egy",
"el", "en", "eo", "es", "et", "eu", "ext", "fa", "fi", "fo", "fr", "frr",
"fur", "fy", "ga", "gag", "gd", "gl", "gld", "got", "grc", "gsw", "gu",
"gv", "hak", "haw", "he", "hi", "hr", "hsb", "ht", "hu", "hy", "ia", "id",
"io", "is", "ist", "it", "iu", "ja", "jbo", "jct", "jv", "ka", "kk", "km",
"kn", "ko", "koi", "krl", "ku", "kw", "ky", "la", "lad", "lb", "les", "lg",
"li", "lij", "lis", "liv", "lld", "lmo", "ln", "lo", "lra", "lt", "lv",
"lzh", "mai", "mas", "mdf", "mfe", "mg", "mh", "mhr", "mi", "min", "mk",
"ml", "mn", "mnc", "mnp", "mr", "mrj", "ms", "mwl", "my", "myv", "nah",
"nan", "nds", "ne", "ng", "nl", "no", "non", "nrn", "nv", "oc", "olo",
"osx", "ota", "pa", "pau", "pcd", "pdt", "peo", "pfl", "pi", "pl", "pms",
"pnb", "pnt", "pox", "ps", "pt", "qu", "rm", "rml", "ro", "ru", "ruo",
"rup", "ryu", "sa", "sah", "sc", "scn", "sco", "se", "see", "sh", "si",
"sjd", "sjk", "sjo", "sk", "sl", "slr", "sn", "sq", "sr", "stq", "su",
"suk", "sv", "sw", "ta", "tah", "te", "tet", "tg", "th", "tl", "tpn",
"tr", "tt", "txg", "udm", "ug", "uk", "ur", "uz", "vec", "vep", "vi",
"vo", "wa", "wym", "xh", "xmf", "yi", "yue", "zh", "zu"}
var clients map[string]*mwclient.Client
var defaultParameters = map[string]string{
"action": "query",
"format": "json",
"generator": "search",
"gsrlimit": strconv.Itoa(maximum),
"gsrprop": "snippet",
"inprop": "url",
"prop": "info",
}
func main() {
clients = make(map[string]*mwclient.Client, len(langs))
for _, lang := range langs {
var client *mwclient.Client
client, err := mwclient.New(fmt.Sprintf(template, lang), "Wikisourcebot")
if err != nil {
panic(err)
}
clients[lang] = client
}
bot, err := tb.NewBot(tb.Settings{
Token: token,
Poller: &tb.LongPoller{Timeout: timeout * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
bot.Handle(tb.OnText, func(m *tb.Message) {
bot.Send(m.Chat, message, tb.ModeHTML)
})
bot.Handle(tb.OnQuery, func(q *tb.Query) {
if strings.TrimSpace(q.Text) == "" {
return
}
splitString := strings.SplitN(q.Text, " ", 2)
var client *mwclient.Client
found := false
lowerText := strings.ToLower(splitString[0])
search := ""
if len(splitString) > 1 {
search = splitString[1]
}
for _, lang := range langs {
if lang == lowerText {
client = clients[lang]
found = true
break
}
}
if !found {
client = clients[defaultLanguage]
search = splitString[0] + " " + search
}
parameters := defaultParameters
parameters["gsrsearch"] = strings.TrimSpace(search)
resp, err := client.Get(parameters)
if err != nil {
log.Println(err)
return
}
pages, err := resp.GetObjectArray("query", "pages")
if err != nil {
log.Println(err)
return
}
results := make(tb.Results, len(pages))
for id, value := range pages {
title, err := value.GetString("title")
if err != nil {
log.Println(err)
return
}
url, err := value.GetString("fullurl")
if err != nil {
log.Println(err)
return
}
result := &tb.ArticleResult{
HideURL: true,
Text: url,
Title: title,
URL: url,
}
result.SetResultID(strconv.Itoa(id))
results[id] = result
}
err = bot.Answer(q, &tb.QueryResponse{
Results: results,
CacheTime: 600,
})
if err != nil {
log.Println(err)
}
})
bot.Start()
}