-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
57 lines (50 loc) · 1.11 KB
/
Copy pathqueue.go
File metadata and controls
57 lines (50 loc) · 1.11 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 (
"context"
"log"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
type InlineQueryJob struct {
ID string
Query string
From models.User
}
func processInqueryQueue(ctx context.Context, b *bot.Bot) {
for {
time.Sleep(50 * time.Millisecond)
var batch []InlineQueryJob
drain:
for {
select {
case job := <-inqueryQueue:
batch = append(batch, job)
default:
break drain
}
}
if len(batch) == 0 {
continue
}
groups := make(map[string][]InlineQueryJob)
for _, job := range batch {
groups[job.Query] = append(groups[job.Query], job)
}
for _, jobs := range groups {
go func(ctx context.Context, b *bot.Bot, jobs []InlineQueryJob) {
for _, job := range jobs {
_, err := b.AnswerInlineQuery(ctx, &bot.AnswerInlineQueryParams{
InlineQueryID: job.ID,
Results: buildRpArticles(job.Query, job.From),
CacheTime: 1,
})
if err != nil {
log.Printf("failed to answer inline query %s: %s", job.ID, err)
}
}
}(ctx, b, jobs)
}
time.Sleep(950 * time.Millisecond)
}
}