package wechat import ( "bytes" "encoding/json" "fmt" "github.com/astaxie/beego/logs" "net/http" "nginx-ui/server/base" "nginx-ui/server/modules/settings" ) type Controller struct { base.Controller } // WebhookFromGitlab Webhook 企业微信机器人回调 func (c *Controller) WebhookFromGitlab() { key := c.GetQuery("key") setting, err := settings.GetSetting("WECHAT_WEB_HOOK_" + key) if err != nil { c.ErrorJson(err) return } url := setting.ConfigValue r := GitlabWebHook{} if !c.ReadBody(&r) { return } content := fmt.Sprintf("%s %s了%d个commits到%s仓库的%s分支\n提交内容:\n", r.UserName, r.ObjectKind, r.TotalCommitsCount, r.Repository.Name, r.Ref) for _, commit := range r.Commits { content = fmt.Sprintf("%s[%s]:%s\n", content, commit.Id[:8], commit.Message) } body := &WechatWebHook{ MsgType: "markdown", Markdown: WechatMarkdown{ Content: content, }, } b, _ := json.Marshal(body) res, err := http.Post(url, "application/json;charset=utf-8", bytes.NewBuffer(b)) if err != nil { c.ErrorJson(err) return } logs.Warn("error post: %s", url, res) c.Json() }