-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline_query.go
46 lines (39 loc) · 1.3 KB
/
inline_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"errors"
"fmt"
"strconv"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/BecauseOfProg/BecauseOfProg_Bot/lib"
)
// HandleInlineQuery handles an inline query from a user (here: suggest publications to send into the channel)
func HandleInlineQuery(bot *telegram.BotAPI, update telegram.Update) error {
result, err := lib.GetPublicationsBySearch(update.InlineQuery.Query)
if err != nil {
return errors.New(lib.Red.Sprintf("‼ Error calling the BecauseOfProg API: %s", err))
}
var results []interface{}
for _, publication := range result.Data {
url, link := publication.FormatLink()
results = append(results, telegram.InlineQueryResultArticle{
Type: "article",
InputMessageContent: telegram.InputTextMessageContent{
Text: link,
ParseMode: "Markdown",
},
ID: strconv.Itoa(publication.Timestamp),
Title: publication.Title,
Description: fmt.Sprintf("%s - %s", publication.Author.Name, publication.Description),
URL: url,
ThumbURL: publication.Banner,
})
}
_, err = bot.AnswerInlineQuery(telegram.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
Results: results,
})
if err != nil {
err = errors.New(lib.Red.Sprintf("‼ Error sending inline query result: %s", err))
}
return err
}