Add SMSFactor

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

View File

@ -125,6 +125,7 @@ type ConfYaml struct {
Android SectionAndroid `yaml:"android"`
Huawei SectionHuawei `yaml:"huawei"`
Ios SectionIos `yaml:"ios"`
SMSFactor SectionSMSFactor `yaml:"smsfactor"`
Queue SectionQueue `yaml:"queue"`
Log SectionLog `yaml:"log"`
Stat SectionStat `yaml:"stat"`
@ -201,6 +202,13 @@ type SectionIos struct {
TeamID string `yaml:"team_id"`
}
// SectionIos is sub section of config.
type SectionSMSFactor struct {
Enabled bool `yaml:"enabled"`
APIKey string `yaml:"apikey"`
MaxRetry int `yaml:"max_retry"`
}
// SectionLog is sub section of config.
type SectionLog struct {
Format string `yaml:"format"`
@ -392,6 +400,11 @@ func LoadConf(confPath ...string) (*ConfYaml, error) {
conf.Ios.KeyID = viper.GetString("ios.key_id")
conf.Ios.TeamID = viper.GetString("ios.team_id")
// SMSFactor
conf.SMSFactor.Enabled = viper.GetBool("smsfactor.enabled")
conf.SMSFactor.APIKey = viper.GetString("smsfactor.apikey")
conf.SMSFactor.MaxRetry = viper.GetInt("smsfactor.max_retry")
// log
conf.Log.Format = viper.GetString("log.format")
conf.Log.AccessLog = viper.GetString("log.access_log")

View File

@ -7,6 +7,10 @@ const (
PlatFormAndroid
// PlatFormHuawei constant is 3 for Huawei
PlatFormHuawei
// PlatformUnifiedPush constant is 4 for UnifiedPush
PlatformUnifiedPush
// PlatformSMSFactor constant is 5 for SMSFactor
PlatformSMSFactor
)
const (

View File

@ -21,6 +21,18 @@ const (
// HuaweiErrorKey is key name for huawei error count of storage
HuaweiErrorKey = "gorush-huawei-error-count"
// UnifiedPushSuccessKey is key name for unifiedpush success count of storage
UnifiedPushSuccessKey = "gorush-unifiedpush-success-count"
// UnifiedPushErrorKey is key name for unifiedpush error count of storage
UnifiedPushErrorKey = "gorush-unifiedpush-error-count"
// SMSFactirSuccessKey is key name for smsfactor success count of storage
SMSFactorSuccessKey = "gorush-smsfactor-success-count"
// SMSfactorErrorKey is key name for smsfactor error count of storage
SMSFactorErrorKey = "gorush-smsfactor-error-count"
)
// Storage interface

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
}