| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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
- WORKWX_NAME = "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 WORKWX_NAME
- }
- func (c *workwx) Send(bp *beaconfire.BeaconParam) 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
- }
|