-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
190 lines (153 loc) · 4.36 KB
/
bot.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
package vkbotgo
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"github.com/google/go-querystring/query"
)
// VkBot interacts with the VK API.
type VkBot struct {
AccessToken string
Buffer int
Version string
Client *http.Client
Conf *Confirmation
}
// NewVkBot creates a new VkBot
// and you can pass a http.Client.
func NewVkBot(accessToken, ver string, client *http.Client) (*VkBot, error) {
vkBot := &VkBot{
AccessToken: accessToken,
Buffer: 128,
Version: ver,
Client: client,
Conf: nil,
}
return vkBot, nil
}
// NewVkBotWithAuth creates a new VkBot with login/pass authorization
// and you can pass a http.Client.
func NewVkBotWithAuth(login, pass, scope, ver string, clientID int64, client *http.Client) (*VkBot, error) {
// We need to remember cookies
tmpClient := *client
tmpClient.Jar, _ = cookiejar.New(nil)
var resp *http.Response
// User sends login init request
requestURL := fmt.Sprintf(AuthAPIurl, clientID, scope, ver)
resp, err := tmpClient.Get(requestURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// User eneters email and pass
actionAddr, postVals, err := parseLoginForm(resp)
if err != nil {
return nil, err
}
postVals.Set("email", login)
postVals.Set("pass", pass)
resp, err = tmpClient.PostForm(actionAddr, *postVals)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Request.URL.Path != "/blank.html" {
queryVals := resp.Request.URL.Query()
if queryVals.Get("act") == "authcheck" {
// User uses Two-factor authentication
// Enter confirmation code for VK message
var confirmString string
_, err := fmt.Scanf("%s", &confirmString)
if err != nil {
return nil, err
}
confirmAction, confirmValues, err := parseLoginForm(resp)
if err != nil {
return nil, err
}
confirmValues.Set("code", confirmString)
resp, err = tmpClient.PostForm(resp.Request.URL.Scheme+"://"+resp.Request.URL.Host+confirmAction, *confirmValues)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Still needs to press confirm
}
// User press "Confirm" button
grantAction, grantValues, err := parseLoginForm(resp)
resp, err = tmpClient.PostForm(grantAction, *grantValues)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Request.URL.Path != "/blank.html" {
return nil, errors.New("login error")
}
}
respArgs, err := url.ParseQuery(resp.Request.URL.Fragment)
if err != nil {
return nil, err
}
return NewVkBot(respArgs.Get("access_token"), ver, client)
}
// Request makes a request to a method with given params.
func (vkBot *VkBot) Request(method string, params url.Values) (*VkResponse, error) {
requestURL := fmt.Sprintf(VkAPIurl, method, vkBot.AccessToken, vkBot.Version)
//Always use only POST request;
response, err := vkBot.Client.PostForm(requestURL, params)
if err != nil {
return &VkResponse{}, err
}
defer response.Body.Close()
vkResp, err := decodeVkResponse(response.Body)
if err != nil {
return &VkResponse{}, err
}
return vkResp, nil
}
// CallMethod calls VK API method with given params(with validation).
//
// For params: CamelCase method name + Params.
// Example: MessagesSendParams (for messages.send)
func (vkBot *VkBot) CallMethod(method string, params APIParameters) (*VkResponse, error) {
err := params.Validate()
if err != nil {
return nil, err
}
vals, err := query.Values(params)
if err != nil {
return nil, err
}
return vkBot.Request(method, vals)
}
// SetConfirmation sets a conformation response
//
// if conformation is not set, you can not use Callback API.
func (vkBot *VkBot) SetConfirmation(conf *Confirmation) {
vkBot.Conf = conf
}
// ListenForEvents registers a http handler for a events.
func (vkBot *VkBot) ListenForEvents() EventsChannel {
events := make(chan *Event, vkBot.Buffer)
http.HandleFunc(vkBot.Conf.Pattern, func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
event := &Event{}
json.Unmarshal(b, event)
if event.Type == ConfirmationType && event.GroupID == vkBot.Conf.GroupID {
_, err := w.Write(vkBot.Conf.ResponseBytes)
if err != nil {
log.Fatalf("can not send conformation string. error: %s", err.Error())
}
return
}
w.Write([]byte(OkString)) //Make byte constant
events <- event
})
return events
}