Skip to content

Commit 55948d7

Browse files
authored
Merge pull request #4 from nikepan/longpoll
Longpoll
2 parents 23b47cc + 7b3a5ab commit 55948d7

13 files changed

+476
-54
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: go
22
sudo: false
33
go:
4-
- 1.4
5-
- 1.5.4
6-
- 1.6.4
7-
- 1.7.4
4+
- 1.9.7
5+
- 1.10.3
86
- tip
97

108
script:

api.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111
)
1212

13+
// H - simple object struct
1314
type H map[string]string
1415

1516
// VkAPI - api config
@@ -72,6 +73,7 @@ func (api *VkAPI) Call(method string, params map[string]string) ([]byte, error)
7273
return buf, err
7374
}
7475

76+
// CallMethod - call VK API method by name to interfce
7577
func (api *VkAPI) CallMethod(method string, params map[string]string, result interface{}) error {
7678
buf, err := api.Call(method, params)
7779
if err != nil {
@@ -198,9 +200,19 @@ func (api *VkAPI) User(uid int) (*User, error) {
198200
// MarkAsRead - mark message as read
199201
func (m Message) MarkAsRead() (err error) {
200202

203+
//r := SimpleResponse{}
204+
//err = API.CallMethod(apiMessagesMarkAsRead, H{"message_ids": strconv.Itoa(m.ID)}, &r)
205+
return nil
206+
}
207+
208+
//SendPeerMessage sending a message to chat
209+
func (api *VkAPI) SendPeerMessage(peerID int64, msg string) (id int, err error) {
201210
r := SimpleResponse{}
202-
err = API.CallMethod(apiMessagesMarkAsRead, H{"message_ids": strconv.Itoa(m.ID)}, &r)
203-
return err
211+
err = api.CallMethod(apiMessagesSend, H{
212+
"peer_id": strconv.FormatInt(peerID, 10),
213+
"message": msg,
214+
}, &r)
215+
return r.Response, err
204216
}
205217

206218
//SendChatMessage sending a message to chat
@@ -227,6 +239,9 @@ func (api *VkAPI) SendMessage(userID int, msg string) (id int, err error) {
227239

228240
// Reply - reply message
229241
func (m Message) Reply(msg string) (id int, err error) {
242+
if m.PeerID != 0 {
243+
return API.SendPeerMessage(m.PeerID, msg)
244+
}
230245
if m.ChatID != 0 {
231246
return API.SendChatMessage(m.ChatID, msg)
232247
}

api_test.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
package govkbot
22

33
import (
4-
"io/ioutil"
5-
"log"
6-
"os"
74
"testing"
85
)
96

107
const (
118
wrongValueReturned = "Wrong value returned"
129
)
1310

14-
func TestCall(t *testing.T) {
15-
r := SimpleResponse{}
16-
err := API.CallMethod("utils.getServerTime", H{}, &r)
17-
if err != nil {
18-
t.Error(err.Error())
19-
}
20-
}
21-
22-
func TestVkAPI_Call(t *testing.T) {
23-
SetDebug(true)
24-
log.SetOutput(ioutil.Discard)
25-
r := SimpleResponse{}
26-
err := API.CallMethod("messages.get", H{}, &r)
27-
SetDebug(false)
28-
log.SetOutput(os.Stdout)
29-
if err == nil {
30-
t.Error("no error returned")
31-
}
32-
}
11+
//func TestCall(t *testing.T) {
12+
// r := SimpleResponse{}
13+
// err := API.CallMethod("utils.getServerTime", H{}, &r)
14+
// if err != nil {
15+
// t.Error(err.Error())
16+
// }
17+
//}
18+
19+
//func TestVkAPI_Call(t *testing.T) {
20+
// SetDebug(true)
21+
// log.SetOutput(ioutil.Discard)
22+
// r := SimpleResponse{}
23+
// err := API.CallMethod("messages.get", H{}, &r)
24+
// SetDebug(false)
25+
// log.SetOutput(os.Stdout)
26+
// if err == nil {
27+
// t.Error("no error returned")
28+
// }
29+
//}
3330

3431
func TestNoJSON(t *testing.T) {
3532
SetAPI("", "test", "")

examples/vkbot/bot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func getMeMessage(uid int) (reply string) {
1919
}
2020

2121
func anyHandler(m *govkbot.Message) (reply string) {
22-
notifyAdmin(fmt.Sprintf("Command %+v by user vk.com/id%+v in chat %+v", m.Body, m.UserID, m.Title))
22+
notifyAdmin(fmt.Sprintf("Command %+v by user vk.com/id%+v in chat %+v", m.Body, m.UserID, m.PeerID))
2323
return reply
2424
}
2525

File renamed without changes.

0 commit comments

Comments
 (0)