-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.go
110 lines (96 loc) · 3.43 KB
/
callback.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
package tim
import (
"net/http"
"sync"
"github.com/labstack/echo/v4"
)
var mutex sync.Mutex
const (
StateStateChange = "State.StateChange"
SnsFriendAdd = "Sns.CallbackFriendAdd"
SnsFriendDelete = "Sns.CallbackFriendDelete"
SnsBlackListAdd = "Sns.CallbackBlackListAdd"
SnsBlackListDelete = "Sns.CallbackBlackListDelete"
C2CBeforeSendMsg = "C2C.CallbackBeforeSendMsg"
C2CAfterSendMsg = "C2C.CallbackAfterSendMsg"
GroupBeforeCreateGroup = "Group.CallbackBeforeCreateGroup"
GroupAfterCreateGroup = "Group.CallbackAfterCreateGroup"
GroupBeforeApplyJoinGroup = "Group.CallbackBeforeApplyJoinGroup"
GroupBeforeInviteJoinGroup = "Group.CallbackBeforeInviteJoinGroup"
GroupAfterNewMemberJoin = "Group.CallbackAfterNewMemberJoin"
GroupAfterMemberExit = "Group.CallbackAfterMemberExit"
GroupBeforeSendMsg = "Group.CallbackBeforeSendMsg"
GroupAfterSendMsg = "Group.CallbackAfterSendMsg"
GroupAfterGroupFull = "Group.CallbackAfterGroupFull"
GroupAfterGroupDestroyed = "Group.CallbackAfterGroupDestroyed"
GroupAfterGroupInfoChanged = "Group.CallbackAfterGroupInfoChanged"
)
type Req struct {
SdkAppid string `query:"SdkAppid"`
CallbackCommand string `query:"CallbackCommand"`
Contenttype string `query:"contenttype"`
ClientIP string `query:"ClientIP"`
OptPlatform string `query:"OptPlatform"`
}
type Response struct {
ActionStatus string `json:"ActionStatus"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
}
func Run(appId string) {
e := echo.New()
e.POST("/imcallback", func(c echo.Context) error {
var req Req
var response Response
req.SdkAppid = c.QueryParam("SdkAppid")
req.CallbackCommand = c.QueryParam("CallbackCommand")
req.OptPlatform = c.QueryParam("OptPlatform")
req.ClientIP = c.QueryParam("ClientIP")
req.Contenttype = c.QueryParam("contenttype")
if appId != req.SdkAppid {
return nil
}
if err := command(req, c); err != nil {
response.ActionStatus = "fail"
response.ErrorCode = 400
response.ErrorInfo = err.Error()
return c.JSON(http.StatusOK, response)
}
response.ActionStatus = "OK"
response.ErrorCode = 0
return c.JSON(http.StatusOK, response)
})
e.Logger.Fatal(e.Start(":80"))
}
func command(req Req, c echo.Context) error {
if cmd, ok := h[req.CallbackCommand]; ok {
return cmd(req, c)
}
return nil
}
func AddCommandFuc(command string, commandFunc CommandFunc) {
mutex.Lock()
defer mutex.Unlock()
h[command] = commandFunc
}
var h = map[string]CommandFunc{
StateStateChange: StateChange,
SnsFriendAdd: FriendAdd,
SnsFriendDelete: FriendDelete,
SnsBlackListAdd: BlackListAdd,
SnsBlackListDelete: BlackListDelete,
C2CBeforeSendMsg: BeforeSendMsg,
C2CAfterSendMsg: AfterSendMsg,
GroupBeforeCreateGroup: BeforeCreateGroup,
GroupAfterCreateGroup: AfterCreateGroup,
GroupBeforeApplyJoinGroup: BeforeApplyJoinGroup,
GroupBeforeInviteJoinGroup: BeforeInviteJoinGroup,
GroupAfterNewMemberJoin: AfterNewMemberJoin,
GroupAfterMemberExit: AfterMemberExit,
GroupBeforeSendMsg: BeforeSendMsg,
GroupAfterSendMsg: GafterSendMsg,
GroupAfterGroupFull: AfterGroupFull,
GroupAfterGroupDestroyed: AfterGroupDestroyed,
GroupAfterGroupInfoChanged: AfterGroupInfoChanged,
}
type CommandFunc func(req Req, c echo.Context) error