59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
|
package notify
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/appleboy/gorush/config"
|
||
|
"github.com/appleboy/gorush/logx"
|
||
|
)
|
||
|
|
||
|
// InitSMSFactorClient use for initialize SMSFactor Request.
|
||
|
func InitSMSFactorRequest(cfg *config.ConfYaml, token string, notification *PushNotification, recipient string) (*http.Request, error) {
|
||
|
|
||
|
req, _ := http.NewRequest("GET", "https://api.smsfactor.com/send", nil)
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
|
||
|
q := req.URL.Query()
|
||
|
q.Add("text", notification.Message)
|
||
|
q.Add("to", recipient)
|
||
|
q.Add("token", token)
|
||
|
req.URL.RawQuery = q.Encode()
|
||
|
|
||
|
return req, nil
|
||
|
}
|
||
|
|
||
|
func PushToSMSFactor(notification *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
|
||
|
logx.LogAccess.Debug("Start push notification for SMSFactor")
|
||
|
|
||
|
// check message
|
||
|
err = CheckMessage(notification)
|
||
|
if err != nil {
|
||
|
logx.LogError.Error("request error: " + err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
resp = &ResponsePush{}
|
||
|
|
||
|
for _, recipient := range notification.Tokens {
|
||
|
var request *http.Request
|
||
|
|
||
|
if notification.APIKey != "" {
|
||
|
request, err = InitSMSFactorRequest(cfg, notification.APIKey, notification, recipient)
|
||
|
} else {
|
||
|
request, err = InitSMSFactorRequest(cfg, cfg.SMSFactor.APIKey, notification, recipient)
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
logx.LogError.Error(err.Error())
|
||
|
}
|
||
|
|
||
|
client := &http.Client{}
|
||
|
_, err := client.Do(request)
|
||
|
if err != nil {
|
||
|
logx.LogError.Error(err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return resp, nil
|
||
|
}
|