Add untested support for Inline Queries.

bot-api-6.1
Syfaro 2016-01-01 00:10:19 -06:00
parent f219f3e9db
commit 1ae7803be0
3 changed files with 135 additions and 20 deletions

45
bot.go
View File

@ -121,29 +121,31 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
buf := bytes.NewBuffer(f.Bytes) buf := bytes.NewBuffer(f.Bytes)
ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf) ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
case FileReader: case FileReader:
if f.Size == -1 { if f.Size != -1 {
data, err := ioutil.ReadAll(f.Reader) ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
if err != nil {
return APIResponse{}, err
}
buf := bytes.NewBuffer(data)
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
break break
} }
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader) data, err := ioutil.ReadAll(f.Reader)
if err != nil {
return APIResponse{}, err
}
buf := bytes.NewBuffer(data)
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
default: default:
return APIResponse{}, errors.New("bad file type") return APIResponse{}, errors.New("bad file type")
} }
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil) req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
ms.SetupRequest(req)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
ms.SetupRequest(req)
res, err := bot.Client.Do(req) res, err := bot.Client.Do(req)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
@ -156,7 +158,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
} }
if bot.Debug { if bot.Debug {
log.Println(string(bytes[:])) log.Println(string(bytes))
} }
var apiResp APIResponse var apiResp APIResponse
@ -194,9 +196,7 @@ func (bot *BotAPI) GetMe() (User, error) {
var user User var user User
json.Unmarshal(resp.Result, &user) json.Unmarshal(resp.Result, &user)
if bot.Debug { bot.debugLog("getMe", nil, user)
log.Printf("getMe: %+v\n", user)
}
return user, nil return user, nil
} }
@ -451,3 +451,20 @@ func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler
return updatesChan, handler return updatesChan, handler
} }
// AnswerInlineQuery sends a response to an inline query.
func (bot *BotAPI) AnswerInlineQuery(config InlineConfig) (APIResponse, error) {
v := url.Values{}
v.Add("inline_query_id", config.InlineQueryID)
v.Add("cache_time", strconv.Itoa(config.CacheTime))
v.Add("is_personal", strconv.FormatBool(config.IsPersonal))
v.Add("next_offset", config.NextOffset)
data, err := json.Marshal(config.Results)
if err != nil {
return APIResponse{}, err
}
v.Add("results", string(data))
return bot.MakeRequest("answerInlineQuery", v)
}

View File

@ -38,13 +38,13 @@ const (
ModeMarkdown = "Markdown" ModeMarkdown = "Markdown"
) )
//Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others) // Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others)
type Chattable interface { type Chattable interface {
Values() (url.Values, error) Values() (url.Values, error)
Method() string Method() string
} }
//Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig) // Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
type Fileable interface { type Fileable interface {
Chattable Chattable
Params() (map[string]string, error) Params() (map[string]string, error)
@ -53,7 +53,7 @@ type Fileable interface {
UseExistingFile() bool UseExistingFile() bool
} }
// BaseChat is base struct for all chat event(Message, Photo and so on) // BaseChat is base struct for all chat events (Message, Photo and so on)
type BaseChat struct { type BaseChat struct {
ChatID int ChatID int
ChannelUsername string ChannelUsername string
@ -86,7 +86,7 @@ func (chat *BaseChat) Values() (url.Values, error) {
return v, nil return v, nil
} }
// BaseFile is base struct for all file events(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig) // BaseFile is base struct for all file events (PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
type BaseFile struct { type BaseFile struct {
BaseChat BaseChat
FilePath string FilePath string
@ -505,3 +505,12 @@ type FileReader struct {
Reader io.Reader Reader io.Reader
Size int64 Size int64
} }
// InlineConfig contains information on making an InlineQuery response.
type InlineConfig struct {
InlineQueryID string `json:"inline_query_id"`
Results []InlineQueryResult `json:"results"`
CacheTime int `json:"cache_time"`
IsPersonal bool `json:"is_personal"`
NextOffset string `json:"next_offset"`
}

View File

@ -17,8 +17,9 @@ type APIResponse struct {
// Update is an update response, from GetUpdates. // Update is an update response, from GetUpdates.
type Update struct { type Update struct {
UpdateID int `json:"update_id"` UpdateID int `json:"update_id"`
Message Message `json:"message"` Message Message `json:"message"`
InlineQuery InlineQuery `json:"inline_query"`
} }
// User is a user, contained in Message and returned by GetSelf. // User is a user, contained in Message and returned by GetSelf.
@ -257,3 +258,91 @@ type ForceReply struct {
ForceReply bool `json:"force_reply"` ForceReply bool `json:"force_reply"`
Selective bool `json:"selective"` Selective bool `json:"selective"`
} }
// InlineQuery is a Query from Telegram for an inline request
type InlineQuery struct {
ID string `json:"id"`
From User `json:"user"`
Query string `json:"query"`
Offset string `json:"offset"`
}
// InlineQueryResult is the base type that all InlineQuery Results have.
type InlineQueryResult struct {
Type string `json:"type"` // required
ID string `json:"id"` // required
}
// InlineQueryResultArticle is an inline query response article.
type InlineQueryResultArticle struct {
InlineQueryResult
Title string `json:"title"` // required
MessageText string `json:"message_text"` // required
ParseMode string `json:"parse_mode"` // required
DisableWebPagePreview bool `json:"disable_web_page_preview"`
URL string `json:"url"`
HideURL bool `json:"hide_url"`
Description string `json:"description"`
ThumbURL string `json:"thumb_url"`
ThumbWidth int `json:"thumb_width"`
ThumbHeight int `json:"thumb_height"`
}
// InlineQueryResultPhoto is an inline query response photo.
type InlineQueryResultPhoto struct {
InlineQueryResult
URL string `json:"photo_url"` // required
MimeType string `json:"mime_type"`
Width int `json:"photo_width"`
Height int `json:"photo_height"`
ThumbURL string `json:"thumb_url"`
Title string `json:"title"`
Description string `json:"description"`
Caption string `json:"caption"`
MessageText string `json:"message_text"`
ParseMode string `json:"parse_mode"`
DisableWebPagePreview bool `json:"disable_web_page_preview"`
}
// InlineQueryResultGIF is an inline query response GIF.
type InlineQueryResultGIF struct {
InlineQueryResult
URL string `json:"gif_url"` // required
Width int `json:"gif_width"`
Height int `json:"gif_height"`
ThumbURL string `json:"thumb_url"`
Title string `json:"title"`
Caption string `json:"caption"`
MessageText string `json:"message_text"`
ParseMode string `json:"parse_mode"`
DisableWebPagePreview bool `json:"disable_web_page_preview"`
}
// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
type InlineQueryResultMPEG4GIF struct {
InlineQueryResult
URL string `json:"mpeg4_url"` // required
Width int `json:"mpeg4_width"`
Height int `json:"mpeg4_height"`
ThumbURL string `json:"thumb_url"`
Title string `json:"title"`
Caption string `json:"caption"`
MessageText string `json:"message_text"`
ParseMode string `json:"parse_mode"`
DisableWebPagePreview bool `json:"disable_web_page_preview"`
}
// InlineQueryResultVideo is an inline query response video.
type InlineQueryResultVideo struct {
InlineQueryResult
URL string `json:"video_url"` // required
MimeType string `json:"mime_type"` // required
MessageText string `json:"message_text"` // required
ParseMode string `json:"parse_mode"`
DisableWebPagePreview bool `json:"disable_web_page_preview"`
Width int `json:"video_width"`
Height int `json:"video_height"`
ThumbURL string `json:"thumb_url"`
Title string `json:"title"`
Description string `json:"description"`
}