summaryrefslogtreecommitdiffstats
path: root/bridge/telegram/handlers.go
blob: efafa9d65c81f643b60d13e9d4ed3720587ec47a (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package btelegram

import (
	"fmt"
	"html"
	"path/filepath"
	"strconv"
	"strings"
	"unicode/utf16"

	"github.com/42wim/matterbridge/bridge/config"
	"github.com/42wim/matterbridge/bridge/helper"
	"github.com/davecgh/go-spew/spew"
	tgbotapi "github.com/matterbridge/telegram-bot-api/v6"
)

func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *tgbotapi.Message) *tgbotapi.Message {
	// handle channels
	if posted != nil {
		if posted.Text == "/chatId" {
			chatID := strconv.FormatInt(posted.Chat.ID, 10)

			// Handle chat topics
			if posted.IsTopicMessage {
				chatID = chatID + "/" + strconv.Itoa(posted.MessageThreadID)
			}

			_, err := b.Send(config.Message{
				Channel: chatID,
				Text:    fmt.Sprintf("ID of this chat: %s", chatID),
			})
			if err != nil {
				b.Log.Warnf("Unable to send chatID to %s", chatID)
			}
		} else {
			message = posted
			rmsg.Text = message.Text
		}
	}

	// edited channel message
	if edited != nil && !b.GetBool("EditDisable") {
		message = edited
		rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
	}
	return message
}

// handleChannels checks if it's a channel message and if the message is a new or edited messages
func (b *Btelegram) handleChannels(rmsg *config.Message, message *tgbotapi.Message, update tgbotapi.Update) *tgbotapi.Message {
	return b.handleUpdate(rmsg, message, update.ChannelPost, update.EditedChannelPost)
}

// handleGroups checks if it's a group message and if the message is a new or edited messages
func (b *Btelegram) handleGroups(rmsg *config.Message, message *tgbotapi.Message, update tgbotapi.Update) *tgbotapi.Message {
	return b.handleUpdate(rmsg, message, update.Message, update.EditedMessage)
}

// handleForwarded handles forwarded messages
func (b *Btelegram) handleForwarded(rmsg *config.Message, message *tgbotapi.Message) {
	if message.ForwardDate == 0 {
		return
	}

	if message.ForwardFromChat != nil && message.ForwardFrom == nil {
		rmsg.Text = "Forwarded from " + message.ForwardFromChat.Title + ": " + rmsg.Text
		return
	}

	if message.ForwardFrom == nil {
		rmsg.Text = "Forwarded from " + unknownUser + ": " + rmsg.Text
		return
	}

	usernameForward := ""
	if b.GetBool("UseFirstName") {
		usernameForward = message.ForwardFrom.FirstName
	}
	if b.GetBool("UseFullName") {
		usernameForward = message.ForwardFrom.FirstName + " " + message.ForwardFrom.LastName
	}

	if usernameForward == "" {
		usernameForward = message.ForwardFrom.UserName
		if usernameForward == "" {
			usernameForward = message.ForwardFrom.FirstName
		}
	}

	if usernameForward == "" {
		usernameForward = unknownUser
	}

	rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
}

// handleQuoting handles quoting of previous messages
func (b *Btelegram) handleQuoting(rmsg *config.Message, message *tgbotapi.Message) {
	// Used to check if the message was a reply to the root topic
	if message.ReplyToMessage != nil && (!message.IsTopicMessage || message.ReplyToMessage.MessageID != message.MessageThreadID) { //nolint:nestif
		usernameReply := ""
		if message.ReplyToMessage.From != nil {
			if b.GetBool("UseFirstName") {
				usernameReply = message.ReplyToMessage.From.FirstName
			}
			if b.GetBool("UseFullName") {
				usernameReply = message.ReplyToMessage.From.FirstName + " " + message.ReplyToMessage.From.LastName
			}
			if usernameReply == "" {
				usernameReply = message.ReplyToMessage.From.UserName
				if usernameReply == "" {
					usernameReply = message.ReplyToMessage.From.FirstName
				}
			}
		}
		if usernameReply == "" {
			usernameReply = unknownUser
		}
		if !b.GetBool("QuoteDisable") {
			quote := message.ReplyToMessage.Text
			if quote == "" {
				quote = message.ReplyToMessage.Caption
			}
			rmsg.Text = b.handleQuote(rmsg.Text, usernameReply, quote)
		}
	}
}

