Skip to content

Commit 1ae7803

Browse files
author
Syfaro
committed
Add untested support for Inline Queries.
1 parent f219f3e commit 1ae7803

File tree

3 files changed

+135
-20
lines changed

3 files changed

+135
-20
lines changed

bot.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,31 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
121121
buf := bytes.NewBuffer(f.Bytes)
122122
ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
123123
case FileReader:
124-
if f.Size == -1 {
125-
data, err := ioutil.ReadAll(f.Reader)
126-
if err != nil {
127-
return APIResponse{}, err
128-
}
129-
buf := bytes.NewBuffer(data)
130-
131-
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
124+
if f.Size != -1 {
125+
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
132126

133127
break
134128
}
135129

136-
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
130+
data, err := ioutil.ReadAll(f.Reader)
131+
if err != nil {
132+
return APIResponse{}, err
133+
}
134+
135+
buf := bytes.NewBuffer(data)
136+
137+
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
137138
default:
138139
return APIResponse{}, errors.New("bad file type")
139140
}
140141

141142
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
142-
ms.SetupRequest(req)
143143
if err != nil {
144144
return APIResponse{}, err
145145
}
146146

147+
ms.SetupRequest(req)
148+
147149
res, err := bot.Client.Do(req)
148150
if err != nil {
149151
return APIResponse{}, err
@@ -156,7 +158,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
156158
}
157159

158160
if bot.Debug {
159-
log.Println(string(bytes[:]))
161+
log.Println(string(bytes))
160162
}
161163

162164
var apiResp APIResponse
@@ -194,9 +196,7 @@ func (bot *BotAPI) GetMe() (User, error) {
194196
var user User
195197
json.Unmarshal(resp.Result, &user)
196198

197-
if bot.Debug {
198-
log.Printf("getMe: %+v\n", user)
199-
}
199+
bot.debugLog("getMe", nil, user)
200200

201201
return user, nil
202202
}
@@ -451,3 +451,20 @@ func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler
451451

452452
return updatesChan, handler
453453
}
454+
455+
// AnswerInlineQuery sends a response to an inline query.
456+
func (bot *BotAPI) AnswerInlineQuery(config InlineConfig) (APIResponse, error) {
457+
v := url.Values{}
458+
459+
v.Add("inline_query_id", config.InlineQueryID)
460+
v.Add("cache_time", strconv.Itoa(config.CacheTime))
461+
v.Add("is_personal", strconv.FormatBool(config.IsPersonal))
462+
v.Add("next_offset", config.NextOffset)
463+
data, err := json.Marshal(config.Results)
464+
if err != nil {
465+
return APIResponse{}, err
466+
}
467+
v.Add("results", string(data))
468+
469+
return bot.MakeRequest("answerInlineQuery", v)
470+
}

configs.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ const (
3838
ModeMarkdown = "Markdown"
3939
)
4040

41-
//Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others)
41+
// Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others)
4242
type Chattable interface {
4343
Values() (url.Values, error)
4444
Method() string
4545
}
4646

