forked from mileusna/viber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
57 lines (48 loc) · 1.56 KB
/
webhook.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
package viber
import "encoding/json"
//
//https://chatapi.viber.com/pa/set_webhook
// {
// "url": "https://my.host.com",
// "event_types": ["delivered", "seen", "failed", "subscribed", "unsubscribed", "conversation_started"]
// }
// WebhookReq request
type WebhookReq struct {
URL string `json:"url"`
EventTypes []string `json:"event_types"`
}
// {
// "status": 0,
// "status_message": "ok",
// "event_types": ["delivered", "seen", "failed", "subscribed", "unsubscribed", "conversation_started"]
// }
//WebhookResp response
type WebhookResp struct {
Status int `json:"status"`
StatusMessage string `json:"status_message"`
EventTypes []string `json:"event_types,omitempty"`
}
// WebhookVerify response
type WebhookVerify struct {
Event string `json:"event"`
Timestamp uint64 `json:"timestamp"`
MessageToken uint64 `json:"message_token"`
}
// SetWebhook for Viber callbacks
// if eventTypes is nil, all callbacks will be set to webhook
// if eventTypes is empty []string mandatory callbacks will be set
// Mandatory callbacks: "message", "subscribed", "unsubscribed"
// All possible callbacks: "message", "subscribed", "unsubscribed", "delivered", "seen", "failed", "conversation_started"
func (v *Viber) SetWebhook(url string, eventTypes []string) (WebhookResp, error) {
var resp WebhookResp
req := WebhookReq{
URL: url,
EventTypes: eventTypes,
}
r, err := v.PostData("https://chatapi.viber.com/pa/set_webhook", req)
if err != nil {
return resp, err
}
err = json.Unmarshal(r, &resp)
return resp, err
}