| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package providers
- import (
- "encoding/json"
- "errors"
- "fmt"
- "git.wenlab.co/joe/beaconfire"
- )
- const BEACON_NAME_MATTERMOST = "mattermost"
- type OptionsMattermost struct {
- Url string
- HookId string
- IconUrl string
- BotName string
- To []string
- }
- type mattermost struct {
- opt *OptionsMattermost
- }
- var _ beaconfire.BeaconFire = &mattermost{}
- func NewMattermost(opt *OptionsMattermost) *mattermost {
- return &mattermost{
- opt: opt,
- }
- }
- func (c mattermost) Name() string {
- return BEACON_NAME_MATTERMOST
- }
- type message struct {
- Channel string `json:"channel,omitempty"`
- Username string `json:"username,omitempty"`
- IconUrl string `json:"icon_url,omitempty"`
- Text string `json:"text"`
- }
- func (c *mattermost) Send(bp *beaconfire.BeaconMessage) error {
- if len(c.opt.To) <= 0 {
- return beaconfire.ErrNoReceiver
- }
- u := fmt.Sprintf("%s/hooks/%s", c.opt.Url, c.opt.HookId)
- for _, channel := range c.opt.To {
- m := message{
- Channel: channel,
- Username: c.opt.BotName,
- IconUrl: c.opt.IconUrl,
- Text: bp.Content,
- }
- msg, err := json.Marshal(m)
- if err != nil {
- return err
- }
- res, err := beaconfire.PostJson(u, msg)
- if err != nil {
- return err
- }
- var r struct {
- Ok bool `json:"ok"`
- ErrorCode int `json:"error_code"`
- Description string `json:"description"`
- }
- err = json.Unmarshal(res, &r)
- if nil != err {
- return err
- }
- if !r.Ok {
- return errors.New(r.Description)
- }
- }
- return nil
- }
|