Skip to content

Commit

Permalink
feat: slack bot
Browse files Browse the repository at this point in the history
  • Loading branch information
lenye committed Dec 23, 2022
1 parent 64170db commit f2c4275
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* 企业微信群机器人消息
* 钉钉自定义机器人消息
* 飞书自定义机器人消息
* Slack机器人消息

### 微信

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ func init() {
rootCmd.AddCommand(weiXinWorkCmd)
rootCmd.AddCommand(dingTalkCmd)
rootCmd.AddCommand(feiShuCmd)
rootCmd.AddCommand(slackCmd)
}
34 changes: 34 additions & 0 deletions cmd/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"

"github.com/lenye/pmsg/pkg/flags"
)

// slackCmd slack
var slackCmd = &cobra.Command{
Use: "slack",
Aliases: []string{"sk"},
Short: "slack",
}

func init() {
slackCmd.PersistentFlags().StringVarP(&userAgent, flags.UserAgent, "a", "", "http user agent")

slackCmd.AddCommand(slackBotCmd)
}
48 changes: 48 additions & 0 deletions cmd/slack_bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/lenye/pmsg/pkg/flags"
"github.com/lenye/pmsg/pkg/slack/bot"
)

// slackBotCmd slack bot
var slackBotCmd = &cobra.Command{
Use: "bot",
Short: "publish slack bot message",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
arg := bot.CmdSendParams{
UserAgent: userAgent,
URL: url,
Data: args[0],
}
if err := bot.CmdSend(&arg); err != nil {
fmt.Fprintln(os.Stderr, err)
}
},
Example: "pmsg slack bot --url webhook_url '{\"text\": \"Hello, World!\"}'",
}

func init() {
slackBotCmd.Flags().StringVar(&url, flags.Url, "", "slack webhook url")
slackBotCmd.MarkFlagRequired(flags.Url)
}
4 changes: 4 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

* [自定义机器人消息](feishu/bot_message.md)

### Slack

* [机器人消息](slack/bot_message.md)

## 微信

* [获取接口调用凭证(公众号、小程序)](weixin/access_token.md)access_token
Expand Down
25 changes: 25 additions & 0 deletions docs/slack/bot_message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### 推送slack机器人消息

命令参数说明

```text
$ pmsg slack bot -h
-a, --user_agent string http user agent
--url string slack webhook url
args 参数:消息内容
```

样例

linux

```shell
$ pmsg slack bot --url webhook_url '{"text": "Hello, World!"}'

ok
```

官方开发文档 [推送slack机器人消息](https://api.slack.com/messaging/webhooks)
28 changes: 28 additions & 0 deletions pkg/slack/bot/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bot

import (
"github.com/lenye/pmsg/pkg/slack/client"
)

// Send 发送消息
func Send(webhookUrl, body string) error {
_, err := client.PostJSON(webhookUrl, body)
if err != nil {
return err
}
return nil
}
41 changes: 41 additions & 0 deletions pkg/slack/bot/message_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bot

import (
"fmt"

"github.com/lenye/pmsg/pkg/http/client"
"github.com/lenye/pmsg/pkg/slack"
)

type CmdSendParams struct {
UserAgent string
URL string
Data string
}

// CmdSend 发送消息
func CmdSend(arg *CmdSendParams) error {

client.SetUserAgent(arg.UserAgent)

if err := Send(arg.URL, arg.Data); err != nil {
return err
}
fmt.Println(slack.MessageOK)

return nil
}
44 changes: 44 additions & 0 deletions pkg/slack/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"fmt"
"net/http"
"strings"

httpClient "github.com/lenye/pmsg/pkg/http/client"
"github.com/lenye/pmsg/pkg/slack"
)

// PostJSON http post json
func PostJSON(url, reqBody string) (http.Header, error) {
resp, err := httpClient.Post(url, httpClient.HdrValContentTypeJson, strings.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("%w; %s %s, %v", httpClient.ErrRequest, http.MethodPost, url, err)
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusTooManyRequests {
return resp.Header, fmt.Errorf("%w; rate limit exceeded, retry after %s second", slack.ErrRequest, resp.Header.Get("Retry-After"))
}

// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
if resp.StatusCode != http.StatusOK {
return resp.Header, fmt.Errorf("%w; server error: %s", slack.ErrRequest, resp.Status)
}

return resp.Header, nil
}
25 changes: 25 additions & 0 deletions pkg/slack/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2022 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package slack

import (
"errors"
)

const (
MessageOK = "ok"
)

var ErrRequest = errors.New("slack request error")

0 comments on commit f2c4275

Please sign in to comment.