// handleUsername handles the correct setting of the username
func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Message) {
	if message.From != nil {
		rmsg.UserID = strconv.FormatInt(message.From.ID, 10)
		if b.GetBool("UseFirstName") {
			rmsg.Username = message.From.FirstName
		}
		if b.GetBool("UseFullName") {
			if message.From.FirstName != "" && message.From.LastName != "" {
				rmsg.Username = message.From.FirstName + " " + message.From.LastName
			}
		}
		if rmsg.Username == "" {
			rmsg.Username = message.From.UserName
			if rmsg.Username == "" {
				rmsg.Username = message.From.FirstName
			}
		}
		// only download avatars if we have a place to upload them (configured mediaserver)
		if b.General.MediaServerUpload != "" || (b.General.MediaServerDownload != "" && b.General.MediaDownloadPath != "") {
			b.handleDownloadAvatar(message.From.ID, rmsg.Channel)
		}
	}

	if message.SenderChat != nil { //nolint:nestif
		rmsg.UserID = strconv.FormatInt(message.SenderChat.ID, 10)
		if b.GetBool("UseFirstName") {
			rmsg.Username = message.SenderChat.FirstName
		}
		if b.GetBool("UseFullName") {
			if message.SenderChat.FirstName != "" && message.SenderChat.LastName != "" {
				rmsg.Username = message.SenderChat.FirstName + " " + message.SenderChat.LastName
			}
		}

		if rmsg.Username == "" || rmsg.Username == "Channel_Bot" {
			rmsg.Username = message.SenderChat.UserName

			if rmsg.Username == "" || rmsg.Username == "Channel_Bot" {
				rmsg.Username = message.SenderChat.FirstName
			}
		}
		// only download avatars if we have a place to upload them (configured mediaserver)
		if b.General.MediaServerUpload != "" || (b.General.MediaServerDownload != "" && b.General.MediaDownloadPath != "") {
			b.handleDownloadAvatar(message.SenderChat.ID, rmsg.Channel)
		}
	}

	// Fallback on author signature (used in "channel" type of chat)
	if rmsg.Username == "" && message.AuthorSignature != "" {
		rmsg.Username = message.AuthorSignature
	}

	// if we really didn't find a username, set it to unknown
	if rmsg.Username == "" {
		rmsg.Username = unknownUser
	}
}

func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
	for update := range updates {
		b.Log.Debugf("== Receiving event: %#v", update.Message)

		if update.Message == nil && update.ChannelPost == nil &&
			update.EditedMessage == nil && update.EditedChannelPost == nil {
			b.Log.Info("Received event without messages, skipping.")
			continue
		}

		if b.GetInt("debuglevel") == 1 {
			spew.Dump(update.Message)
		}

		b.handleGroupUpdate(update)

		var message *tgbotapi.Message

		rmsg := config.Message{Account: b.Account, Extra: make(map[string][]interface{})}

		// handle channels
		message = b.handleChannels(&rmsg, message, update)

		// handle groups
		message = b.handleGroups(&rmsg, message, update)

		if message == nil {
			b.Log.Error("message is nil, this shouldn't happen.")
			continue
		}

		// set the ID's from the channel or group message
		rmsg.ID = strconv.Itoa(message.MessageID)
		rmsg.Channel = strconv.FormatInt(message.Chat.ID, 10)
		if message.IsTopicMessage {
			rmsg.Channel += "/" + strconv.Itoa(message.MessageThreadID)
		}

		// preserve threading from telegram reply
		if message.ReplyToMessage != nil &&
			// Used to check if the message was a reply to the root topic
			(!message.IsTopicMessage || message.ReplyToMessage.MessageID != message.MessageThreadID) {
			rmsg.ParentID = strconv.Itoa(message.ReplyToMessage.MessageID)
		}

		// handle entities (adding URLs)
		b.handleEntities(&rmsg, message)

		// handle username
		b.handleUsername(&rmsg, message)

		// handle any downloads
		err := b.handleDownload(&rmsg, message)
		if err != nil {
			b.Log.Errorf("download failed: %s", err)
		}

		// handle forwarded messages
		b.handleForwarded(&rmsg, message)

		// quote the previous message
		b.handleQuoting(&rmsg, message)

		if rmsg.Text != "" || len(rmsg.Extra) > 0 {
			// Comment the next line out due to avoid removing empty lines in Telegram
			// rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text)
			// channels don't have (always?) user information. see #410
			if message.From != nil {
				rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.FormatInt(message.From.ID, 10), b.General)
			}

			b.Log.Debugf("<= Sending message from %s on %s to gateway", rmsg.Username, b.Account)
			b.Log.Debugf("<= Message is %#v", rmsg)
			b.Remote <- rmsg
		}
	}
}

