Browse Source

send webhook

master
linquan 3 weeks ago
parent
commit
cd4880e30e
  1. 11
      api/v1/game/loginUrl.go
  2. 5
      internal/controller/game_login_url.go
  3. 10
      internal/serviceGame/internal/dbinit.go
  4. 49
      internal/serviceGame/loginUrl.go

11
api/v1/game/loginUrl.go

@ -121,3 +121,14 @@ type GetGameLoginUrlRes struct {
g.Meta `mime:"application/json"`
UrlStr []LoginUrlData `json:"serverList"`
}
type ServerUpdateMsgReq struct {
g.Meta `path:"/loginUrl/sendMsg" tags:"server" method:"post" summary:"sendMsg"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Content string `json:"content"`
}
type ServerUpdateMsgRes struct {
g.Meta `mime:"application/json"`
}

5
internal/controller/game_login_url.go

@ -71,3 +71,8 @@ func (c *loginUrlController) UpdateRouter(ctx context.Context, req *game.AddRout
res, err = serviceGame.GameLoginUrl().UpdateRouter(ctx, req)
return res, err
}
func (c *loginUrlController) ServerUpdateMsg(ctx context.Context, req *game.ServerUpdateMsgReq) (res *game.ServerUpdateMsgRes, err error) {
res, err = serviceGame.GameLoginUrl().ServerUpdateMsg(ctx, req)
return
}

10
internal/serviceGame/internal/dbinit.go

@ -33,6 +33,7 @@ var (
RouterServer string
Md5key string
CallbackKey string
Webhook string
)
func init() {
@ -72,6 +73,7 @@ func init() {
CallbackKey = callbackKeyCfg.String()
}
WebHookInit(ctx)
}
func RedisInit(config []map[string]interface{}) {
@ -172,3 +174,11 @@ func InitRouterServer(ctx context.Context) {
log.Println("InitRechargeServer: ", err)
}
}
func WebHookInit(ctx context.Context) {
webhook, err := g.Cfg().Get(ctx, "game.webhook")
if err != nil {
return
}
Webhook = webhook.String()
}

49
internal/serviceGame/loginUrl.go

@ -2,6 +2,7 @@ package serviceGame
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf/v2/encoding/gcharset"
"github.com/gogf/gf/v2/encoding/gjson"
@ -33,6 +34,7 @@ type IGameLoginUrl interface {
GetRouter(ctx context.Context, req *game.GetRouterReq) (res *game.GetRouterRes, err error)
GetServerVersionList(ctx context.Context, req *game.GetServerVersionListReq) (res *game.GetServerVersionListRes, err error)
UpdateRouter(ctx context.Context, req *game.AddRouterReq) (res *game.AddRouterRes, err error)
ServerUpdateMsg(ctx context.Context, req *game.ServerUpdateMsgReq) (res *game.ServerUpdateMsgRes, err error)
}
type gameLoginUrlImpl struct {
@ -233,6 +235,7 @@ func (c *gameLoginUrlImpl) ReConfigServer(ctx context.Context, req *game.ReConfi
service.Email().ConfigInit()
service.TencentSms().ConfigInit()
internal.InitChannel(ctx)
internal.WebHookInit(ctx)
} else {
internal.InitServer(ctx)
internal.InitRechargeServer(ctx)
@ -348,3 +351,49 @@ func (c *gameLoginUrlImpl) GetServerVersionList(ctx context.Context, req *game.G
}
return
}
func (c *gameLoginUrlImpl) ServerUpdateMsg(ctx context.Context, req *game.ServerUpdateMsgReq) (res *game.ServerUpdateMsgRes, err error) {
//url := "https://open.feishu.cn/open-apis/bot/v2/hook/4033c60d-08b4-4d28-8eab-1d4159df98ed"
//url := "https://open.feishu.cn/open-apis/bot/v2/hook/6c2ebace-6815-49a8-9964-eb580ec0a56b"
url := internal.Webhook
if len(url) == 0 {
return nil, errors.New("地址为空!")
}
normal_v2 := map[string]interface{}{"default": "normal", "pc": "normal", "mobile": "heading"}
text_size := map[string]interface{}{"normal_v2": normal_v2}
style := map[string]interface{}{"text_size": text_size}
config := map[string]interface{}{"update_multi": true, "style": style}
elements := []map[string]interface{}{
{
"tag": "markdown",
"content": req.Content,
"text_align": "left",
"text_size": "normal_v2",
"margin": "0px 0px 0px 0px",
},
}
body1 := map[string]interface{}{"direction": "vertical", "padding": "12px 12px 12px 12px", "elements": elements}
title := map[string]interface{}{"tag": "plain_text", "content": req.Title}
subtitle := map[string]interface{}{"tag": "plain_text", "content": req.Subtitle}
header := map[string]interface{}{"title": title, "template": "blue", "subtitle": subtitle, "padding": "12px 12px 12px 12px"}
card := map[string]interface{}{"schema": "2.0", "config": config, "body": body1, "header": header}
body := map[string]interface{}{"msg_type": "interactive", "card": card}
fmt.Println("GetOnlineList - body: ", gjson.MustEncodeString(body))
bytes := g.Client().PostBytes(ctx, url, gjson.MustEncodeString(body))
src := string(bytes)
if g.IsEmpty(src) {
return
}
srcCharset := "UTF-8"
tmp, _ := gcharset.ToUTF8(srcCharset, src)
json, err := gjson.DecodeToJson(tmp)
if err != nil {
return
}
fmt.Println("GetOnlineList - json: ", tmp)
if json.Get("code").Int() != 0 {
err = errors.New(json.Get("msg").String())
}
return
}

Loading…
Cancel
Save