-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
10 changed files
with
245 additions
and
63 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
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,55 @@ | ||
package apis | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/authgear/authgear-sms-gateway/pkg/lib/infra/sms/accessyou/models" | ||
) | ||
|
||
func SendSMS( | ||
client *http.Client, | ||
baseUrl string, | ||
accountNo string, | ||
user string, | ||
pwd string, | ||
sender string, | ||
to string, | ||
body string, | ||
) ([]byte, *models.SendSMSResponse, error) { | ||
req, _ := http.NewRequest( | ||
"POST", | ||
fmt.Sprintf( | ||
"%v/sendsms.php?accountno=%v&pwd=%v&tid=1&phone=%v&a=%v&user=%v&from=%v", | ||
baseUrl, | ||
accountNo, | ||
pwd, | ||
to, | ||
body, | ||
user, | ||
sender), | ||
nil) | ||
req.Header.Set("Cookie", "dynamic=sms") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
respData, err := io.ReadAll(resp.Body) | ||
|
||
// The response data is in format | ||
// "\ufeff{\"msg_status\":\"100\",\"msg_status_desc\":\"Successfully submitted message. \\u6267\\u884c\\u6210\\u529f\",\"phoneno\":\"852********\",\"msg_id\":852309279}" | ||
|
||
// Remove BOM token from resp json | ||
respData = bytes.Replace(respData, []byte("\ufeff"), []byte(""), -1) | ||
|
||
sendSMSResponse, err := models.ParseSendSMSResponse(respData) | ||
if err != nil { | ||
return respData, nil, err | ||
} | ||
return respData, sendSMSResponse, err | ||
} |
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,23 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/authgear/authgear-sms-gateway/pkg/lib/type_util" | ||
) | ||
|
||
type SendSMSResponse struct { | ||
MessageID int `json:"msg_id"` | ||
Status string `json:"msg_status"` | ||
Description string `json:"msg_status_desc"` | ||
PhoneNo type_util.SensitivePhoneNumber `json:"phoneno"` | ||
} | ||
|
||
func ParseSendSMSResponse(jsonData []byte) (*SendSMSResponse, error) { | ||
response := &SendSMSResponse{} | ||
err := json.Unmarshal(jsonData, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response, 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
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
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,35 @@ | ||
package apis | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/authgear/authgear-sms-gateway/pkg/lib/infra/sms/sendcloud/models" | ||
) | ||
|
||
func Send(client *http.Client, baseUrl string, sendRequest *models.SendRequest, smsKey string) ([]byte, *models.SendResponse, error) { | ||
values := sendRequest.ToValues() | ||
values.Set("signature", sendRequest.Sign(smsKey)) | ||
|
||
data := values.Encode() | ||
|
||
req, _ := http.NewRequest("POST", fmt.Sprintf("%v/smsapi/send", baseUrl), strings.NewReader(data)) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
resp, err := client.Do(req) | ||
|
||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
respData, err := io.ReadAll(resp.Body) | ||
|
||
sendResponse, err := models.ParseSendResponse(respData) | ||
if err != nil { | ||
return respData, nil, err | ||
} | ||
|
||
return respData, sendResponse, err | ||
} |
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,109 @@ | ||
package models | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type SendRequest struct { | ||
msgType string | ||
phone []string | ||
sendRequestId string | ||
smsUser string | ||
templateId string | ||
vars map[string]interface{} | ||
} | ||
|
||
func NewSendRequest( | ||
msgType string, | ||
phone []string, | ||
smsUser string, | ||
templateId string, | ||
vars map[string]interface{}, | ||
) SendRequest { | ||
s := SendRequest{ | ||
msgType: msgType, | ||
phone: phone, | ||
smsUser: smsUser, | ||
templateId: templateId, | ||
vars: vars, | ||
} | ||
presign := s.Presign() | ||
h := md5.Sum([]byte(presign)) | ||
sendRequestId := hex.EncodeToString(h[:]) | ||
return SendRequest{ | ||
msgType: msgType, | ||
phone: phone, | ||
sendRequestId: sendRequestId, | ||
smsUser: smsUser, | ||
templateId: templateId, | ||
vars: vars, | ||
} | ||
} | ||
|
||
func (r *SendRequest) Presign() string { | ||
vars, _ := json.Marshal(r.vars) | ||
return strings.Join([]string{ | ||
fmt.Sprintf("msgType=%v", r.msgType), | ||
fmt.Sprintf("phone=%v", strings.Join(r.phone, ",")), | ||
fmt.Sprintf("sendRequestId=%v", r.sendRequestId), | ||
fmt.Sprintf("smsUser=%v", r.smsUser), | ||
fmt.Sprintf("templateId=%v", r.templateId), | ||
fmt.Sprintf("vars=%v", string(vars)), | ||
}, "&") | ||
} | ||
|
||
func (r *SendRequest) ToMap() map[string]interface{} { | ||
vars, _ := json.Marshal(r.vars) | ||
return map[string]interface{}{ | ||
"msgType": r.msgType, | ||
"phone": strings.Join(r.phone, ","), | ||
"sendRequestId": r.sendRequestId, | ||
"smsUser": r.smsUser, | ||
"templateId": r.templateId, | ||
"vars": vars, | ||
} | ||
} | ||
|
||
func (r *SendRequest) ToValues() url.Values { | ||
vars, _ := json.Marshal(r.vars) | ||
values := url.Values{} | ||
values.Set("msgType", fmt.Sprintf("%v", r.msgType)) | ||
values.Set("phone", strings.Join(r.phone, ",")) | ||
values.Set("sendRequestId", r.sendRequestId) | ||
values.Set("smsUser", r.smsUser) | ||
values.Set("templateId", fmt.Sprintf("%v", r.templateId)) | ||
values.Set("vars", string(vars)) | ||
return values | ||
} | ||
|
||
func (r *SendRequest) Sign(key string) string { | ||
signStr := fmt.Sprintf("%v&%v&%v", key, r.Presign(), key) | ||
h := md5.Sum([]byte(signStr)) | ||
return hex.EncodeToString(h[:]) | ||
} | ||
|
||
type SendResponseInfo struct { | ||
SuccessCount int `json:"successCount,omitempty"` | ||
SMSIDs []string `json:"smsIds,omitempty"` | ||
} | ||
|
||
type SendResponse struct { | ||
Result bool `json:"result,omitempty"` | ||
StatusCode int `json:"statusCode,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Info *SendResponseInfo `json:"info"` | ||
} | ||
|
||
func ParseSendResponse(jsonData []byte) (*SendResponse, error) { | ||
response := &SendResponse{} | ||
err := json.Unmarshal(jsonData, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response, nil | ||
} |
Oops, something went wrong.