func (b *Btelegram) handleGroupUpdate(update tgbotapi.Update) {
	msg := update.Message

	switch {
	case msg.NewChatMembers != nil:
		b.handleUserJoin(update)
	case msg.LeftChatMember != nil:
		b.handleUserLeave(update)
	}
}

func (b *Btelegram) handleUserJoin(update tgbotapi.Update) {
	msg := update.Message
	for _, user := range msg.NewChatMembers {
		rmsg := config.Message{
			UserID:   strconv.FormatInt(user.ID, 10),
			Username: user.FirstName, // for some reason all the other name felids are empty on this event (at least for me)
			Channel:  strconv.FormatInt(msg.Chat.ID, 10),
			Account:  b.Account,
			Protocol: b.Protocol,
			Event:    config.EventJoinLeave,
			Text:     "joined chat",
		}
		b.Remote <- rmsg
	}
}

func (b *Btelegram) handleUserLeave(update tgbotapi.Update) {
	msg := update.Message
	user := msg.LeftChatMember

	rmsg := config.Message{
		UserID:   strconv.FormatInt(user.ID, 10),
		Username: user.FirstName, // for some reason all the other name felids are empty on this event (at least for me)
		Channel:  strconv.FormatInt(msg.Chat.ID, 10),
		Account:  b.Account,
		Protocol: b.Protocol,
		Event:    config.EventJoinLeave,
		Text:     "left chat",
	}

	b.Remote <- rmsg
}

// handleDownloadAvatar downloads the avatar of userid from channel
// sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful.
// logs an error message if it fails
func (b *Btelegram) handleDownloadAvatar(userid int64, channel string) {
	rmsg := config.Message{
		Username: "system",
		Text:     "avatar",
		Channel:  channel,
		Account:  b.Account,
		UserID:   strconv.FormatInt(userid, 10),
		Event:    config.EventAvatarDownload,
		Extra:    make(map[string][]interface{}),
	}

	if _, ok := b.avatarMap[strconv.FormatInt(userid, 10)]; ok {
		return
	}

	photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1})
	if err != nil {
		b.Log.Errorf("Userprofile download failed for %#v %s", userid, err)
	}

	if len(photos.Photos) > 0 {
		photo := photos.Photos[0][0]
		url := b.getFileDirectURL(photo.FileID)
		name := strconv.FormatInt(userid, 10) + ".png"
		b.Log.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize)

		err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(photo.FileSize), b.General)
		if err != nil {
			b.Log.Error(err)
			return
		}
		data, err := helper.DownloadFile(url)
		if err != nil {
			b.Log.Errorf("download %s failed %#v", url, err)
			return
		}
		helper.HandleDownloadData(b.Log, &rmsg, name, rmsg.Text, "", data, b.General)
		b.Remote <- rmsg
	}
}

func (b *Btelegram) maybeConvertTgs(name *string, data *[]byte) {
	format := b.GetString("MediaConvertTgs")
	if helper.SupportsFormat(format) {
		b.Log.Debugf("Format supported by %s, converting %v", helper.LottieBackend(), name)
	} else {
		// Otherwise, no conversion was requested. Trying to run the usual webp
		// converter would fail, because '.tgs.webp' is actually a gzipped JSON
		// file, and has nothing to do with WebP.
		return
	}
	err := helper.ConvertTgsToX(data, format, b.Log)
	if err != nil {
		b.Log.Errorf("conversion failed: %v", err)
	} else {
		*name = strings.Replace(*name, "tgs.webp", format, 1)
	}
}

func (b *Btelegram) maybeConvertWebp(name *string, data *[]byte) {
	if b.GetBool("MediaConvertWebPToPNG") {
		b.Log.Debugf("WebP to PNG conversion enabled, converting %v", name)
		err := helper.ConvertWebPToPNG(data)
		if err != nil {
			b.Log.Errorf("conversion failed: %v", err)
		} else {
			*name = strings.Replace(*name, ".webp", ".png", 1)
		}
	}
}

