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