sms completed

This commit is contained in:
Vladimir V Maksimov
2025-12-03 18:34:25 +03:00
parent d7bd28b759
commit 85cbc3e940
4 changed files with 171 additions and 9 deletions

View File

@@ -4,6 +4,8 @@ import (
"log" "log"
"os" "os"
"testing" "testing"
"github.com/google/uuid"
) )
func loadToken(t *testing.T) string { func loadToken(t *testing.T) string {
@@ -28,3 +30,22 @@ func TestSMS(t *testing.T) {
"Привет", "Привет",
) )
} }
func TestSMSWait(t *testing.T) {
token := loadToken(t)
trID, err := SendSMSWait(token, "+79857770038", "NG.Market", "Привет из Москвы :)")
if err != nil {
t.Fatal(err)
}
t.Log(trID)
}
func TestStatus(t *testing.T) {
token := loadToken(t)
id, _ := uuid.Parse("9987bf00-c082-4dec-b25f-875eb528cf34")
status, err := RequestStatus(id, token)
if err != nil {
t.Fatal(err)
}
t.Log(status)
}

View File

@@ -9,6 +9,12 @@ import (
"net/url" "net/url"
) )
type ErrorCode struct {
Error int `json:"error"`
Name string `json:"name"`
Message string `json:"message"`
}
func httpRequest(req *http.Request, resp any) error { func httpRequest(req *http.Request, resp any) error {
client := &http.Client{} client := &http.Client{}
@@ -23,7 +29,13 @@ func httpRequest(req *http.Request, resp any) error {
return fmt.Errorf("http response read error: %w", err) return fmt.Errorf("http response read error: %w", err)
} }
//log.Println(string(body))
if err = json.Unmarshal(body, resp); err != nil { if err = json.Unmarshal(body, resp); err != nil {
var rErr ErrorCode
if err = json.Unmarshal(body, &rErr); err == nil && rErr.Error != 0 {
return fmt.Errorf("error %d %s: %s", rErr.Error, rErr.Name, rErr.Message)
}
err = fmt.Errorf("http response unmarshal error: %w", err) err = fmt.Errorf("http response unmarshal error: %w", err)
} }
return err return err

42
sms.go
View File

@@ -1,7 +1,9 @@
package sigmasms package sigmasms
import ( import (
"fmt"
"log" "log"
"time"
"github.com/google/uuid" "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( err := requestPost(
"https://user.sigmasms.ru/api/sendings", "https://user.sigmasms.ru/api/sendings",
@@ -63,7 +65,39 @@ func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
return nil, err return nil, err
} }
log.Println(resp) return &resp, nil
}
return nil, 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
} }

105
status.go
View File

@@ -1,9 +1,104 @@
package sigmasms package sigmasms
import "github.com/google/uuid" import (
"github.com/google/uuid"
)
func RequestStatus(id uuid.UUID, apiKey string) error { /*
url := "https://user.sigmasms.ru/api/sendings/" + id.String() {
resp := make(map[string]) "id": "9987bf00-c082-4dec-b25f-875eb528cf34",
requestGet(url, apiKey, nil, ) "chainId": "9987bf00-c082-4dec-b25f-875eb528cf34",
"groupId": null,
"type": "sms",
"createdAt": "2025-12-02T16:13:27.265Z",
"updatedAt": "2025-12-02T16:13:34.194Z",
"meta": {
"billing": {
"id": "4dbd2a09-0a77-4e70-8706-3a9851fcb587",
"amount": -7.49,
"refunded": false,
"TariffId": "9d9c19a9-5ac9-4963-9e4b-d69f78e811e0"
},
"billings": {
"ids": [
"4dbd2a09-0a77-4e70-8706-3a9851fcb587"
]
},
"stats": {
"segments": 1,
"characters": 6
},
"patternId": false,
"_recipientData": {
"mcc": "250",
"mnc": "01",
"code": "Мобильные ТелеСистемы ПАО_250_01",
"type": "def",
"group": "Russia | MTS",
"operator": "Мобильные ТелеСистемы ПАО"
}
},
"state": {
"status": "delivered",
"error": false,
"extendedStatus": null
},
"OwnerId": "99f5860a-a342-48fc-8e2e-3d4a26cdb79f"
}
*/
type ReceepientData struct {
MCC string `json:"mcc"`
MNC string `json:"mnc"`
Code string `json:"code"`
Type string `json:"type"`
Group string `json:"group"`
Operator string `json:"operator"`
}
type Billing struct {
ID string `json:"id"`
Amount float64 `json:"amount"`
Refunded bool `json:"refunded"`
TariffID string `json:"TariffId"`
}
type Billings struct {
IDs []uuid.UUID `json:"ids"`
}
type Stats struct {
Segments int `json:"segments"`
Characters int `json:"characters"`
}
type Meta struct {
Billing Billing `json:"billing"`
Billings Billings `json:"billings"`
Stats Stats `json:"stats"`
PatternID bool `json:"patternId"`
RecipientData ReceepientData `json:"_recipientData"`
}
type State struct {
Status string `json:"status"`
Error bool `json:"error"`
ExtendedStatus string `json:"extendedStatus"`
}
type StatusResponse struct {
ID uuid.UUID `json:"id"`
ChainID uuid.UUID `json:"chainId"`
Type string `json:"type"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
State State `json:"state"`
Meta *Meta `json:"meta"`
}
// 9987bf00-c082-4dec-b25f-875eb528cf34
func RequestStatus(id uuid.UUID, apiKey string) (resp *StatusResponse, err error) {
url := "https://user.sigmasms.ru/api/sendings/" + id.String()
err = requestGet(url, apiKey, nil, &resp)
return
} }