Get Entity Text from utf8 string via utf16 entities

pull/265/head
temamagic 6 years ago
parent e10891fe60
commit f9e06705d3
  1. 15
      types.go

@ -7,6 +7,7 @@ import (
"net/url"
"strings"
"time"
"unicode/utf16"
)
// APIResponse is a response from the Telegram API with the result
@ -270,6 +271,20 @@ func (entity MessageEntity) ParseURL() (*url.URL, error) {
return url.Parse(entity.URL)
}
// GetEntityText attempts to get entity text value from utf16.
func (m *Message) GetEntityText(entity MessageEntity) string {
if m.Text == "" {
return ""
}
// Encode it into utf16
utfEncodedString := utf16.Encode([]rune(m.Text))
runeString := utf16.Decode(utfEncodedString[entity.Offset : entity.Offset+entity.Length])
// Transform []rune into string
str := string(runeString)
return str
}
// PhotoSize contains information about photos.
type PhotoSize struct {
FileID string `json:"file_id"`