| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package providers
- import (
- "encoding/json"
- "fmt"
- "git.wenlab.co/joe/beaconfire"
- )
- // API:
- // https://open.dingtalk.com/document/group/custom-robot-access
- const (
- _DINGTALK_ENDPOINT = "https://oapi.dingtalk.com/robot/send"
- _DINGTALK_DEFAULT_MSGTYPE = "markdown" // this type would be lcompatible with others easily
- BEACON_NAME_DINGTALK = "dingtalk"
- )
- type OptionsDingtalk struct {
- AccessToken string
- To []string // userid
- }
- type dingtalk struct {
- opt *OptionsDingtalk
- }
- var _ beaconfire.BeaconFire = &dingtalk{}
- func NewDingtalk(opt *OptionsDingtalk) *dingtalk {
- return &dingtalk{
- opt: opt,
- }
- }
- func (c dingtalk) Name() string {
- return BEACON_NAME_DINGTALK
- }
- func (c *dingtalk) Send(bp *beaconfire.BeaconMessage) error {
- msgType := _DINGTALK_DEFAULT_MSGTYPE
- url := fmt.Sprintf("%s?access_token=%s", _DINGTALK_ENDPOINT, c.opt.AccessToken)
- content := bp.FormatMarkdown()
- values := map[string]interface{}{
- "msgtype": msgType,
- msgType: map[string]interface{}{
- "content": content,
- },
- "at": map[string]interface{}{
- "atUserIds": c.opt.To,
- "isAtAll": false,
- },
- }
- jsonBuffer, err := json.Marshal(values)
- if err != nil {
- return err
- }
- res, err := beaconfire.PostJson(url, jsonBuffer)
- if err != nil {
- return err
- }
- var r map[string]interface{}
- err = json.Unmarshal(res, &r)
- if err != nil {
- return err
- }
- // DEBUG
- fmt.Printf("dingtalk: %+v\n", r)
- return nil
- }
|