-
Notifications
You must be signed in to change notification settings - Fork 0
/
getsticker.go
112 lines (98 loc) · 2.75 KB
/
getsticker.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
103
104
105
106
107
108
109
110
111
112
package getsticker
import (
"bytes"
"image/png"
"io/ioutil"
"net/http"
"time"
"github.com/capric98/kusoDD_bot/core"
"golang.org/x/image/webp"
)
var (
ack = make(chan bool, 1)
client *http.Client
python, script string
)
// New :)
func New(settings map[string]interface{}) (func(core.Message) <-chan bool, time.Duration, []core.Opt) {
if settings["python"] != nil {
python = settings["python"].(string)
}
if settings["script"] != nil {
script = settings["script"].(string)
}
checkTmp()
client = &http.Client{
Timeout: 5 * time.Second,
}
return func(msg core.Message) <-chan bool {
return handle(msg)
}, 5 * time.Second, nil
}
func handle(msg core.Message) <-chan bool {
select {
case <-ack:
default:
}
if msg.Message == nil {
ack <- false
return ack
}
go func() {
if msg.Message.IsCommand() && msg.Message.Command() == "getsticker" {
ack <- true
sticker := msg.Message.Sticker
if sticker == nil && msg.Message.ReplyToMessage != nil {
sticker = msg.Message.ReplyToMessage.Sticker
}
if sticker == nil {
resp := core.NewMessage(msg.Message.Chat.ID, "未找到sticker,请对一个sticker回复该指令")
resp.ReplyToMessageID = msg.Message.MessageID
if _, e := msg.Bot.Send(resp); e != nil {
msg.Bot.Printf("%6s - getsticker failed to send response: \"%v\".\n", "warn", e)
}
} else {
go func() { _, _ = msg.Bot.Send(core.NewChatAction(msg.Message.Chat.ID, "UPLOAD_DOCUMENT")) }()
filename := sticker.SetName + "-" + sticker.FileID + ".png"
slink, e := msg.Bot.GetFileDirectURL(sticker.FileID)
if e != nil {
msg.Bot.Printf("%6s - getsticker failed to get file link: \"%v\".\n", "warn", e)
return
}
sresp, e := client.Get(slink)
if e != nil {
msg.Bot.Printf("%6s - getsticker failed to download file: \"%v\".\n", "warn", e)
return
}
ibody, e := ioutil.ReadAll(sresp.Body)
sresp.Body.Close()
if e != nil {
msg.Bot.Printf("%6s - getsticker failed to download file: \"%v\".\n", "warn", e)
return
}
image, e := webp.Decode(bytes.NewReader(ibody))
if e != nil {
//msg.Bot.Printf("%6s - getsticker failed to decode sticker file: \"%v\".\n", "warn", e)
decodeTGS(ibody, sticker.SetName+"-"+sticker.FileID, msg)
return
}
var buf bytes.Buffer
e = png.Encode(&buf, image)
if e != nil {
msg.Bot.Printf("%6s - getsticker failed to encode sticker file: \"%v\".\n", "warn", e)
return
}
resp := core.NewDocumentUpload(
msg.Message.Chat.ID,
core.NewFileBytes(filename, &buf, int64(buf.Len())),
)
if _, e := msg.Bot.Send(resp); e != nil {
msg.Bot.Printf("%6s - getsticker failed to send response: \"%v\".\n", "warn", e)
}
}
} else {
ack <- false
}
}()
return ack
}