// handleDownloadFile handles file download
func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Message) error {
	size := int64(0)
	var url, name, text string
	switch {
	case message.Sticker != nil:
		text, name, url = b.getDownloadInfo(message.Sticker.FileID, ".webp", true)
		size = int64(message.Sticker.FileSize)
	case message.Voice != nil:
		text, name, url = b.getDownloadInfo(message.Voice.FileID, ".ogg", true)
		size = message.Voice.FileSize
	case message.Video != nil:
		text, name, url = b.getDownloadInfo(message.Video.FileID, "", true)
		size = message.Video.FileSize
	case message.Audio != nil:
		text, name, url = b.getDownloadInfo(message.Audio.FileID, "", true)
		size = message.Audio.FileSize
	case message.Document != nil:
		_, _, url = b.getDownloadInfo(message.Document.FileID, "", false)
		size = message.Document.FileSize
		name = message.Document.FileName
		text = " " + message.Document.FileName + " : " + url
	case message.Photo != nil:
		photos := message.Photo
		size = int64(photos[len(photos)-1].FileSize)
		text, name, url = b.getDownloadInfo(photos[len(photos)-1].FileID, "", true)
	}

	// if name is empty we didn't match a thing to download
	if name == "" {
		return nil
	}
	// use the URL instead of native upload
	if b.GetBool("UseInsecureURL") {
		b.Log.Debugf("Setting message text to :%s", text)
		rmsg.Text += text
		return nil
	}
	// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
	err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General)
	if err != nil {
		return err
	}
	data, err := helper.DownloadFile(url)
	if err != nil {
		return err
	}

	if strings.HasSuffix(name, ".tgs.webp") {
		b.maybeConvertTgs(&name, data)
	} else if strings.HasSuffix(name, ".webp") {
		b.maybeConvertWebp(&name, data)
	}

	// rename .oga to .ogg  https://github.com/42wim/matterbridge/issues/906#issuecomment-741793512
	if strings.HasSuffix(name, ".oga") && message.Audio != nil {
		name = strings.Replace(name, ".oga", ".ogg", 1)
	}

	helper.HandleDownloadData(b.Log, rmsg, name, message.Caption, "", data, b.General)
	return nil
}

func (b *Btelegram) getDownloadInfo(id string, suffix string, urlpart bool) (string, string, string) {
	url := b.getFileDirectURL(id)
	name := ""
	if urlpart {
		urlPart := strings.Split(url, "/")
		name = urlPart[len(urlPart)-1]
	}
	if suffix != "" && !strings.HasSuffix(name, suffix) && !strings.HasSuffix(name, ".webm") {
		name += suffix
	}
	text := " " + url
	return text, name, url
}

// handleDelete handles message deleting
func (b *Btelegram) handleDelete(msg *config.Message, chatid int64) (string, error) {
	if msg.ID == "" {
		return "", nil
	}

	msgid, err := strconv.Atoi(msg.ID)
	if err != nil {
		return "", err
	}

	cfg := tgbotapi.NewDeleteMessage(chatid, msgid)
	_, err = b.c.Request(cfg)

	return "", err
}

// handleEdit handles message editing.
func (b *Btelegram) handleEdit(msg *config.Message, chatid int64) (string, error) {
	msgid, err := strconv.Atoi(msg.ID)
	if err != nil {
		return "", err
	}
	if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
		b.Log.Debug("Using mode HTML - nick only")
		msg.Text = html.EscapeString(msg.Text)
	}
	m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
	switch b.GetString("MessageFormat") {
	case HTMLFormat:
		b.Log.Debug("Using mode HTML")
		m.ParseMode = tgbotapi.ModeHTML
	case "Markdown":
		b.Log.Debug("Using mode markdown")
		m.ParseMode = tgbotapi.ModeMarkdown
	case MarkdownV2:
		b.Log.Debug("Using mode MarkdownV2")
		m.ParseMode = MarkdownV2
	}
	if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
		b.Log.Debug("Using mode HTML - nick only")
		m.ParseMode = tgbotapi.ModeHTML
	}
	_, err = b.c.Send(m)
	if err != nil {
		return "", err
	}
	return "", nil
}

