all repos — telebonk @ ca7eca93e8e9ff2f8f448b34de61471eae48fc70

reposter from honk to telegram

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