telebot/inline_article.go

91 lines
1.5 KiB
Go
Raw Normal View History

package telebot
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
)
// ArticleResult represents a link to an article or web page.
type ArticleResult struct {
// [Required!] Title of the result.
Title string
// [Required!] Text of the message to be sent, 1-512 characters.
Text string
// Short description of the result.
Description string
// Markdown, HTML?
Mode ParseMode
// Disables link previews for links in the sent message.
DisableWebPagePreview bool
// URL of the result
URL string
// If true, the URL won't be shown in the message.
HideURL bool
// Result's thumbnail URL.
ThumbURL string
}
func (r ArticleResult) id() string {
sum := md5.Sum([]byte(r.Title + r.Text))
return string(hex.EncodeToString(sum[:]))
}
// MarshalJSON ...
func (r ArticleResult) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
2016-01-22 13:03:58 +00:00
props := map[string]string{}
2016-01-22 13:03:58 +00:00
props["type"] = "article"
props["id"] = r.id()
props["title"] = r.Title
props["description"] = r.Description
props["message_text"] = r.Text
if r.URL != "" {
2016-01-22 13:03:58 +00:00
props["url"] = r.URL
}
if r.ThumbURL != "" {
2016-02-18 19:45:31 +00:00
props["thumb_url"] = r.ThumbURL
}
if r.HideURL {
2016-01-22 13:03:58 +00:00
props["hide_url"] = "true"
}
if r.DisableWebPagePreview {
2016-01-22 13:03:58 +00:00
props["disable_web_page_preview"] = "true"
}
if r.Mode != ModeDefault {
2016-01-22 13:03:58 +00:00
props["parse_mode"] = string(r.Mode)
}
b.WriteRune('{')
if len(props) > 0 {
const tpl = `"%s":"%s",`
for key, value := range props {
b.WriteString(fmt.Sprintf(tpl, key, value))
}
// the last
b.WriteString(`"":""`)
}
b.WriteRune('}')
return b.Bytes(), nil
}