-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinlinequery.go
102 lines (87 loc) · 2.53 KB
/
inlinequery.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"log"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func handleInlineQuery(bot *tgbotapi.BotAPI, update tgbotapi.Update, st Store) error {
polls, err := st.GetPollsByUser(update.InlineQuery.From.ID)
if err != nil {
return fmt.Errorf("could not get polls for user: %v", err)
}
if len(polls) > maxPollsInlineQuery {
polls = polls[0 : maxPollsInlineQuery-1]
}
results := make([]interface{}, len(polls))
for i, p := range polls {
log.Println(p)
article := tgbotapi.NewInlineQueryResultArticleHTML(strconv.Itoa(p.ID), p.Question, buildPollListing(p, st))
if len(p.Options) > 0 {
article.ReplyMarkup = buildPollMarkup(p)
}
article.Description = locInlineInsertPoll
results[i] = article
}
inlineConfig := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
Results: results,
IsPersonal: true,
CacheTime: 0,
SwitchPMText: locCreateNewPoll,
SwitchPMParameter: createNewPollQuery,
}
_, err = bot.AnswerInlineQuery(inlineConfig)
if err != nil {
return fmt.Errorf("could not answer inline query: %v", err)
}
return nil
}
func handleInlineQueryAdmin(bot *tgbotapi.BotAPI, update tgbotapi.Update, st Store) error {
splits := strings.Split(update.InlineQuery.Query, ":")
if len(splits) < 1 {
return fmt.Errorf("Could not convert query to pollid")
}
active := false
if len(splits) == 2 {
if splits[1] == "p" {
active = true
}
}
pollid, err := strconv.Atoi(splits[0])
if err != nil {
return fmt.Errorf("Could not convert query to pollid: %v", err)
}
p, err := st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get polls for user: %v", err)
}
polls := []*poll{p}
if len(polls) > maxPollsInlineQuery {
polls = polls[0 : maxPollsInlineQuery-1]
}
results := make([]interface{}, len(polls))
for i, p := range polls {
log.Println(p)
article := tgbotapi.NewInlineQueryResultArticleHTML(strconv.Itoa(p.ID), p.Question, buildPollListing(p, st))
if len(p.Options) > 0 && active {
article.ReplyMarkup = buildPollMarkup(p)
}
article.Description = locInlineInsertPoll
results[i] = article
}
inlineConfig := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
Results: results,
IsPersonal: true,
CacheTime: 0,
SwitchPMText: locCreateNewPoll,
SwitchPMParameter: createNewPollQuery,
}
_, err = bot.AnswerInlineQuery(inlineConfig)
if err != nil {
return fmt.Errorf("could not answer inline query: %v", err)
}
return nil
}