// handleUploadFile handles native upload of files
func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64, threadid int, parentID int) (string, error) {
	var media []interface{}
	for _, f := range msg.Extra["file"] {
		fi := f.(config.FileInfo)
		file := tgbotapi.FileBytes{
			Name:  fi.Name,
			Bytes: *fi.Data,
		}

		if b.GetString("MessageFormat") == HTMLFormat {
			fi.Comment = makeHTML(html.EscapeString(fi.Comment))
		}

		switch filepath.Ext(fi.Name) {
		case ".jpg", ".jpe", ".png":
			pc := tgbotapi.NewInputMediaPhoto(file)
			if fi.Comment != "" {
				pc.Caption, pc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
			}
			media = append(media, pc)
		case ".mp4", ".m4v":
			vc := tgbotapi.NewInputMediaVideo(file)
			if fi.Comment != "" {
				vc.Caption, vc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
			}
			media = append(media, vc)
		case ".mp3", ".oga":
			ac := tgbotapi.NewInputMediaAudio(file)
			if fi.Comment != "" {
				ac.Caption, ac.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
			}
			media = append(media, ac)
		case ".ogg":
			voc := tgbotapi.NewVoice(chatid, file)
			voc.Caption, voc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
			voc.ReplyToMessageID = parentID
			res, err := b.c.Send(voc)
			if err != nil {
				return "", err
			}
			return strconv.Itoa(res.MessageID), nil
		default:
			dc := tgbotapi.NewInputMediaDocument(file)
			if fi.Comment != "" {
				dc.Caption, dc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
			}
			media = append(media, dc)
		}
	}

	return b.sendMediaFiles(msg, chatid, threadid, parentID, media)
}

func (b *Btelegram) handleQuote(message, quoteNick, quoteMessage string) string {
	format := b.GetString("quoteformat")
	if format == "" {
		format = "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
	}
	quoteMessagelength := len([]rune(quoteMessage))
	if b.GetInt("QuoteLengthLimit") != 0 && quoteMessagelength >= b.GetInt("QuoteLengthLimit") {
		runes := []rune(quoteMessage)
		quoteMessage = string(runes[0:b.GetInt("QuoteLengthLimit")])
		if quoteMessagelength > b.GetInt("QuoteLengthLimit") {
			quoteMessage += "..."
		}
	}
	format = strings.Replace(format, "{MESSAGE}", message, -1)
	format = strings.Replace(format, "{QUOTENICK}", quoteNick, -1)
	format = strings.Replace(format, "{QUOTEMESSAGE}", quoteMessage, -1)
	return format
}

// handleEntities handles messageEntities
func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Message) {
	if message.Entities == nil {
		return
	}

	indexMovedBy := 0
	prevLinkOffset := -1

	for _, e := range message.Entities {

		asRunes := utf16.Encode([]rune(rmsg.Text))

		if e.Type == "text_link" {
			offset := e.Offset + indexMovedBy
			url, err := e.ParseURL()
			if err != nil {
				b.Log.Errorf("entity text_link url parse failed: %s", err)
				continue
			}
			utfEncodedString := utf16.Encode([]rune(rmsg.Text))
			if offset+e.Length > len(utfEncodedString) {
				b.Log.Errorf("entity length is too long %d > %d", offset+e.Length, len(utfEncodedString))
				continue
			}
			rmsg.Text = string(utf16.Decode(asRunes[:offset+e.Length])) + " (" + url.String() + ")" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += len(url.String()) + 3
			prevLinkOffset = e.Offset
		}

		if e.Offset == prevLinkOffset {
			continue
		}

		if e.Type == "code" {
			offset := e.Offset + indexMovedBy
			rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "`" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "`" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += 2
		}

		if e.Type == "pre" {
			offset := e.Offset + indexMovedBy
			rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "```\n" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "```\n" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += 8
		}

		if e.Type == "bold" {
			offset := e.Offset + indexMovedBy
			rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "*" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "*" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += 2
		}
		if e.Type == "italic" {
			offset := e.Offset + indexMovedBy
			rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "_" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "_" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += 2
		}
		if e.Type == "strike" {
			offset := e.Offset + indexMovedBy
			rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "~" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "~" + string(utf16.Decode(asRunes[offset+e.Length:]))
			indexMovedBy += 2
		}
	}
}