Possibly breaking some builds, but inline message IDs won't work

otherwise.
pull/115/head
Ian Byrd 7 years ago
parent f704eefacf
commit bda24d8650

@ -619,10 +619,10 @@ func (b *Bot) Edit(message Editable, what interface{}, options ...interface{}) (
// if inline message
if chatID == 0 {
params["inline_message_id"] = strconv.Itoa(messageID)
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = strconv.Itoa(messageID)
params["message_id"] = messageID
}
sendOpts := extractOptions(options)
@ -646,10 +646,10 @@ func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error
// if inline message
if chatID == 0 {
params["inline_message_id"] = strconv.Itoa(messageID)
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = strconv.Itoa(messageID)
params["message_id"] = messageID
}
respJSON, err := b.Raw("editMessageCaption", params)
@ -676,7 +676,7 @@ func (b *Bot) Delete(message Editable) error {
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": strconv.Itoa(messageID),
"message_id": messageID,
}
respJSON, err := b.Raw("deleteMessage", params)
@ -716,6 +716,10 @@ func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
func (b *Bot) Answer(query *Query, response *QueryResponse) error {
response.QueryID = query.ID
for _, result := range response.Results {
result.Process()
}
respJSON, err := b.Raw("answerInlineQuery", response)
if err != nil {
return err
@ -981,7 +985,7 @@ func (b *Bot) Pin(message Editable, options ...interface{}) error {
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": strconv.Itoa(messageID),
"message_id": messageID,
}
sendOpts := extractOptions(options)

@ -13,7 +13,7 @@ type Editable interface {
// MessageSig is a "message signature".
//
// For inline messages, return chatID = 0.
MessageSig() (messageID int, chatID int64)
MessageSig() (messageID string, chatID int64)
}
// StoredMessage is an example struct suitable for being
@ -21,10 +21,10 @@ type Editable interface {
// a larger struct, which is often the case (you might
// want to store some metadata alongside, or might not.)
type StoredMessage struct {
MessageID int `sql:"message_id" json:"message_id"`
ChatID int64 `sql:"chat_id" json:"chat_id"`
MessageID string `sql:"message_id" json:"message_id"`
ChatID int64 `sql:"chat_id" json:"chat_id"`
}
func (x StoredMessage) MessageSig() (int, int64) {
func (x StoredMessage) MessageSig() (string, int64) {
return x.MessageID, x.ChatID
}

@ -3,24 +3,13 @@ package telebot
import (
"encoding/json"
"fmt"
"hash/fnv"
"strconv"
"github.com/mitchellh/hashstructure"
"github.com/pkg/errors"
)
// inlineQueryHashOptions sets the HashOptions to be used when hashing
// an inline query result (used to generate IDs).
var inlineQueryHashOptions = &hashstructure.HashOptions{
Hasher: fnv.New64(),
}
// Query is an incoming inline query. When the user sends
// an empty query, your bot could return some default or
// trending results.
type Query struct {
// Unique identifier for this query.
// Unique identifier for this query. 1-64 bytes.
ID string `json:"id"`
// Sender.
@ -76,24 +65,17 @@ type QueryResponse struct {
type Result interface {
ResultID() string
SetResultID(string)
Process()
}
// Results is a slice wrapper for convenient marshalling.
type Results []Result
// MarshalJSON makes sure IQRs have proper IDs and Type variables set.
//
// If ID of some result appears empty, it gets set to a new hash.
// JSON-specific Type gets infered from the actual (specific) IQR type.
func (results Results) MarshalJSON() ([]byte, error) {
for _, result := range results {
if result.ResultID() == "" {
hash, err := hashstructure.Hash(result, inlineQueryHashOptions)
if err != nil {
return nil, errors.Wrap(err, "telebot: can't hash the result")
}
result.SetResultID(strconv.FormatUint(hash, 16))
result.SetResultID(fmt.Sprintf("%d", &result))
}
if err := inferIQR(result); err != nil {

@ -4,10 +4,10 @@ package telebot
type ResultBase struct {
// Unique identifier for this result, 1-64 Bytes.
// If left unspecified, a 64-bit FNV-1 hash will be calculated
ID string `json:"id",hash:"ignore"`
ID string `json:"id"`
// Ignore. This field gets set automatically.
Type string `json:"type",hash:"ignore"`
Type string `json:"type"`
// Optional. Content of the message to be sent.
Content *InputMessageContent `json:"input_message_content,omitempty"`
@ -26,6 +26,12 @@ func (r *ResultBase) SetResultID(id string) {
r.ID = id
}
func (r *ResultBase) Process() {
if r.ReplyMarkup != nil {
processButtons(r.ReplyMarkup.InlineKeyboard)
}
}
// ArticleResult represents a link to an article or web page.
// See also: https://core.telegram.org/bots/api#inlinequeryresultarticle
type ArticleResult struct {

@ -1,6 +1,7 @@
package telebot
import (
"strconv"
"time"
)
@ -203,8 +204,8 @@ type MessageEntity struct {
}
// MessageSig satisfies Editable interface (see Editable.)
func (m *Message) MessageSig() (int, int64) {
return m.ID, m.Chat.ID
func (m *Message) MessageSig() (string, int64) {
return strconv.Itoa(m.ID), m.Chat.ID
}
// Time returns the moment of message creation in local time.

@ -163,26 +163,30 @@ func embedSendOptions(params map[string]string, opt *SendOptions) {
}
if opt.ReplyMarkup != nil {
keys := opt.ReplyMarkup.InlineKeyboard
if len(keys) > 0 && len(keys[0]) > 0 {
for i, _ := range keys {
for j, _ := range keys[i] {
key := &keys[i][j]
if key.Unique != "" {
// Format: "\f<callback_name>|<data>"
data := key.Data
if data == "" {
key.Data = "\f" + key.Unique
} else {
key.Data = "\f" + key.Unique + "|" + data
}
}
processButtons(opt.ReplyMarkup.InlineKeyboard)
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}
}
func processButtons(keys [][]InlineButton) {
if keys == nil || len(keys) < 1 || len(keys[0]) < 1 {
return
}
for i, _ := range keys {
for j, _ := range keys[i] {
key := &keys[i][j]
if key.Unique != "" {
// Format: "\f<callback_name>|<data>"
data := key.Data
if data == "" {
key.Data = "\f" + key.Unique
} else {
key.Data = "\f" + key.Unique + "|" + data
}
}
}
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}
}

Loading…
Cancel
Save