flash call

This commit is contained in:
2025-12-03 19:45:24 +03:00
parent 85cbc3e940
commit 1bc08c90bd
5 changed files with 122 additions and 84 deletions

6
common.go Normal file
View File

@@ -0,0 +1,6 @@
package sigmasms
type Payload struct {
Sender string `json:"sender"`
Text string `json:"text"`
}

View File

@@ -1,46 +1,105 @@
package sigmasms package sigmasms
import ( import (
"bytes" "crypto/rand"
"encoding/json" "fmt"
"net/http" "log"
"time"
"github.com/google/uuid"
) )
/*
{
"recipient": "+79999999999",
"type": "flashcall",
"payload": {
"sender": "Имя отправителя",
"text": "1234"
}
}
*/
type FlashCallRequest struct { type FlashCallRequest struct {
APIKey string `json:"api_key"` Recepient string `json:"recipient"`
Phone string `json:"phone"` Type string `json:"type"`
CodeLen int `json:"code_len"` // 4, 5, 6 Payload Payload `json:"payload"`
} }
type FlashCallResponse struct { type FlashCallResponse struct {
Status string `json:"status"` ID uuid.UUID `json:"id"`
CallID string `json:"call_id"` Price float64 `json:"price"`
From string `json:"from"` // номер, с которого звонили Recepient string `json:"recipient"`
Code string `json:"code"` // обычно это последние digits из from Status string `json:"status"`
Error string `json:"error"`
} }
func FlashCall(apiKey, phone string, codeLen int) (*FlashCallResponse, error) { // GenerateRandomDigits генерирует строку из четырёх случайных цифр.
body := FlashCallRequest{ func GenerateFreshcCallCode() (string, error) {
APIKey: apiKey, var b [2]byte
Phone: phone, _, err := rand.Read(b[:])
CodeLen: codeLen, if err != nil {
return "", err
} }
data, _ := json.Marshal(body) // Превращаем 2 байта в число
num := int(b[0])<<8 | int(b[1])
// Ограничиваем диапазон 09999
num = num % 10000
resp, err := http.Post( // Возвращаем строку с ведущими нулями
"https://voice.sigmasms.ru/flashcall", return fmt.Sprintf("%04d", num), nil
"application/json", }
bytes.NewReader(data),
) 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 { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close()
var result FlashCallResponse return &resp, nil
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { }
return nil, err
} func FlashCallWait(apiKey, phone, sender string, code string) (transactionID uuid.UUID, err error) {
return &result, nil 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
} }

View File

@@ -18,7 +18,20 @@ func loadToken(t *testing.T) string {
func TestFlashCall(t *testing.T) { func TestFlashCall(t *testing.T) {
token := loadToken(t) token := loadToken(t)
log.Println(token) 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) { func TestSMS(t *testing.T) {
@@ -42,10 +55,18 @@ func TestSMSWait(t *testing.T) {
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
token := loadToken(t) token := loadToken(t)
id, _ := uuid.Parse("9987bf00-c082-4dec-b25f-875eb528cf34") id, _ := uuid.Parse("518095e8-a62d-4af1-ad1f-2a56e955cf55")
status, err := RequestStatus(id, token) status, err := RequestStatus(id, token)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(status) t.Log(status)
} }
func TestFreshCallCode(t *testing.T) {
code, err := GenerateFreshcCallCode()
if err != nil {
t.Fatal(err)
}
t.Log(code)
}

6
sms.go
View File

@@ -23,11 +23,6 @@ var (
TypeSMS = "sms" TypeSMS = "sms"
) )
type Payload struct {
Sender string `json:"sender"`
Text string `json:"text"`
}
type SendSMSResponse struct { type SendSMSResponse struct {
Error string `json:"error"` Error string `json:"error"`
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
@@ -84,7 +79,6 @@ func SendSMSWait(apiKey, phone, sender, text string) (transactionID uuid.UUID, e
for resp.Status == "pending" { for resp.Status == "pending" {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
sResp, err = RequestStatus(resp.ID, apiKey) sResp, err = RequestStatus(resp.ID, apiKey)
log.Println(sResp.State.Status, err)
if err != nil { if err != nil {
return return
} }

View File

@@ -4,50 +4,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
/* type RecepientData struct {
{
"id": "9987bf00-c082-4dec-b25f-875eb528cf34",
"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"` MCC string `json:"mcc"`
MNC string `json:"mnc"` MNC string `json:"mnc"`
Code string `json:"code"` Code string `json:"code"`
@@ -73,11 +30,11 @@ type Stats struct {
} }
type Meta struct { type Meta struct {
Billing Billing `json:"billing"` Billing Billing `json:"billing"`
Billings Billings `json:"billings"` Billings Billings `json:"billings"`
Stats Stats `json:"stats"` Stats Stats `json:"stats"`
PatternID bool `json:"patternId"` PatternID bool `json:"patternId"`
RecipientData ReceepientData `json:"_recipientData"` RecepientData RecepientData `json:"_recipientData"`
} }
type State struct { type State struct {
@@ -94,6 +51,7 @@ type StatusResponse struct {
UpdatedAt string `json:"updatedAt"` UpdatedAt string `json:"updatedAt"`
State State `json:"state"` State State `json:"state"`
Meta *Meta `json:"meta"` Meta *Meta `json:"meta"`
OwnerID uuid.UUID `json:"OwnerId"`
} }
// 9987bf00-c082-4dec-b25f-875eb528cf34 // 9987bf00-c082-4dec-b25f-875eb528cf34