forked from gotd/botapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline.go
More file actions
57 lines (48 loc) · 1.62 KB
/
Copy pathinline.go
File metadata and controls
57 lines (48 loc) · 1.62 KB
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
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"strings"
glog "github.com/gotd/log"
"github.com/gotd/botapi"
)
// registerInline wires inline mode (enable it in @BotFather first). Type
// "@yourbot some text" in any chat and the bot offers transformed variants;
// picking one fires OnChosenInlineResult.
func registerInline(bot *botapi.Bot) {
bot.OnInlineQuery(func(c *botapi.Context) error {
q := strings.TrimSpace(c.Update.InlineQuery.Query)
if q == "" {
// An empty answer clears the inline results list.
return c.AnswerInline(nil)
}
results := []botapi.InlineQueryResult{
article("upper", "UPPERCASE", strings.ToUpper(q)),
article("lower", "lowercase", strings.ToLower(q)),
article("reverse", "Reversed", reverse(q)),
}
// IsPersonal + a short cache time keep results per-user and fresh.
return c.AnswerInline(results,
botapi.WithInlineCacheTime(1),
botapi.WithInlinePersonal(),
)
})
bot.OnChosenInlineResult(func(c *botapi.Context) error {
chosen := c.Update.ChosenInlineResult
glog.For(c.Bot.Logger()).Info(c, "inline result chosen",
glog.String("result_id", chosen.ResultID),
glog.String("query", chosen.Query),
)
return nil
})
}
// article builds a text-content inline result with an attached inline keyboard.
func article(id, title, text string) botapi.InlineQueryResult {
return &botapi.InlineQueryResultArticle{
ID: id,
Title: title,
Description: text,
InputMessageContent: &botapi.InputTextMessageContent{MessageText: text},
ReplyMarkup: botapi.InlineKeyboard(botapi.InlineRow(
botapi.InlineButtonURL("gotd/td", "https://github.com/gotd/td"),
)),
}
}