diff --git a/core/typescore/telephony.go b/core/typescore/telephony.go index ced46c5..716f277 100644 --- a/core/typescore/telephony.go +++ b/core/typescore/telephony.go @@ -1,6 +1,9 @@ package typescore -import "time" +import ( + "encoding/json" + "time" +) // SmsLog - модель для хранения истории SMS type SmsLog struct { @@ -31,15 +34,17 @@ type WebhookMeta struct { // CallEvent описывает полезную нагрузку события (звонок или SMS) type CallEvent struct { - EventType int `json:"event_type"` // 1 - начало; 2 - ответ; 4 - окончание; 32 - SMS - EventCreated int64 `json:"event_created"` - Direction int `json:"direction"` // 0 - входящий, 1 - исходящий - ClientNumber string `json:"client_number"` - ClientName string `json:"client_name"` - EventPbxCallID string `json:"event_pbx_call_id"` - SrcNumber string `json:"src_number"` - SrcID string `json:"src_id"` - SrcSlot int `json:"src_slot"` + EventType int `json:"event_type"` // 1 - начало; 2 - ответ; 4 - окончание; 32 - SMS + EventCreated int64 `json:"event_created"` + Direction int `json:"direction"` // 0 - входящий, 1 - исходящий + ClientNumber string `json:"client_number"` + ClientName string `json:"client_name"` + SrcNumber string `json:"src_number"` + SrcSlot int `json:"src_slot"` + + EventPbxCallID FlexString `json:"event_pbx_call_id"` + SrcID FlexString `json:"src_id"` + DBCallID FlexString `json:"db_call_id,omitempty"` // Поля, которые приходят только в событии окончания звонка (event_type == 4) StartTime int64 `json:"start_time,omitempty"` @@ -48,8 +53,26 @@ type CallEvent struct { Duration int `json:"duration,omitempty"` Answered int `json:"answered,omitempty"` // 0 - не отвечен, 1 - отвечен Recording string `json:"recording,omitempty"` // Ссылка на запись разговора - DBCallID string `json:"db_call_id,omitempty"` // Поле, которое приходит только для SMS (event_type == 32) Text string `json:"text,omitempty"` } + +// FlexString принимает JSON-строку или число. +type FlexString string + +func (f *FlexString) UnmarshalJSON(b []byte) error { + if string(b) == "null" { + return nil + } + if len(b) > 0 && b[0] == '"' { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + *f = FlexString(s) + return nil + } + *f = FlexString(b) // число как есть + return nil +} diff --git a/service/rest_telephony_service/cmd/app.go b/service/rest_telephony_service/cmd/app.go index db8d794..fe12141 100644 --- a/service/rest_telephony_service/cmd/app.go +++ b/service/rest_telephony_service/cmd/app.go @@ -130,7 +130,7 @@ func initBaseApiRouter(ipc *typesm.InternalProviderControl) (*chi.Mux, error) { // регистрирует маршруты func registerRoutes(router chi.Router, ipc *typesm.InternalProviderControl) error { // Подключаем наши маршруты - if err := telephony.RegisterTelephonyRoutes(router, ipc); err != nil { + if err := telephonyservice.RegisterTelephonyRoutes(router, ipc); err != nil { return errors.New(fmt.Sprintf("🔴 failed to register telephony routes: %v", err)) } diff --git a/service/rest_telephony_service/handlers/telephony/bitrix.go b/service/rest_telephony_service/handlers/telephony/bitrix.go index d6cc90d..391205f 100644 --- a/service/rest_telephony_service/handlers/telephony/bitrix.go +++ b/service/rest_telephony_service/handlers/telephony/bitrix.go @@ -1,4 +1,4 @@ -package telephony +package telephonyservice import ( "bytes" diff --git a/service/rest_telephony_service/handlers/telephony/handlers.go b/service/rest_telephony_service/handlers/telephony/handlers.go index b5e7939..430519f 100644 --- a/service/rest_telephony_service/handlers/telephony/handlers.go +++ b/service/rest_telephony_service/handlers/telephony/handlers.go @@ -1,9 +1,11 @@ -package telephony +package telephonyservice import ( "bitrix_balancer_api/core/typescore" + "bytes" "context" "errors" + "io" "net/http" "rest_telephony_service/handlers" typesm "rest_telephony_service/types" @@ -17,6 +19,11 @@ import ( func (s *TelephonyReg) WebhookTelephonyHandler(w http.ResponseWriter, r *http.Request) (interface{}, error) { var req typesm.TelephonyWebhookReq + body, _ := io.ReadAll(r.Body) + logrus.Infof("webhook raw: %s", string(body)) + r.Body = io.NopCloser(bytes.NewReader(body)) + r.ContentLength = int64(len(body)) + // Используем ваш стандартный парсер тела запроса if err := handlers.ParseRequestBodyPost(r, &req); err != nil { return nil, errors.New("invalid request body") diff --git a/service/rest_telephony_service/handlers/telephony/routes.go b/service/rest_telephony_service/handlers/telephony/routes.go index 3baa024..0941e87 100644 --- a/service/rest_telephony_service/handlers/telephony/routes.go +++ b/service/rest_telephony_service/handlers/telephony/routes.go @@ -1,4 +1,4 @@ -package telephony +package telephonyservice import ( "github.com/go-chi/chi/v5"