2016-01-22 11:38:45 +00:00
|
|
|
|
package telebot
|
|
|
|
|
|
2016-06-26 14:05:37 +00:00
|
|
|
|
import (
|
2016-11-10 17:52:45 +00:00
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2016-06-26 14:05:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
2016-01-22 11:38:45 +00:00
|
|
|
|
// 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 {
|
2017-12-26 22:39:15 +00:00
|
|
|
|
// Unique identifier for this query. 1-64 bytes.
|
2016-06-26 14:05:37 +00:00
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
|
|
// Sender.
|
|
|
|
|
From User `json:"from"`
|
|
|
|
|
|
2017-11-26 08:17:52 +00:00
|
|
|
|
// Sender location, only for bots that request user location.
|
|
|
|
|
Location *Location `json:"location"`
|
2016-06-26 14:05:37 +00:00
|
|
|
|
|
|
|
|
|
// Text of the query (up to 512 characters).
|
2016-01-22 11:38:45 +00:00
|
|
|
|
Text string `json:"query"`
|
2016-06-26 14:05:37 +00:00
|
|
|
|
|
|
|
|
|
// Offset of the results to be returned, can be controlled by the bot.
|
|
|
|
|
Offset string `json:"offset"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryResponse builds a response to an inline Query.
|
|
|
|
|
// See also: https://core.telegram.org/bots/api#answerinlinequery
|
|
|
|
|
type QueryResponse struct {
|
|
|
|
|
// The ID of the query to which this is a response.
|
2017-11-24 14:08:23 +00:00
|
|
|
|
//
|
|
|
|
|
// Note: Telebot sets this field automatically!
|
2016-06-26 14:05:37 +00:00
|
|
|
|
QueryID string `json:"inline_query_id"`
|
|
|
|
|
|
|
|
|
|
// The results for the inline query.
|
2017-11-24 14:08:23 +00:00
|
|
|
|
Results Results `json:"results"`
|
2016-06-26 14:05:37 +00:00
|
|
|
|
|
|
|
|
|
// (Optional) The maximum amount of time in seconds that the result
|
|
|
|
|
// of the inline query may be cached on the server.
|
|
|
|
|
CacheTime int `json:"cache_time,omitempty"`
|
|
|
|
|
|
|
|
|
|
// (Optional) Pass True, if results may be cached on the server side
|
|
|
|
|
// only for the user that sent the query. By default, results may
|
|
|
|
|
// be returned to any user who sends the same query.
|
|
|
|
|
IsPersonal bool `json:"is_personal"`
|
|
|
|
|
|
|
|
|
|
// (Optional) Pass the offset that a client should send in the next
|
|
|
|
|
// query with the same text to receive more results. Pass an empty
|
|
|
|
|
// string if there are no more results or if you don‘t support
|
|
|
|
|
// pagination. Offset length can’t exceed 64 bytes.
|
|
|
|
|
NextOffset string `json:"next_offset"`
|
|
|
|
|
|
|
|
|
|
// (Optional) If passed, clients will display a button with specified
|
|
|
|
|
// text that switches the user to a private chat with the bot and sends
|
|
|
|
|
// the bot a start message with the parameter switch_pm_parameter.
|
|
|
|
|
SwitchPMText string `json:"switch_pm_text,omitempty"`
|
|
|
|
|
|
|
|
|
|
// (Optional) Parameter for the start message sent to the bot when user
|
|
|
|
|
// presses the switch button.
|
|
|
|
|
SwitchPMParameter string `json:"switch_pm_parameter,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
|
// Result represents one result of an inline query.
|
|
|
|
|
type Result interface {
|
|
|
|
|
ResultID() string
|
|
|
|
|
SetResultID(string)
|
2017-12-26 22:39:15 +00:00
|
|
|
|
Process()
|
2016-11-10 17:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
|
// Results is a slice wrapper for convenient marshalling.
|
|
|
|
|
type Results []Result
|
2016-11-10 17:52:45 +00:00
|
|
|
|
|
2016-11-10 18:05:52 +00:00
|
|
|
|
// MarshalJSON makes sure IQRs have proper IDs and Type variables set.
|
2017-11-24 14:08:23 +00:00
|
|
|
|
func (results Results) MarshalJSON() ([]byte, error) {
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
if result.ResultID() == "" {
|
2017-12-26 22:39:15 +00:00
|
|
|
|
result.SetResultID(fmt.Sprintf("%d", &result))
|
2016-11-10 17:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 20:10:37 +00:00
|
|
|
|
if err := inferIQR(result); err != nil {
|
2017-11-24 14:08:23 +00:00
|
|
|
|
return nil, err
|
2016-11-10 17:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 20:14:25 +00:00
|
|
|
|
return json.Marshal([]Result(results))
|
2016-06-26 14:05:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
|
func inferIQR(result Result) error {
|
2016-11-10 17:52:45 +00:00
|
|
|
|
switch r := result.(type) {
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *ArticleResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "article"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *AudioResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "audio"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *ContactResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "contact"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *DocumentResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "document"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *GifResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "gif"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *LocationResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "location"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *Mpeg4GifResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "mpeg4_gif"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *PhotoResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "photo"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *VenueResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "venue"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *VideoResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "video"
|
2017-11-24 14:08:23 +00:00
|
|
|
|
case *VoiceResult:
|
2016-11-10 17:52:45 +00:00
|
|
|
|
r.Type = "voice"
|
2017-11-26 08:17:52 +00:00
|
|
|
|
case *StickerResult:
|
|
|
|
|
r.Type = "sticker"
|
2016-11-10 17:52:45 +00:00
|
|
|
|
default:
|
2017-11-24 14:08:23 +00:00
|
|
|
|
return fmt.Errorf("result %v is not supported", result)
|
2016-06-26 14:05:37 +00:00
|
|
|
|
}
|
2016-11-10 17:52:45 +00:00
|
|
|
|
|
|
|
|
|
return nil
|
2016-01-22 11:38:45 +00:00
|
|
|
|
}
|