-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.go
137 lines (124 loc) · 3.04 KB
/
messages.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
package vkutil
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"strings"
"github.com/zhuharev/vk"
)
func (api *Api) MessagesGetConversations() error {
resp, err := api.VkAPI.Request("messages.getConversations")
log.Printf("%s", resp)
return err
}
func (api *Api) MessagesGet(params ...url.Values) ([]Message, error) {
rparams := url.Values{}
if len(params) == 1 {
rparams = params[0]
}
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_GET, rparams)
if err != nil {
return nil, err
}
return ParseMessagesResponse(resp)
}
func (api *Api) MessagesSend(uid int, message string,
params ...url.Values) (int, error) {
rparams := url.Values{}
if len(params) == 1 {
rparams = params[0]
}
if uid != 0 {
rparams.Set("user_id", fmt.Sprint(uid))
}
rparams.Set("message", message)
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_SEND, rparams)
if err != nil {
return 0, err
}
var r ResponseInt
err = json.Unmarshal(resp, &r)
if err != nil {
return 0, err
}
if r.Error.Code != 0 {
return 0, errors.New(r.Error.Msg)
}
return r.Response, nil
}
func (api *Api) MessagesGetDialogs(params ...url.Values) ([]Dialog, error) {
rparams := url.Values{}
if len(params) == 1 {
rparams = params[0]
}
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_GET_DIALOGS, rparams)
if err != nil {
return nil, err
}
return ParseDialogsResponse(resp)
}
func (api *Api) MessagesMarkAsRead(messageId int, messages ...int) error {
var ids = []int{messageId}
if len(messages) > 0 {
ids = append(ids, messages...)
}
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_MARK_AS_READ, url.Values{
"message_ids": {strings.Join(arrIntToStr(ids), ",")},
})
if err != nil {
return err
}
var r ResponseInt
err = json.Unmarshal(resp, &r)
if err != nil {
return err
}
if r.Error.Code != 0 {
return errors.New(r.Error.Msg)
}
return nil
}
func (api *Api) MessagesGetHistory(udserId int, params ...url.Values) ([]Message, error) {
rparams := url.Values{}
if len(params) == 1 {
rparams = params[0]
}
rparams.Set("user_id", fmt.Sprint(udserId))
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_GET_HISTORY, rparams)
if err != nil {
return nil, err
}
return ParseMessagesResponse(resp)
}
func (api *Api) MessagesSetActivity(udserID int, params ...url.Values) error {
rparams := url.Values{}
if len(params) == 1 {
rparams = params[0]
}
rparams.Set("user_id", fmt.Sprint(udserID))
rparams.Set("type", "typing")
resp, err := api.VkApi.Request(vk.METHOD_MESSAGES_SET_ACTIVITY, rparams)
if err != nil {
return err
}
_, err = ParseIntResponse(resp)
return err
}
func (api *Api) IsMessagesFromGroupAllowed(ctx context.Context, groupID, userID int) (bool, error) {
rparams := url.Values{}
rparams.Set("group_id", toString(groupID))
rparams.Set("user_id", toString(userID))
var res struct {
Response struct {
IsAllowed int `json:"is_allowed"`
}
}
err := api.VkApi.RequestTypedContext(ctx, vk.MethodMessageFromGroupAllowed, rparams, &res)
if err != nil {
return false, err
}
return res.Response.IsAllowed == 1, nil
}