Files
sigmasms/sms.go
Vladimir V Maksimov 85cbc3e940 sms completed
2025-12-03 18:34:25 +03:00

104 lines
1.9 KiB
Go

package sigmasms
import (
"fmt"
"log"
"time"
"github.com/google/uuid"
)
/*
{
"recipient": "+79999999999",
"type": "sms",
"payload": {
"sender": "Имя отправителя",
"text": "Текст сообщения"
}
}
*/
var (
TypeSMS = "sms"
)
type Payload struct {
Sender string `json:"sender"`
Text string `json:"text"`
}
type SendSMSResponse struct {
Error string `json:"error"`
ID uuid.UUID `json:"id"`
Price float64 `json:"price"`
Recepient string `json:"recipient"`
Status string `json:"status"`
}
type SendSMSRequest struct {
Recepient string `json:"recipient"`
Type string `json:"type"`
Payload Payload `json:"payload"`
}
func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
req := SendSMSRequest{
Recepient: phone,
Type: TypeSMS,
Payload: Payload{
Sender: sender,
Text: text,
},
}
var resp SendSMSResponse
err := requestPost(
"https://user.sigmasms.ru/api/sendings",
apiKey,
&req,
&resp,
)
if err != nil {
return nil, err
}
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
}