Add SMSFactor

This commit is contained in:
2023-03-13 00:26:36 +01:00
parent 85fe50f3b4
commit 65bcc8b451
5 changed files with 104 additions and 9 deletions

View File

@@ -232,6 +232,12 @@ func CheckPushConf(cfg *config.ConfYaml) error {
}
}
if cfg.SMSFactor.Enabled {
if cfg.SMSFactor.APIKey == "" {
return errors.New("missing smsfactor api key")
}
}
return nil
}
@@ -251,6 +257,8 @@ func SendNotification(req qcore.QueuedMessage, cfg *config.ConfYaml) (resp *Resp
resp, err = PushToAndroid(v, cfg)
case core.PlatFormHuawei:
resp, err = PushToHuawei(v, cfg)
case core.PlatformSMSFactor:
resp, err = PushToSMSFactor(v, cfg)
}
if cfg.Core.FeedbackURL != "" {

View File

@@ -0,0 +1,58 @@
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
}