telebot/inline_article.go
Nick Groenen 46992b037b Improve support for inline queries
These changes make it possible to set custom options when responding to
inline queries, as described on
https://core.telegram.org/bots/api#answerinlinequery.

It also includes all the (non-cached) inline result types as described
at https://core.telegram.org/bots/api#inlinequeryresult.

Some remarks:
* The internals of sendCommand have changed. It now expects a
  JSON-serializable object. Instead of doing GET requests with
  URL-encoded query parameters it now POSTS JSON directly.
* Because of the above, sendFile() has changed as well. It now expects a
* `map[string]string` which it will internally convert to URL encoded
  form values.
* Respond has been deprecated in favor of the new AnswerInlineQuery
  function. It is only kept for backward compatibility.
* A dependency on https://github.com/mitchellh/hashstructure has been
  introduced in order to generate automatic IDs for inline results.
2016-06-26 16:33:16 +02:00

99 lines
1.8 KiB
Go

package telebot
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
)
// ArticleResult represents a link to an article or web page.
// Deprecated, use InlineQueryResultArticle instead.
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
// Sends the message silently. iOS users will not receive a notification, Android users will receive a notification with no sound.
DisableNotification 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 is a serializer.
func (r ArticleResult) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
props := map[string]string{}
props["type"] = "article"
props["id"] = r.id()
props["title"] = r.Title
props["description"] = r.Description
props["message_text"] = r.Text
if r.URL != "" {
props["url"] = r.URL
}
if r.ThumbURL != "" {
props["thumb_url"] = r.ThumbURL
}
if r.HideURL {
props["hide_url"] = "true"
}
if r.DisableWebPagePreview {
props["disable_web_page_preview"] = "true"
}
if r.DisableNotification {
props["disable_notification"] = "true"
}
if r.Mode != ModeDefault {
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
}