Compare commits
3 Commits
1af2045a1b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bc08c90bd | |||
|
|
85cbc3e940 | ||
| d7bd28b759 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
token.test
|
||||
6
common.go
Normal file
6
common.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package sigmasms
|
||||
|
||||
type Payload struct {
|
||||
Sender string `json:"sender"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
105
flash_call.go
Normal file
105
flash_call.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
{
|
||||
"recipient": "+79999999999",
|
||||
"type": "flashcall",
|
||||
"payload": {
|
||||
"sender": "Имя отправителя",
|
||||
"text": "1234"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
type FlashCallRequest struct {
|
||||
Recepient string `json:"recipient"`
|
||||
Type string `json:"type"`
|
||||
Payload Payload `json:"payload"`
|
||||
}
|
||||
|
||||
type FlashCallResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Price float64 `json:"price"`
|
||||
Recepient string `json:"recipient"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// GenerateRandomDigits генерирует строку из четырёх случайных цифр.
|
||||
func GenerateFreshcCallCode() (string, error) {
|
||||
var b [2]byte
|
||||
_, err := rand.Read(b[:])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Превращаем 2 байта в число
|
||||
num := int(b[0])<<8 | int(b[1])
|
||||
// Ограничиваем диапазон 0–9999
|
||||
num = num % 10000
|
||||
|
||||
// Возвращаем строку с ведущими нулями
|
||||
return fmt.Sprintf("%04d", num), nil
|
||||
}
|
||||
|
||||
func FlashCall(apiKey, phone, sender string, code string) (*FlashCallResponse, error) {
|
||||
req := FlashCallRequest{
|
||||
Recepient: phone,
|
||||
Type: "flashcall",
|
||||
Payload: Payload{
|
||||
Sender: sender,
|
||||
Text: code,
|
||||
},
|
||||
}
|
||||
|
||||
var resp FlashCallResponse
|
||||
|
||||
err := requestPost("https://user.sigmasms.ru/api/sendings", apiKey, &req, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func FlashCallWait(apiKey, phone, sender string, code string) (transactionID uuid.UUID, err error) {
|
||||
var resp *FlashCallResponse
|
||||
if resp, err = FlashCall(apiKey, phone, sender, code); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
} else {
|
||||
if resp.Status == "failed" {
|
||||
err = fmt.Errorf("flashcall 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)
|
||||
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
|
||||
}
|
||||
72
flashcall_test.go
Normal file
72
flashcall_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func loadToken(t *testing.T) string {
|
||||
data, err := os.ReadFile("./token.test")
|
||||
if err != nil {
|
||||
t.Fatalf("test token load error: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func TestFlashCall(t *testing.T) {
|
||||
token := loadToken(t)
|
||||
code, _ := GenerateFreshcCallCode()
|
||||
log.Println(code)
|
||||
FlashCall(token, "+79857770038", "NG.Market", code)
|
||||
}
|
||||
|
||||
func TestFlashCallWait(t *testing.T) {
|
||||
token := loadToken(t)
|
||||
code, _ := GenerateFreshcCallCode()
|
||||
log.Println(code)
|
||||
trID, err := FlashCallWait(token, "+79857770038", "NG.Market", code)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(trID)
|
||||
}
|
||||
|
||||
func TestSMS(t *testing.T) {
|
||||
token := loadToken(t)
|
||||
SendSMS(
|
||||
token,
|
||||
"+79857770038",
|
||||
"NG.Market",
|
||||
"Привет",
|
||||
)
|
||||
}
|
||||
|
||||
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("518095e8-a62d-4af1-ad1f-2a56e955cf55")
|
||||
status, err := RequestStatus(id, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(status)
|
||||
}
|
||||
|
||||
func TestFreshCallCode(t *testing.T) {
|
||||
code, err := GenerateFreshcCallCode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(code)
|
||||
}
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module git.gm6.ru/icewind/sigmasms
|
||||
|
||||
go 1.25.4
|
||||
|
||||
require github.com/google/uuid v1.6.0
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
74
request.go
Normal file
74
request.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"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 {
|
||||
client := &http.Client{}
|
||||
|
||||
httpResp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request error: %w", err)
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(httpResp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http response read error: %w", err)
|
||||
}
|
||||
|
||||
//log.Println(string(body))
|
||||
|
||||
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)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func requestPost(url, apiKey string, req, resp any) error {
|
||||
reqData, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("request marshal error: %w", err)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(reqData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request create error: %w", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", apiKey)
|
||||
httpReq.Header.Set("Accept", "application/json")
|
||||
|
||||
err = httpRequest(httpReq, resp)
|
||||
return err
|
||||
}
|
||||
|
||||
func requestGet(url, apiKey string, params url.Values, resp any) error {
|
||||
httpReq, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request create error: %w", err)
|
||||
}
|
||||
httpReq.URL.RawQuery = params.Encode()
|
||||
httpReq.Header.Set("Authorization", apiKey)
|
||||
httpReq.Header.Set("Accept", "application/json")
|
||||
|
||||
err = httpRequest(httpReq, resp)
|
||||
return err
|
||||
}
|
||||
97
sms.go
Normal file
97
sms.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
{
|
||||
"recipient": "+79999999999",
|
||||
"type": "sms",
|
||||
"payload": {
|
||||
"sender": "Имя отправителя",
|
||||
"text": "Текст сообщения"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
var (
|
||||
TypeSMS = "sms"
|
||||
)
|
||||
|
||||
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)
|
||||
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
|
||||
}
|
||||
62
status.go
Normal file
62
status.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package sigmasms
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RecepientData 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"`
|
||||
RecepientData RecepientData `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"`
|
||||
OwnerID uuid.UUID `json:"OwnerId"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user