Skip to content

Commit

Permalink
Merge pull request #2 from lddsb/fix/at-mobiles
Browse files Browse the repository at this point in the history
fix: at mobiles not working bug
  • Loading branch information
lddsb authored Jul 2, 2021
2 parents fdc17f4 + 58d8ae2 commit 19a734f
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -66,8 +65,8 @@ type PayLoad struct {

// WebHook `web hook base config`
type WebHook struct {
AccessToken string `json:"accessToken"`
APIURL string `json:"apiUrl"`
accessToken string
apiUrl string
Secret string
}

Expand All @@ -80,26 +79,23 @@ type Response struct {
// NewWebHook `new a WebHook`
func NewWebHook(accessToken string) *WebHook {
baseAPI := "https://oapi.dingtalk.com/robot/send"
return &WebHook{AccessToken: accessToken, APIURL: baseAPI}
return &WebHook{accessToken: accessToken, apiUrl: baseAPI}
}

// reset api URL
func (w *WebHook) resetAPIURL() {
w.APIURL = "https://oapi.dingtalk.com/robot/send"
func (w *WebHook) resetApiUrl() {
w.apiUrl = "https://oapi.dingtalk.com/robot/send"
}

var regStr = `(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}`
var regPattern = regexp.MustCompile(regStr)

// real send request to api
func (w *WebHook) sendPayload(payload *PayLoad) error {
params := make(map[string]string)
var apiURL string
if strings.Contains(w.AccessToken, w.APIURL) {
apiURL = w.AccessToken
if strings.Contains(w.accessToken, w.apiUrl) {
apiURL = w.accessToken
} else {
params["access_token"] = w.AccessToken
apiURL = w.APIURL
params["access_token"] = w.accessToken
apiURL = w.apiUrl
}

if w.Secret != "" {
Expand Down Expand Up @@ -180,16 +176,6 @@ func (w *WebHook) SendLinkMsg(title, content, picURL, msgURL string) error {

// SendMarkdownMsg `send a markdown msg`
func (w *WebHook) SendMarkdownMsg(title, content string, isAtAll bool, mobiles ...string) error {
firstLine := false
for _, mobile := range mobiles {
if regPattern.MatchString(mobile) {
if false == firstLine {
content += "#####"
}
content += " @" + mobile
firstLine = true
}
}
// send request
return w.sendPayload(&PayLoad{
MsgType: "markdown",
Expand Down

0 comments on commit 19a734f

Please sign in to comment.