47-
//Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
47+
// Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
4848
type Fileable interface {
4949
Chattable
5050
Params() (map[string]string, error)
@@ -53,7 +53,7 @@ type Fileable interface {
5353
UseExistingFile() bool
5454
}
5555

56-
// BaseChat is base struct for all chat event(Message, Photo and so on)
56+
// BaseChat is base struct for all chat events (Message, Photo and so on)
5757
type BaseChat struct {
5858
ChatID int
5959
ChannelUsername string
@@ -86,7 +86,7 @@ func (chat *BaseChat) Values() (url.Values, error) {
8686
return v, nil
8787
}
8888

89-
// BaseFile is base struct for all file events(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
89+
// BaseFile is base struct for all file events (PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
9090
type BaseFile struct {
9191
BaseChat
9292
FilePath string
@@ -505,3 +505,12 @@ type FileReader struct {
505505
Reader io.Reader
506506
Size int64
507507
}
508+
509+
// InlineConfig contains information on making an InlineQuery response.
510+
type InlineConfig struct {
511+
InlineQueryID string `json:"inline_query_id"`
512+
Results []InlineQueryResult `json:"results"`
513+
CacheTime int `json:"cache_time"`
514+
IsPersonal bool `json:"is_personal"`
515+
NextOffset string `json:"next_offset"`
516+
}

types.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ type APIResponse struct {
1717

1818
// Update is an update response, from GetUpdates.
1919
type Update struct {
20-
UpdateID int `json:"update_id"`
21-
Message Message `json:"message"`
20+
UpdateID int `json:"update_id"`
21+
Message Message `json:"message"`
22+
InlineQuery InlineQuery `json:"inline_query"`
2223
}
2324

2425
// User is a user, contained in Message and returned by GetSelf.
@@ -257,3 +258,91 @@ type ForceReply struct {
257258
ForceReply bool `json:"force_reply"`
258259
Selective bool `json:"selective"`
259260
}
261+
262+
// InlineQuery is a Query from Telegram for an inline request
263+
type InlineQuery struct {
264+
ID string `json:"id"`
265+
From User `json:"user"`
266+
Query string `json:"query"`
267+
Offset string `json:"offset"`
268+
}
269+
270+
// InlineQueryResult is the base type that all InlineQuery Results have.
271+
type InlineQueryResult struct {
272+
Type string `json:"type"` // required
273+
ID string `json:"id"` // required
274+
}
275+
276+
// InlineQueryResultArticle is an inline query response article.
277+
type InlineQueryResultArticle struct {
278+
InlineQueryResult
279+
Title string `json:"title"` // required
280+
MessageText string `json:"message_text"` // required
281+
ParseMode string `json:"parse_mode"` // required
282+
DisableWebPagePreview bool `json:"disable_web_page_preview"`
283+
URL string `json:"url"`
284+
HideURL bool `json:"hide_url"`
285+
Description string `json:"description"`
286+
ThumbURL string `json:"thumb_url"`
287+
ThumbWidth int `json:"thumb_width"`
288+
ThumbHeight int `json:"thumb_height"`
289+
}
290+
291+
// InlineQueryResultPhoto is an inline query response photo.
292+
type InlineQueryResultPhoto struct {
293+
InlineQueryResult
294+
URL string `json:"photo_url"` // required
295+
MimeType string `json:"mime_type"`
296+
Width int `json:"photo_width"`
297+
Height int `json:"photo_height"`
298+
ThumbURL string `json:"thumb_url"`
299+
Title string `json:"title"`
300+
Description string `json:"description"`
301+
Caption string `json:"caption"`
302+
MessageText string `json:"message_text"`
303+
ParseMode string `json:"parse_mode"`
304+
DisableWebPagePreview bool `json:"disable_web_page_preview"`
305+
}
306+
307+
// InlineQueryResultGIF is an inline query response GIF.
308+
type InlineQueryResultGIF struct {
309+
InlineQueryResult
310+
URL string `json:"gif_url"` // required
311+
Width int `json:"gif_width"`
312+
Height int `json:"gif_height"`
313+
ThumbURL string `json:"thumb_url"`
314+
Title string `json:"title"`
315+
Caption string `json:"caption"`
316+
MessageText string `json:"message_text"`
317+
ParseMode string `json:"parse_mode"`
318+
DisableWebPagePreview bool `json:"disable_web_page_preview"`
319+
}
320+
321+
// InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
322+
type InlineQueryResultMPEG4GIF struct {
323+
InlineQueryResult
324+
URL string `json:"mpeg4_url"` // required
325+
Width int `json:"mpeg4_width"`
326+
Height int `json:"mpeg4_height"`
327+
ThumbURL string `json:"thumb_url"`
328+
Title string `json:"title"`
329+
Caption string `json:"caption"`
330+
MessageText string `json:"message_text"`
331+
ParseMode string `json:"parse_mode"`
332+
DisableWebPagePreview bool `json:"disable_web_page_preview"`
333+
}
334+
335+
// InlineQueryResultVideo is an inline query response video.
336+
type InlineQueryResultVideo struct {
337+
InlineQueryResult
338+
URL string `json:"video_url"` // required
339+
MimeType string `json:"mime_type"` // required
340+
MessageText string `json:"message_text"` // required
341+
ParseMode string `json:"parse_mode"`
342+
DisableWebPagePreview bool `json:"disable_web_page_preview"`
343+
Width int `json:"video_width"`
344+
Height int `json:"video_height"`
345+
ThumbURL string `json:"thumb_url"`
346+
Title string `json:"title"`
347+
Description string `json:"description"`
348+
}

0 commit comments

Comments
 (0)