diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74628cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +token.test \ No newline at end of file diff --git a/flash_call.go b/flash_call.go new file mode 100644 index 0000000..f3d320b --- /dev/null +++ b/flash_call.go @@ -0,0 +1,46 @@ +package sigmasms + +import ( + "bytes" + "encoding/json" + "net/http" +) + +type FlashCallRequest struct { + APIKey string `json:"api_key"` + Phone string `json:"phone"` + CodeLen int `json:"code_len"` // 4, 5, 6 +} + +type FlashCallResponse struct { + Status string `json:"status"` + CallID string `json:"call_id"` + From string `json:"from"` // номер, с которого звонили + Code string `json:"code"` // обычно это последние digits из from +} + +func FlashCall(apiKey, phone string, codeLen int) (*FlashCallResponse, error) { + body := FlashCallRequest{ + APIKey: apiKey, + Phone: phone, + CodeLen: codeLen, + } + + data, _ := json.Marshal(body) + + resp, err := http.Post( + "https://voice.sigmasms.ru/flashcall", + "application/json", + bytes.NewReader(data), + ) + 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 +} diff --git a/flashcall_test.go b/flashcall_test.go new file mode 100644 index 0000000..060adbf --- /dev/null +++ b/flashcall_test.go @@ -0,0 +1,30 @@ +package sigmasms + +import ( + "log" + "os" + "testing" +) + +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) + log.Println(token) +} + +func TestSMS(t *testing.T) { + token := loadToken(t) + SendSMS( + token, + "+79857770038", + "NG.Market", + "Привет", + ) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e840b57 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.gm6.ru/icewind/sigmasms + +go 1.25.4 + +require github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/go.sum @@ -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= diff --git a/request.go b/request.go new file mode 100644 index 0000000..ef15ed3 --- /dev/null +++ b/request.go @@ -0,0 +1,62 @@ +package sigmasms + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" +) + +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) + } + + if err = json.Unmarshal(body, resp); err != nil { + 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 +} diff --git a/sms.go b/sms.go new file mode 100644 index 0000000..8b81bff --- /dev/null +++ b/sms.go @@ -0,0 +1,69 @@ +package sigmasms + +import ( + "log" + + "github.com/google/uuid" +) + +/* +{ + "recipient": "+79999999999", + "type": "sms", + "payload": { + "sender": "Имя отправителя", + "text": "Текст сообщения" + } +} +*/ + +var ( + TypeSMS = "sms" +) + +type Payload struct { + Sender string `json:"sender"` + Text string `json:"text"` +} + +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, + }, + } + + resp := make(map[string]any) + + err := requestPost( + "https://user.sigmasms.ru/api/sendings", + apiKey, + &req, + &resp, + ) + + if err != nil { + return nil, err + } + + log.Println(resp) + + return nil, nil +} diff --git a/status.go b/status.go new file mode 100644 index 0000000..a2aa11f --- /dev/null +++ b/status.go @@ -0,0 +1,9 @@ +package sigmasms + +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]) + requestGet(url, apiKey, nil, ) +}