main.go (view raw)
1// Telebonk is a reposter from Honk to Telegram.
2package main
3
4import (
5 "bytes"
6 "encoding/json"
7 "flag"
8 "fmt"
9 "io"
10 "log"
11 "net/http"
12 "net/url"
13 "regexp"
14 "sort"
15 "strconv"
16 "strings"
17 "time"
18)
19
20// A Config is holding configuration of telebonk.
21type Config struct {
22 TgBotToken string
23 TgChatID string
24 TgApiURL string
25 HonkAuthToken string
26 HonkURL string
27}
28
29// Check makes sure that no Config fields are set to empty strings.
30func (c *Config) Check() error {
31 var what string
32 if c.TgBotToken == "" {
33 what = "bot_token"
34 }
35 if c.TgChatID == "" {
36 what = "chat_id"
37 }
38 if c.TgApiURL == "" {
39 what = "tgapi_url"
40 }
41 if c.HonkAuthToken == "" {
42 what = "honk_token"
43 }
44 if c.HonkURL == "" {
45 what = "honk_url"
46 }
47 if what != "" {
48 return fmt.Errorf("'%s' shouldn't be empty", what)
49 }
50 return nil
51}
52
53var config = &Config{}
54
55// A Honk is a post from honk.
56type Honk struct {
57 ID int // unique id of a post
58 What string // type of an action (post, repost, reply)
59 Oondle string // e-mail style handle of the original author of a post
60 Oonker string // url of the original author of a post
61 XID string // url of a post, also unique
62 RID string // url of a post that current post is replying to
63 Date time.Time // datetime of a post
64 Precis string // post summary
65 Noise string // contents of a post
66 Onts []string // a slice of tags in a post
67 Donks []*Donk // a slice of attachments to a post
68
69 MessID int // telegram message_id
70 ReplyToID int // telegram message_id of a message to reply to
71 Action HonkAction
72}
73
74// A HonkAction tells what to do with the saved honk.
75type HonkAction int
76
77const (
78 HonkIgnore HonkAction = iota
79 HonkSend
80 HonkEdit
81)
82
83// A Donk stores metadata for media files.
84type Donk struct {
85 URL string
86 Media string // mime-type of an attachment
87 Desc string
88}
89
90// Check performs some checks on a Honk to filter out what's not going to be posted.
91//
92// It rejects honk if if falls into one of these categories:
93// - it is posted before the telebonk started
94// - it is replying to a honk that's not posted by telebonk (either a remote honk or old honk)
95// - it is of unsupported type (not a regular honk, reply or bonk)
96// - it contains a `#notg` tag.
97// - it is empty
98func (h *Honk) Check() error {
99 log.Print("checking honk #", h.ID) // info
100 if h.Date.Before(now) {
101 return fmt.Errorf("honk #%d is old", h.ID)
102 }
103 switch h.What {
104 case "honked", "bonked":
105 break
106 case "honked back":
107 hi, ok := honkMap[h.RID]
108 if !ok {
109 return fmt.Errorf("cannot reply to nonexisting telebonk")
110 }
111 h.ReplyToID = hi.MessID
112 default:
113 return fmt.Errorf("unsupported honk type: %s", h.What)
114 }
115
116 for _, ont := range h.Onts {
117 if strings.ToLower(ont) == "#notg" {
118 return fmt.Errorf("skipping #notg honk")
119 }
120 }
121
122 if h.Noise == emptyNoise && len(h.Donks) == 0 {
123 return fmt.Errorf("empty honk")
124 }
125 return nil
126}
127
128func (h *Honk) Decide() {
129 hi, ok := honkMap[h.XID]
130 if ok {
131 if hi.MessID == 0 || hi.Date.Equal(h.Date) {
132 h.save(hi.MessID)
133 h.Action = HonkIgnore
134 }
135 h.Action = HonkEdit
136 }
137 h.Action = HonkSend
138}
139
140// save records a Honk to a honkMap
141func (h *Honk) save(messID int) {
142 h.MessID = messID
143 honkMap[h.XID] = h
144}
145
146// forget removes a Honk from a honkMap
147func (h *Honk) forget() {
148 delete(honkMap, h.XID)
149}
150
151// A Mess holds data for a message to be sent to Telegram.
152type Mess struct {
153 Text string `json:"text"`
154 ParseMode string `json:"parse_mode"`
155 ChatID string `json:"chat_id"`
156 MessageID int `json:"message_id,omitempty"`
157 ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
158
159 Document string `json:"document,omitempty"`
160 Photo string `json:"photo,omitempty"`
161 Caption string `json:"caption,omitempty"`
162
163 kind messKind
164}
165
166// A TelegramResponse is a response from Telegram API.
167type TelegramResponse struct {
168 Ok bool
169 Description string
170 Result telegramResponseResult
171}
172
173type telegramResponseResult struct {
174 MessageID int `json:"message_id"`
175}
176
177// NewMess creates and populates a new Mess with default values.
178func NewMess(parseMode string) *Mess {
179 return &Mess{
180 ParseMode: parseMode,
181 ChatID: config.TgChatID,
182 kind: messHonk,
183 }
184}
185
186// NewMessFromHonk creates a slice of Mess objects from existing Honk.
187func NewMessFromHonk(honk *Honk) []*Mess {
188 // donks should be sent as a separate messages, so need to create all of 'em
189 // cap(messes) = 1 for honk + 1 for each donk
190 var messes = make([]*Mess, 0, 1+len(honk.Donks))
191
192 messes = append(messes, NewMess("html"))
193 for _, donk := range honk.Donks {
194 donkMess := NewMess("MarkdownV2") // donks don't contain html
195 donkMess.Caption = donk.Desc // FIXME: no check for length
196 switch {
197 case strings.HasPrefix(donk.Media, "image/"):
198 donkMess.Photo = donk.URL
199 donkMess.kind = messDonkPht
200 case donk.Media == "application/pdf", donk.Media == "text/plain":
201 donkMess.Document = donk.URL
202 donkMess.kind = messDonkDoc
203 }
204 messes = append(messes, donkMess)
205 }
206 if honk.Noise == emptyNoise {
207 messes = messes[1:] // just donks
208 }
209
210 if honk.Action == HonkEdit {
211 // TODO: implement editing documents and photos
212 messes[0].kind = messEdit
213 messes[0].MessageID = honk.MessID
214 messes = messes[:1] // don't donk if editing
215 }
216
217 var noise = calmNoise(honk.Noise)
218 // bonk, then honk back - ok
219 // honk back, then bonk - not gonna sync, is it ok?
220 // upd: bonks work really confusing
221 switch honk.What {
222 case "honked":
223 break
224 case "honked back":
225 messes[0].ReplyToMessageID = honk.ReplyToID
226 case "bonked":
227 oonker := fmt.Sprintf("<a href=\"%s\">%s</a>:", honk.Oonker, honk.Oondle)
228 noise = oonker + "\n" + noise
229 }
230
231 // danger zone handling
232 if strings.HasPrefix(honk.Precis, "DZ:") {
233 noise = strings.Join([]string{"<tg-spoiler>", "</tg-spoiler>"}, noise)
234 noise = honk.Precis + "\n" + noise
235 }
236 messes[0].Text = noise
237
238 return messes
239}
240
241// Send sends a Mess to Telegram.
242func (m *Mess) Send() (*TelegramResponse, error) {
243 var apiURL = botAPIMethod(tgSendMessage)
244
245 switch m.kind {
246 case messHonk:
247 // noop
248 case messEdit:
249 apiURL = botAPIMethod(tgEditMessageText)
250 case messDonkPht:
251 apiURL = botAPIMethod(tgSendPhoto)
252 case messDonkDoc:
253 apiURL = botAPIMethod(tgSendDocument)
254 }
255
256 junk, err := json.Marshal(m)
257 if err != nil {
258 return nil, err
259 }
260 buf := bytes.NewBuffer(junk)
261 req, err := http.NewRequest("POST", apiURL, buf)
262 if err != nil {
263 return nil, err
264 }
265 req.Header.Add("Content-type", "application/json")
266 req.Header.Add("Content-length", strconv.Itoa(buf.Len()))
267
268 resp, err := client.Do(req)
269 if err != nil {
270 return nil, err
271 }
272 defer resp.Body.Close()
273
274 var res TelegramResponse
275 json.NewDecoder(resp.Body).Decode(&res)
276 if !res.Ok {
277 return nil, fmt.Errorf("mess send: %s", res.Description)
278 }
279
280 return &res, nil
281}
282
283type messKind int
284
285const (
286 messHonk messKind = iota
287 messEdit
288 messDonkPht
289 messDonkDoc
290)
291
292func botAPIMethod(method string) string {
293 return fmt.Sprintf("%s/bot%s/%s", config.TgApiURL, config.TgBotToken, method)
294}
295
296func checkTgAPI() error {
297 var apiURL = botAPIMethod(tgGetMe)
298 resp, err := client.Get(apiURL)
299 if err != nil {
300 return err
301 }
302 if resp.StatusCode != 200 {
303 status, _ := io.ReadAll(resp.Body)
304 return fmt.Errorf("status: %d: %s", resp.StatusCode, status)
305 }
306 return nil
307}
308
309// Telegram Bot API methods
310const (
311 tgGetMe = "getMe"
312 tgSendMessage = "sendMessage"
313 tgEditMessageText = "editMessageText"
314 tgSendPhoto = "sendPhoto"
315 tgSendDocument = "sendDocument"
316)
317
318// getHonks receives and unmarshals some honks from a Honk instance.
319func getHonks(after int) ([]*Honk, error) {
320 query := url.Values{}
321 query.Set("token", config.HonkAuthToken)
322 query.Set("action", "gethonks")
323 query.Set("page", "home")
324 query.Set("after", strconv.Itoa(after))
325
326 resp, err := client.Get(fmt.Sprint(config.HonkURL, "/api?", query.Encode()))
327 if err != nil {
328 return nil, err
329 }
330 defer resp.Body.Close()
331
332 // honk outputs junk like `{ "honks": [ ... ] }`, need to get into the list
333 var honkJunk map[string][]*Honk
334 err = json.NewDecoder(resp.Body).Decode(&honkJunk)
335 if err != nil {
336 return nil, err
337 }
338
339 honks := honkJunk["honks"]
340 // honk.ID monotonically increases, so it can be used to sort honks
341 sort.Slice(honks, func(i, j int) bool { return honks[i].ID < honks[j].ID })
342
343 return honks, nil
344}
345
346var (
347 rePTags = regexp.MustCompile(`<\/?p>`)
348 reHlTags = regexp.MustCompile(`<\/?span( class="[a-z]{2}")?>`)
349 reBrTags = regexp.MustCompile(`<br>`)
350 reImgTags = regexp.MustCompile(`<img .*src="(.*)">`)
351 reUlTags = regexp.MustCompile(`<\/?ul>`)
352 reTbTags = regexp.MustCompile(`<table>.*</table>`)
353
354 reLiTags = regexp.MustCompile(`<li>([^<>\/]*)<\/li>`)
355 reHnTags = regexp.MustCompile(`<h[1-6]>(.*)</h[1-6]>`)
356 reHrTags = regexp.MustCompile(`<hr>.*<\/hr>`)
357 reBqTags = regexp.MustCompile(`<blockquote>(.*)<\/blockquote>`)
358)
359
360const emptyNoise = "<p></p>\n"
361
362// calmNoise erases and rewrites html tags that are not supported by Telegram.
363func calmNoise(noise string) string {
364 // TODO: check length of a honk
365
366 // delete these
367 noise = rePTags.ReplaceAllString(noise, "")
368 noise = reHlTags.ReplaceAllString(noise, "")
369 noise = reBrTags.ReplaceAllString(noise, "")
370 noise = reImgTags.ReplaceAllString(noise, "")
371 noise = reUlTags.ReplaceAllString(noise, "")
372 noise = reTbTags.ReplaceAllString(noise, "")
373
374 // these can be repurposed
375 noise = reHrTags.ReplaceAllString(noise, "---\n")
376 noise = reHnTags.ReplaceAllString(noise, "<b>$1</b>\n\n")
377 noise = reBqTags.ReplaceAllString(noise, "| <i>$1</i>")
378 noise = reLiTags.ReplaceAllString(noise, "* $1\n")
379
380 return strings.TrimSpace(noise)
381}
382
383func init() {
384 flag.StringVar(&config.TgBotToken, "bot_token", "", "Telegram bot token")
385 flag.StringVar(&config.TgChatID, "chat_id", "", "Telegram chat_id")
386 flag.StringVar(&config.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL")
387 flag.StringVar(&config.HonkAuthToken, "honk_token", "", "Honk auth token")
388 flag.StringVar(&config.HonkURL, "honk_url", "", "URL of a Honk instance")
389
390 flag.Parse()
391
392 if err := config.Check(); err != nil {
393 log.Fatal("config:", err) // fail
394 }
395
396 if err := checkTgAPI(); err != nil {
397 log.Fatal(err) // fail
398 }
399}
400
401var client = http.DefaultClient
402var honkMap = make(map[string]*Honk) // FIXME: not safe for use by multiple goroutines!
403var now = time.Now()
404
405func main() {
406 var retry = 5
407
408 for {
409 honks, err := getHonks(0)
410 if err != nil {
411 log.Print("gethonks:", err) // error
412 retry--
413 if retry == 0 {
414 log.Fatal("gethonks: giving up") // fail
415 }
416 time.Sleep(5 * time.Second)
417 continue
418 }
419
420 HonkLoop:
421 for _, honk := range honks {
422 honk.Decide()
423 switch honk.Action {
424 case HonkIgnore:
425 continue
426 case HonkSend, HonkEdit:
427 if err := honk.Check(); err != nil {
428 log.Print(err) // error
429 continue
430 }
431 }
432 messes := NewMessFromHonk(honk)
433 // messes[0] is a honk or a donk to be sent
434 resp, err := messes[0].Send()
435 if err != nil {
436 log.Print(err) // error
437 honk.forget() // retry
438 continue
439 }
440 // remember only the first mess' response
441 honk.save(resp.Result.MessageID)
442 for _, mess := range messes[1:] {
443 if _, err := mess.Send(); err != nil {
444 log.Print(err) // error
445 continue HonkLoop
446 }
447 }
448 }
449 time.Sleep(30 * time.Second)
450 }
451}