package providers import ( "bytes" "encoding/json" "errors" "fmt" "git.wenlab.co/joe/beaconfire" ) // API: // https://work.weixin.qq.com/api/doc/90000/90136/91770 const ( _WORKWX_ENDPOINT = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send" _WORKWX_MSGTYPE_MARKDOWN = "markdown" // this type would be lcompatible with others easily BEACON_NAME_WORKWX = "workwx" ) type OptionsWorkwx struct { Key string // robot key To []string // mention_list } type workwx struct { opt *OptionsWorkwx } var _ beaconfire.BeaconFire = &workwx{} func NewWorkWx(opt *OptionsWorkwx) *workwx { return &workwx{ opt: opt, } } func (c *workwx) Name() string { return BEACON_NAME_WORKWX } func (c *workwx) Send(bp *beaconfire.BeaconMessage) error { msgtype := _WORKWX_MSGTYPE_MARKDOWN // modify content content := bp.Content if msgtype == _WORKWX_MSGTYPE_MARKDOWN { content = bp.FormatMarkdown() } url := fmt.Sprintf("%s?key=%s", _WORKWX_ENDPOINT, c.opt.Key) values := map[string]interface{}{ "msgtype": msgtype, msgtype: map[string]interface{}{ "content": content, "mentioned_list": c.opt.To, }, } jsonBuffer, err := json.Marshal(values) if err != nil { return err } data, err := beaconfire.PostJson(url, jsonBuffer) if err != nil { return err } var res struct { ErrorCode int `json:"errorcode"` ErrMsg string `json:"errmsg"` } err = json.NewDecoder(bytes.NewBuffer(data)).Decode(&res) if err != nil { return err } if res.ErrorCode != 0 { return errors.New(res.ErrMsg) } /** returns: { "errcode":0, "errmsg":"ok" } */ return nil }