sms completed
This commit is contained in:
42
sms.go
42
sms.go
@@ -1,7 +1,9 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -50,7 +52,7 @@ func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
|
||||
},
|
||||
}
|
||||
|
||||
resp := make(map[string]any)
|
||||
var resp SendSMSResponse
|
||||
|
||||
err := requestPost(
|
||||
"https://user.sigmasms.ru/api/sendings",
|
||||
@@ -63,7 +65,39 @@ func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println(resp)
|
||||
|
||||
return nil, nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func SendSMSWait(apiKey, phone, sender, text string) (transactionID uuid.UUID, err error) {
|
||||
var resp *SendSMSResponse
|
||||
if resp, err = SendSMS(apiKey, phone, sender, text); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
} else {
|
||||
if resp.Status == "failed" {
|
||||
err = fmt.Errorf("sms send error: %s", resp.Error)
|
||||
return
|
||||
}
|
||||
transactionID = resp.ID
|
||||
var sResp *StatusResponse
|
||||
mainLoop:
|
||||
for resp.Status == "pending" {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
sResp, err = RequestStatus(resp.ID, apiKey)
|
||||
log.Println(sResp.State.Status, err)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp.Status = sResp.State.Status
|
||||
switch resp.Status {
|
||||
case "pending", "sent":
|
||||
continue mainLoop
|
||||
case "seen", "delivered":
|
||||
return
|
||||
default:
|
||||
err = fmt.Errorf("sms status: %s", sResp.State.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user