Compare commits
2 Commits
main
...
bd04a8a13b
| Author | SHA1 | Date | |
|---|---|---|---|
| bd04a8a13b | |||
| 93fcaa458d |
@@ -1,6 +0,0 @@
|
|||||||
package sigmasms
|
|
||||||
|
|
||||||
type Payload struct {
|
|
||||||
Sender string `json:"sender"`
|
|
||||||
Text string `json:"text"`
|
|
||||||
}
|
|
||||||
111
flash_call.go
111
flash_call.go
@@ -1,105 +1,46 @@
|
|||||||
package sigmasms
|
package sigmasms
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"bytes"
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"log"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
|
||||||
{
|
|
||||||
"recipient": "+79999999999",
|
|
||||||
"type": "flashcall",
|
|
||||||
"payload": {
|
|
||||||
"sender": "Имя отправителя",
|
|
||||||
"text": "1234"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
type FlashCallRequest struct {
|
type FlashCallRequest struct {
|
||||||
Recepient string `json:"recipient"`
|
APIKey string `json:"api_key"`
|
||||||
Type string `json:"type"`
|
Phone string `json:"phone"`
|
||||||
Payload Payload `json:"payload"`
|
CodeLen int `json:"code_len"` // 4, 5, 6
|
||||||
}
|
}
|
||||||
|
|
||||||
type FlashCallResponse struct {
|
type FlashCallResponse struct {
|
||||||
ID uuid.UUID `json:"id"`
|
Status string `json:"status"`
|
||||||
Price float64 `json:"price"`
|
CallID string `json:"call_id"`
|
||||||
Recepient string `json:"recipient"`
|
From string `json:"from"` // номер, с которого звонили
|
||||||
Status string `json:"status"`
|
Code string `json:"code"` // обычно это последние digits из from
|
||||||
Error string `json:"error"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateRandomDigits генерирует строку из четырёх случайных цифр.
|
func FlashCall(apiKey, phone string, codeLen int) (*FlashCallResponse, error) {
|
||||||
func GenerateFreshcCallCode() (string, error) {
|
body := FlashCallRequest{
|
||||||
var b [2]byte
|
APIKey: apiKey,
|
||||||
_, err := rand.Read(b[:])
|
Phone: phone,
|
||||||
if err != nil {
|
CodeLen: codeLen,
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Превращаем 2 байта в число
|
data, _ := json.Marshal(body)
|
||||||
num := int(b[0])<<8 | int(b[1])
|
|
||||||
// Ограничиваем диапазон 0–9999
|
|
||||||
num = num % 10000
|
|
||||||
|
|
||||||
// Возвращаем строку с ведущими нулями
|
resp, err := http.Post(
|
||||||
return fmt.Sprintf("%04d", num), nil
|
"https://voice.sigmasms.ru/flashcall",
|
||||||
}
|
"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()
|
||||||
|
|
||||||
return &resp, nil
|
var result FlashCallResponse
|
||||||
}
|
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) {
|
|
||||||
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
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadToken(t *testing.T) string {
|
func loadToken(t *testing.T) string {
|
||||||
@@ -18,20 +16,7 @@ func loadToken(t *testing.T) string {
|
|||||||
|
|
||||||
func TestFlashCall(t *testing.T) {
|
func TestFlashCall(t *testing.T) {
|
||||||
token := loadToken(t)
|
token := loadToken(t)
|
||||||
code, _ := GenerateFreshcCallCode()
|
log.Println(token)
|
||||||
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) {
|
||||||
@@ -43,30 +28,3 @@ 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("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)
|
|
||||||
}
|
|
||||||
|
|||||||
12
request.go
12
request.go
@@ -9,12 +9,6 @@ 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{}
|
||||||
|
|
||||||
@@ -29,13 +23,7 @@ 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
|
||||||
|
|||||||
44
sms.go
44
sms.go
@@ -1,9 +1,7 @@
|
|||||||
package sigmasms
|
package sigmasms
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@@ -23,6 +21,11 @@ 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"`
|
||||||
@@ -47,7 +50,7 @@ func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp SendSMSResponse
|
resp := make(map[string]any)
|
||||||
|
|
||||||
err := requestPost(
|
err := requestPost(
|
||||||
"https://user.sigmasms.ru/api/sendings",
|
"https://user.sigmasms.ru/api/sendings",
|
||||||
@@ -60,38 +63,7 @@ func SendSMS(apiKey, phone, sender, text string) (*SendSMSResponse, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resp, nil
|
log.Println(resp)
|
||||||
}
|
|
||||||
|
|
||||||
func SendSMSWait(apiKey, phone, sender, text string) (transactionID uuid.UUID, err error) {
|
return nil, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
61
status.go
61
status.go
@@ -1,62 +1,9 @@
|
|||||||
package sigmasms
|
package sigmasms
|
||||||
|
|
||||||
import (
|
import "github.com/google/uuid"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RecepientData struct {
|
func RequestStatus(id uuid.UUID, apiKey string) error {
|
||||||
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()
|
url := "https://user.sigmasms.ru/api/sendings/" + id.String()
|
||||||
err = requestGet(url, apiKey, nil, &resp)
|
resp := make(map[string])
|
||||||
return
|
requestGet(url, apiKey, nil, )
|
||||||
}
|
}
|
||||||
|
|||||||
1
token.test
Normal file
1
token.test
Normal file
@@ -0,0 +1 @@
|
|||||||
|
b1eaeb6864933be52d8a203785c009fd6f33ccd557786836a8ca9ef3aa6b7aff
|
||||||
Reference in New Issue
Block a user