in progress
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
token.test
|
||||||
46
flash_call.go
Normal file
46
flash_call.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
30
flashcall_test.go
Normal file
30
flashcall_test.go
Normal file
@@ -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",
|
||||||
|
"Привет",
|
||||||
|
)
|
||||||
|
}
|
||||||
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=
|
||||||
62
request.go
Normal file
62
request.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
69
sms.go
Normal file
69
sms.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user