This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
helpers.go
212 lines (188 loc) · 4.53 KB
/
helpers.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package qqbotapi
import (
"encoding/base64"
"errors"
"fmt"
"github.com/catsworld/qq-bot-api/cqcode"
"io"
"io/ioutil"
"net/url"
"reflect"
)
// NewMessage creates a new Message.
//
// chatID is where to send it, message is the message.
func NewMessage(chatID int64, chatType string, message interface{}) MessageConfig {
mc := MessageConfig{
BaseChat: BaseChat{
ChatID: chatID,
ChatType: chatType,
},
}
if !reflect.ValueOf(message).IsValid() {
return mc
}
r := reflect.Indirect(reflect.New(reflect.TypeOf(message)))
r.Set(reflect.ValueOf(message))
var sp interface{}
if r.Kind() == reflect.Ptr {
sp = r.Interface()
} else {
sp = r.Addr().Interface()
}
switch v := sp.(type) {
case *Message:
mc.Text = v.CQString()
case *cqcode.Message:
mc.Text = v.CQString()
case cqcode.Media:
mc.Text = cqcode.FormatCQCode(v)
case *string:
mc.Text = *v
default:
mc.Text = fmt.Sprint(message)
}
return mc
}
// NewUpdate gets updates since the last Offset.
//
// offset is the last Update ID to include.
// You likely want to set this to the last Update ID plus 1.
func NewUpdate(offset int) UpdateConfig {
return UpdateConfig{
BaseUpdateConfig: BaseUpdateConfig{
PreloadUserInfo: false,
},
Offset: offset,
Limit: 0,
Timeout: 0,
}
}
// NewWebhook registers a webhook.
func NewWebhook(pattern string) WebhookConfig {
return WebhookConfig{
BaseUpdateConfig: BaseUpdateConfig{
PreloadUserInfo: false,
},
Pattern: pattern,
}
}
const (
cacheEnabled = 1
cacheDisabled = 0
)
// NetResource is a resource located in the Internet.
type NetResource struct {
Cache int `cq:"cache"`
}
// EnableCache enables CQ HTTP's cache feature.
func (r *NetResource) EnableCache() {
r.Cache = cacheEnabled
}
// DisableCache forces CQ HTTP download from the URL instead of using cache.
func (r *NetResource) DisableCache() {
r.Cache = cacheDisabled
}
// NetImage is an image located in the Internet.
type NetImage struct {
*cqcode.Image
*NetResource
}
// NetRecord is a record located in the Internet.
type NetRecord struct {
*cqcode.Record
*NetResource
}
// NewImageBase64 formats an image in base64.
func NewImageBase64(file interface{}) (*cqcode.Image, error) {
fileid, err := NewFileBase64(file)
if err != nil {
return &cqcode.Image{}, err
}
return &cqcode.Image{
FileID: fileid,
}, nil
}
// NewRecordBase64 formats a record in base64.
func NewRecordBase64(file interface{}) (*cqcode.Record, error) {
fileid, err := NewFileBase64(file)
if err != nil {
return &cqcode.Record{}, err
}
return &cqcode.Record{
FileID: fileid,
}, nil
}
// NewFileBase64 formats a file into base64 format.
func NewFileBase64(file interface{}) (string, error) {
switch f := file.(type) {
case string:
data, err := ioutil.ReadFile(f)
if err != nil {
return "", err
}
encodeString := base64.StdEncoding.EncodeToString(data)
return "base64://" + encodeString, nil
case []byte:
encodeString := base64.StdEncoding.EncodeToString(f)
return "base64://" + encodeString, nil
case io.Reader:
data, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
encodeString := base64.StdEncoding.EncodeToString(data)
return "base64://" + encodeString, nil
default:
return "", errors.New("bad file type")
}
}
// NewImageLocal formats an image with the file path,
// this requires CQ HTTP runs in the same host with your bot.
//
// This method is deprecated and will get removed, see #11.
// Please use NewImageWeb instead.
func NewImageLocal(file string) *cqcode.Image {
return &cqcode.Image{
FileID: NewFileLocal(file),
}
}
// NewRecordLocal formats a record with the file path,
// this requires CQ HTTP runs in the same host with your bot.
//
// This method is deprecated and will get removed, see #11.
// Please use NewRecordWeb instead.
func NewRecordLocal(file string) *cqcode.Record {
return &cqcode.Record{
FileID: NewFileLocal(file),
}
}
// NewFileLocal formats a file with the file path, returning the string.
//
// This method is deprecated and will get removed, see #11.
// Please use NewFileWeb instead.
func NewFileLocal(file string) string {
return "file://" + file
}
// NewImageWeb formats an image with the URL.
func NewImageWeb(url *url.URL) *NetImage {
return &NetImage{
Image: &cqcode.Image{
FileID: url.String(),
},
NetResource: &NetResource{
Cache: cacheEnabled,
},
}
}
// NewRecordWeb formats a record with the URL.
func NewRecordWeb(url *url.URL) *NetRecord {
return &NetRecord{
Record: &cqcode.Record{
FileID: url.String(),
},
NetResource: &NetResource{
Cache: cacheEnabled,
},
}
}