forked from lenye/pmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lenye
committed
Dec 23, 2022
1 parent
64170db
commit f2c4275
Showing
10 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
* 企业微信群机器人消息 | ||
* 钉钉自定义机器人消息 | ||
* 飞书自定义机器人消息 | ||
* Slack机器人消息 | ||
|
||
### 微信 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |