11package server
22
33import (
4+ "fmt"
45 "time"
56
67 "github.com/gorilla/websocket"
1415 startTime time.Time
1516)
1617
18+ var buildinActions []string
19+
1720func init () {
1821 startTime = time .Now ()
1922
@@ -25,15 +28,28 @@ func init() {
2528 actions ["sub" ] = subscribeEventAction
2629 actions ["lsunsub" ] = listUnsubscribedEventsAction
2730
31+ actions ["regapi" ] = registerRobotApiAction
32+ actions ["lsapi" ] = listRobotApiAction
33+ actions ["resp" ] = robotApiRespAction
34+
35+ for k := range actions {
36+ buildinActions = append (buildinActions , k )
37+ }
2838}
2939
3040type ProcessInfo struct {
3141 StartedDuration string `json:"started"`
3242}
3343
34- type ActionDescription struct {
35- Name string
36- Desc string
44+ type RobotApiData struct {
45+ Uid int `json:"uid"`
46+ Action string `json:"action"`
47+ Data any `json:"data"`
48+ }
49+
50+ type RobotApiData1 struct {
51+ Bot int `json:"bot"`
52+ Data any `json:"data"`
3753}
3854
3955func listActionsAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
@@ -125,3 +141,95 @@ func subscribeEventAction(wm *WebsocketManager, ws *websocket.Conn, cmd BotCmd,
125141func listUnsubscribedEventsAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
126142 ws .WriteJSON (BotEvent {Event : cmd .Action , Data : wm .unsubscribedEvents [uid ]})
127143}
144+
145+ func registerRobotApiAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
146+ wm .robotActionHubLock .Lock ()
147+ defer wm .robotActionHubLock .Unlock ()
148+ if apis , ok := cmd .Data .([]any ); ok {
149+ if _ , ok := wm .robotActionHub [uid ]; ! ok {
150+ wm .robotActionHub [uid ] = []string {}
151+ }
152+ for _ , api := range apis {
153+ if apiStr , ok := api .(string ); ok {
154+ if slices .Contains (wm .robotActionHub [uid ], apiStr ) {
155+ continue
156+ }
157+ if slices .Contains (buildinActions , apiStr ) {
158+ ws .WriteJSON (BotEvent {Event : "error" , Data : fmt .Sprintf ("%s already exists. skiped" , apiStr )})
159+ continue
160+ }
161+ wm .robotActionHub [uid ] = append (wm .robotActionHub [uid ], apiStr )
162+ actions [apiStr ] = robotApiGenericAction
163+ continue
164+ }
165+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data format" })
166+ return
167+ }
168+ ws .WriteJSON (BotEvent {Event : cmd .Action , Data : "ok" })
169+ return
170+ }
171+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data format" })
172+ }
173+
174+ func listRobotApiAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
175+ ws .WriteJSON (BotEvent {Event : cmd .Action , Data : wm .robotActionHub })
176+ }
177+
178+ func robotApiGenericAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
179+ if data , ok := cmd .Data .(map [string ]any ); ok {
180+ var (
181+ botAny any
182+ botUid float64
183+ )
184+ if botAny , ok = data ["bot" ]; ! ok {
185+ ws .WriteJSON (BotEvent {Event : "error" , Data : "data.bot not found" })
186+ return
187+ }
188+ if botUid , ok = botAny .(float64 ); ! ok {
189+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data.bot format" })
190+ return
191+ }
192+ req := BotEvent {Event : "req" , Data : RobotApiData {Uid : uid , Action : cmd .Action , Data : data ["data" ]}}
193+ wm .userEventChan <- UserBotEvent {Uid : int (botUid ), Event : req }
194+ return
195+ }
196+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data format" })
197+ }
198+
199+ func robotApiRespAction (wm * WebsocketManager , ws * websocket.Conn , cmd BotCmd , uid int ) {
200+ if data , ok := cmd .Data .(map [string ]any ); ok {
201+ var (
202+ clientUidAny any
203+ actionAny any
204+ botDataAny any
205+ clientUid float64
206+ action string
207+ )
208+ if clientUidAny , ok = data ["uid" ]; ! ok {
209+ ws .WriteJSON (BotEvent {Event : "error" , Data : "data.uid not found" })
210+ return
211+ }
212+ if actionAny , ok = data ["action" ]; ! ok {
213+ ws .WriteJSON (BotEvent {Event : "error" , Data : "data.action not found" })
214+ return
215+ }
216+ if botDataAny , ok = data ["data" ]; ! ok {
217+ ws .WriteJSON (BotEvent {Event : "error" , Data : "data.data not found" })
218+ return
219+ }
220+
221+ if clientUid , ok = clientUidAny .(float64 ); ! ok {
222+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data.uid format" })
223+ return
224+ }
225+
226+ if action , ok = actionAny .(string ); ! ok {
227+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data.action format" })
228+ return
229+ }
230+ resp := BotEvent {Event : action , Data : RobotApiData1 {Bot : int (uid ), Data : botDataAny }}
231+ wm .userEventChan <- UserBotEvent {Uid : int (clientUid ), Event : resp }
232+ return
233+ }
234+ ws .WriteJSON (BotEvent {Event : "error" , Data : "invalid data format" })
235+ }
0 commit comments