controller.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package wechat
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/logs"
  7. "net/http"
  8. "nginx-ui/server/base"
  9. "nginx-ui/server/modules/settings"
  10. )
  11. type Controller struct {
  12. base.Controller
  13. }
  14. // WebhookFromGitlab Webhook 企业微信机器人回调
  15. func (c *Controller) WebhookFromGitlab() {
  16. key := c.GetQuery("key")
  17. setting, err := settings.GetSetting("WECHAT_WEB_HOOK_" + key)
  18. if err != nil {
  19. c.ErrorJson(err)
  20. return
  21. }
  22. url := setting.ConfigValue
  23. r := GitlabWebHook{}
  24. if !c.ReadBody(&r) {
  25. return
  26. }
  27. content := fmt.Sprintf("%s %s了%d个commits到%s仓库的%s分支\n提交内容:\n", r.UserName, r.ObjectKind, r.TotalCommitsCount, r.Repository.Name, r.Ref)
  28. for _, commit := range r.Commits {
  29. content = fmt.Sprintf("%s[%s]:%s\n", content, commit.Id[:8], commit.Message)
  30. }
  31. body := &WechatWebHook{
  32. MsgType: "markdown",
  33. Markdown: WechatMarkdown{
  34. Content: content,
  35. },
  36. }
  37. b, _ := json.Marshal(body)
  38. res, err := http.Post(url, "application/json;charset=utf-8", bytes.NewBuffer(b))
  39. if err != nil {
  40. c.ErrorJson(err)
  41. return
  42. }
  43. logs.Warn("error post: %s", url, res)
  44. c.Json()
  45. }