-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
82 lines (72 loc) · 1.4 KB
/
main.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
package main
import (
"github.com/Sadzeih/bttv-telegram/pkg/emote"
"log"
"math"
"os"
"strconv"
tb "gopkg.in/tucnak/telebot.v2"
)
func main() {
b, err := tb.NewBot(tb.Settings{
// TODO: better config handling (with viper)
Token: os.Getenv("TOKEN"),
Poller: &tb.Webhook{
Listen: os.Getenv("LISTEN_ADDR"),
Endpoint: &tb.WebhookEndpoint{
PublicURL: os.Getenv("PUBLIC_URL"),
},
},
})
if err != nil {
log.Fatal(err)
}
b.Handle(tb.OnQuery, func(q *tb.Query) {
var emotes []emote.Emote
if q.Text == "" {
return
}
emotes, err := getEmotes(q.Text)
if err != nil {
log.Println(err)
return
}
emotes = emotes[:int(math.Min(50, float64(len(emotes))))]
results := make(tb.Results, len(emotes))
for i, e := range emotes {
var result tb.Result
switch e.Type {
case "png":
result = &tb.PhotoResult{
URL: e.URL,
ThumbURL: e.URL,
Width: e.Width,
Height: e.Height,
}
case "gif", "webp":
result = &tb.GifResult{
URL: e.URL,
ThumbURL: e.URL,
ThumbMIME: "image/gif",
Width: e.Width,
Height: e.Height,
}
default:
result = nil
}
if result == nil {
continue
}
result.SetResultID(strconv.Itoa(i + 1))
results[i] = result
}
err = b.Answer(q, &tb.QueryResponse{
Results: results,
CacheTime: 0,
})
if err != nil {
log.Println(err)
}
})
b.Start()
}