Skip to content

Commit

Permalink
Possibly breaking some builds, but inline message IDs won't work
Browse files Browse the repository at this point in the history
otherwise.
  • Loading branch information
Ian Byrd committed Dec 26, 2017
1 parent f704eef commit bda24d8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
16 changes: 10 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions editable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ 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
// stored in the database as-is or being embedded into
// 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
}
24 changes: 3 additions & 21 deletions inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions inline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package telebot

import (
"strconv"
"time"
)

Expand Down Expand Up @@ -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.
Expand Down
38 changes: 21 additions & 17 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit bda24d8

Please sign in to comment.