2016-01-22 11:38:45 +00:00
|
|
|
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 11:38:45 +00:00
|
|
|
|
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
|
2016-01-22 11:38:45 +00:00
|
|
|
|
|
|
|
if r.URL != "" {
|
2016-01-22 13:03:58 +00:00
|
|
|
props["url"] = r.URL
|
2016-01-22 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.ThumbURL != "" {
|
2016-02-18 19:45:31 +00:00
|
|
|
props["thumb_url"] = r.ThumbURL
|
2016-01-22 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.HideURL {
|
2016-01-22 13:03:58 +00:00
|
|
|
props["hide_url"] = "true"
|
2016-01-22 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.DisableWebPagePreview {
|
2016-01-22 13:03:58 +00:00
|
|
|
props["disable_web_page_preview"] = "true"
|
2016-01-22 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(`"":""`)
|
2016-01-22 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteRune('}')
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|