From ac3a0daee9f618ff060c5680e44f943bdbdd573c Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 5 Sep 2024 19:18:58 -0300 Subject: [PATCH 01/15] Add sponsored product in each product section --- services/external/weni/service.go | 69 ++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 8709db0e3..0df406483 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -108,6 +108,8 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log var productEntries []flows.ProductEntry var productEntry flows.ProductEntry searchResult := []string{} + var searchResultSponsored string + var allProductsSponsored []flows.ProductEntry var trace *httpx.Trace var traces []*httpx.Trace var sellerID string @@ -130,7 +132,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) callResult.Traces = append(callResult.Traces, trace) } else if params.SearchType == "vtex" { - searchResult, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType) + searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig) callResult.Traces = append(callResult.Traces, traces...) allProducts = append(allProducts, searchResult...) if searchResult == nil { @@ -158,6 +160,8 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log productRetailerIDS = nil } productRetailerIDMap = make(map[string]struct{}) + + allProductsSponsored = append(allProductsSponsored, flows.ProductEntry{Product: product, ProductRetailerIDs: []string{searchResultSponsored}}) } callResult.ProductRetailerIDS = productEntries @@ -178,7 +182,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult.Traces = callResult.Traces finalResult.ResponseJSON = callResult.ResponseJSON - for _, productEntry := range callResult.ProductRetailerIDS { + for i, productEntry := range callResult.ProductRetailerIDS { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} @@ -186,14 +190,21 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log if hasSimulation { for _, existingProductId := range existingProductsIds { if productRetailerID == existingProductId { - if len(newEntry.ProductRetailerIDs) < 5 { + if len(newEntry.ProductRetailerIDs) < 4 { newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) + if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 { + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) + } } } } } else { - if len(newEntry.ProductRetailerIDs) < 5 { + if len(newEntry.ProductRetailerIDs) < 4 { newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) + if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 { + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) + } + } } } @@ -322,29 +333,65 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co return products["products"], trace, nil } -func GetProductListFromVtex(productSearch string, searchUrl string, apiType string) ([]string, []*httpx.Trace, error) { +func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config) ([]string, string, []*httpx.Trace, error) { var result []string var traces []*httpx.Trace var err error + var productSponsored string if apiType == "legacy" { result, traces, err = VtexLegacySearch(searchUrl, productSearch) if err != nil { - return nil, traces, err + return nil, productSponsored, traces, err } } else if apiType == "intelligent" { result, traces, err = VtexIntelligentSearch(searchUrl, productSearch) if err != nil { - return nil, traces, err + return nil, productSponsored, traces, err } - } else if apiType == "sponsored" { - result, traces, err = VtexSponsoredSearch(searchUrl, productSearch) + } + + resultSponsored, traces, err := VtexSponsoredSearch(searchUrl, productSearch) + if err != nil { + return nil, productSponsored, traces, err + } + + productRetailerIDS := []string{} + productRetailerIDMap := make(map[string]struct{}) + var productEntries []flows.ProductEntry + var productEntry flows.ProductEntry + + for _, productRetailerID := range resultSponsored { + productRetailerIDS = append(productRetailerIDS, productRetailerID) + productRetailerIDMap[productRetailerID] = struct{}{} + } + + if len(productRetailerIDS) > 0 { + productEntry = flows.ProductEntry{ + Product: searchUrl, + ProductRetailerIDs: productRetailerIDS, + } + productEntries = append(productEntries, productEntry) + productRetailerIDS = nil + } + + retries := 2 + var newProductRetailerIDS []flows.ProductEntry + var tracesMeta []*httpx.Trace + for i := 0; i < retries; i++ { + newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) + traces = append(traces, tracesMeta...) if err != nil { - return nil, traces, err + continue } + break + } + + if len(newProductRetailerIDS[0].ProductRetailerIDs) > 0 { + productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] } - return result, traces, nil + return result, productSponsored, traces, nil } type SearchSeller struct { From 0a3f300a22fa369a3299b2a86d89a114f704b2cd Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 6 Sep 2024 16:28:19 -0300 Subject: [PATCH 02/15] update goflow --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a42777e61..6156f8e47 100644 --- a/go.mod +++ b/go.mod @@ -79,4 +79,4 @@ go 1.17 replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni -replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.13.1-goflow-0.144.3 +replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop diff --git a/go.sum b/go.sum index eede5db83..6cf84a437 100644 --- a/go.sum +++ b/go.sum @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= -github.com/weni-ai/goflow v0.13.1-goflow-0.144.3 h1:KrP/XDHg3z9NXwtsyAWu6DO5XM2zi0pPSzYodCFEBcg= -github.com/weni-ai/goflow v0.13.1-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop h1:Satpn6ixt+mEZDxGOezlUgMivmTjqPBsKFS2Tp0GD6Q= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= From c33bd57ab11de6a2784573cbd1c84fe748787f40 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 6 Sep 2024 16:29:47 -0300 Subject: [PATCH 03/15] Use hasVtex field for sponsored products --- core/models/assets.go | 2 + core/models/org_context.go | 1 + services/external/weni/service.go | 87 +++++++++++++++---------------- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/core/models/assets.go b/core/models/assets.go index 2f03c6666..ab0ad32de 100644 --- a/core/models/assets.go +++ b/core/models/assets.go @@ -408,9 +408,11 @@ func NewOrgAssets(ctx context.Context, rt *runtime.Runtime, orgID OrgID, prev *O if prev == nil || refresh&RefreshContext > 0 { context := oa.org.ConfigValue("description", "") + hasVtex := oa.org.o.Config.Get("has_vtex", false) c := &OrgContext{} c.c.OrgContext = context c.c.ProjectUUID = oa.org.ProjectUUID() + c.c.HasVtex = hasVtex.(bool) oa.orgContexts = append(oa.orgContexts, c) if err != nil { return nil, errors.Wrapf(err, "error loading context for org %d", orgID) diff --git a/core/models/org_context.go b/core/models/org_context.go index 07862d734..4dbf42e1b 100644 --- a/core/models/org_context.go +++ b/core/models/org_context.go @@ -48,6 +48,7 @@ type OrgContext struct { OrgContext string `json:"context"` ChannelUUID assets.ChannelUUID `json:"channel_uuid"` ProjectUUID uuids.UUID `json:"project_uuid"` + HasVtex bool `json:"has_vtex"` } } diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 0df406483..11bc8db63 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -103,6 +103,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log return callResult, err } + hasVtex := params.HasVtex productRetailerIDS := []string{} productRetailerIDMap := make(map[string]struct{}) var productEntries []flows.ProductEntry @@ -114,6 +115,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log var traces []*httpx.Trace var sellerID string var allProducts []string + qttProducts := 5 postalCode_ := strings.TrimSpace(params.PostalCode) if params.PostalCode != "" { @@ -132,7 +134,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) callResult.Traces = append(callResult.Traces, trace) } else if params.SearchType == "vtex" { - searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig) + searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtex) callResult.Traces = append(callResult.Traces, traces...) allProducts = append(allProducts, searchResult...) if searchResult == nil { @@ -185,26 +187,22 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log for i, productEntry := range callResult.ProductRetailerIDS { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} - + if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 && hasVtex { + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) + qttProducts = 4 + } for _, productRetailerID := range productEntry.ProductRetailerIDs { if hasSimulation { for _, existingProductId := range existingProductsIds { if productRetailerID == existingProductId { - if len(newEntry.ProductRetailerIDs) < 4 { + if len(newEntry.ProductRetailerIDs) < qttProducts { newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) - if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) - } } } } } else { - if len(newEntry.ProductRetailerIDs) < 4 { + if len(newEntry.ProductRetailerIDs) < qttProducts { newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) - if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) - } - } } } @@ -333,7 +331,7 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co return products["products"], trace, nil } -func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config) ([]string, string, []*httpx.Trace, error) { +func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config, hasVtex bool) ([]string, string, []*httpx.Trace, error) { var result []string var traces []*httpx.Trace var err error @@ -350,45 +348,46 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri return nil, productSponsored, traces, err } } + if hasVtex { + resultSponsored, traces, err := VtexSponsoredSearch(searchUrl, productSearch) + if err != nil { + return nil, productSponsored, traces, err + } - resultSponsored, traces, err := VtexSponsoredSearch(searchUrl, productSearch) - if err != nil { - return nil, productSponsored, traces, err - } - - productRetailerIDS := []string{} - productRetailerIDMap := make(map[string]struct{}) - var productEntries []flows.ProductEntry - var productEntry flows.ProductEntry + productRetailerIDS := []string{} + productRetailerIDMap := make(map[string]struct{}) + var productEntries []flows.ProductEntry + var productEntry flows.ProductEntry - for _, productRetailerID := range resultSponsored { - productRetailerIDS = append(productRetailerIDS, productRetailerID) - productRetailerIDMap[productRetailerID] = struct{}{} - } + for _, productRetailerID := range resultSponsored { + productRetailerIDS = append(productRetailerIDS, productRetailerID) + productRetailerIDMap[productRetailerID] = struct{}{} + } - if len(productRetailerIDS) > 0 { - productEntry = flows.ProductEntry{ - Product: searchUrl, - ProductRetailerIDs: productRetailerIDS, + if len(productRetailerIDS) > 0 { + productEntry = flows.ProductEntry{ + Product: searchUrl, + ProductRetailerIDs: productRetailerIDS, + } + productEntries = append(productEntries, productEntry) + productRetailerIDS = nil } - productEntries = append(productEntries, productEntry) - productRetailerIDS = nil - } - retries := 2 - var newProductRetailerIDS []flows.ProductEntry - var tracesMeta []*httpx.Trace - for i := 0; i < retries; i++ { - newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) - traces = append(traces, tracesMeta...) - if err != nil { - continue + retries := 2 + var newProductRetailerIDS []flows.ProductEntry + var tracesMeta []*httpx.Trace + for i := 0; i < retries; i++ { + newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) + traces = append(traces, tracesMeta...) + if err != nil { + continue + } + break } - break - } - if len(newProductRetailerIDS[0].ProductRetailerIDs) > 0 { - productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] + if len(newProductRetailerIDS[0].ProductRetailerIDs) > 0 { + productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] + } } return result, productSponsored, traces, nil From 16e3d26344536ddc9caaebeffc846352b8ec0ff5 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 6 Sep 2024 16:48:11 -0300 Subject: [PATCH 04/15] Add interface for HasVtex --- core/models/org_context.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/models/org_context.go b/core/models/org_context.go index 4dbf42e1b..dd9a25939 100644 --- a/core/models/org_context.go +++ b/core/models/org_context.go @@ -55,6 +55,7 @@ type OrgContext struct { func (c *OrgContext) Context() string { return c.c.OrgContext } func (c *OrgContext) ChannelUUID() assets.ChannelUUID { return c.c.ChannelUUID } func (c *OrgContext) ProjectUUID() uuids.UUID { return c.c.ProjectUUID } +func (c *OrgContext) HasVtex() bool { return c.c.HasVtex } type OrgContextService interface { flows.OrgContextService From 34ef8f70e2a4b9aa1731bbd95cfc56156cc9e845 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 13 Sep 2024 17:08:12 -0300 Subject: [PATCH 05/15] update goflow --- go.mod | 3 +-- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index f9892665e..eaf0efa2b 100644 --- a/go.mod +++ b/go.mod @@ -79,5 +79,4 @@ go 1.17 replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni -replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop - +replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop diff --git a/go.sum b/go.sum index 6cf84a437..183dc4736 100644 --- a/go.sum +++ b/go.sum @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop h1:Satpn6ixt+mEZDxGOezlUgMivmTjqPBsKFS2Tp0GD6Q= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop h1:BhjSLUn9Bv6GPbDG5dk1lNUOf0M5+5+qJJdpiQRkm98= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= From 33321ac6b193fad52dc0297d02076f9fbba5c512 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 13 Sep 2024 17:08:58 -0300 Subject: [PATCH 06/15] Rename session of sponsored products --- services/external/weni/service.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 11bc8db63..ef0ef4a8e 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -110,7 +110,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log var productEntry flows.ProductEntry searchResult := []string{} var searchResultSponsored string - var allProductsSponsored []flows.ProductEntry var trace *httpx.Trace var traces []*httpx.Trace var sellerID string @@ -129,6 +128,13 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log sellerID = "1" } + allProductsSponsored := []flows.ProductEntry{ + { + Product: languages[params.Language], + ProductRetailerIDs: []string{}, + }, + } + for _, product := range productList { if params.SearchType == "default" { searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) @@ -163,7 +169,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } productRetailerIDMap = make(map[string]struct{}) - allProductsSponsored = append(allProductsSponsored, flows.ProductEntry{Product: product, ProductRetailerIDs: []string{searchResultSponsored}}) + allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored) } callResult.ProductRetailerIDS = productEntries @@ -183,7 +189,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult := &flows.MsgCatalogCall{} finalResult.Traces = callResult.Traces finalResult.ResponseJSON = callResult.ResponseJSON - + finalResult.ProductRetailerIDS = allProductsSponsored for i, productEntry := range callResult.ProductRetailerIDS { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} @@ -740,3 +746,9 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w return newProductEntryList, traces, nil } + +var languages = map[string]string{ + "eng": "You may also like:", + "por": "Você também pode gostar:", + "spa": "También te puede interesar:", +} From 174a85fce797bf7018942e4b540d4d358af7d847 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 20 Sep 2024 08:49:23 -0300 Subject: [PATCH 07/15] Only perform meta search if you have a sponsored product --- services/external/weni/service.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index ef0ef4a8e..7a083dd62 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -355,7 +355,8 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri } } if hasVtex { - resultSponsored, traces, err := VtexSponsoredSearch(searchUrl, productSearch) + resultSponsored, tracesAds, err := VtexSponsoredSearch(searchUrl, productSearch) + traces = append(traces, tracesAds...) if err != nil { return nil, productSponsored, traces, err } @@ -384,7 +385,6 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri var tracesMeta []*httpx.Trace for i := 0; i < retries; i++ { newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) - traces = append(traces, tracesMeta...) if err != nil { continue } @@ -393,6 +393,7 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri if len(newProductRetailerIDS[0].ProductRetailerIDs) > 0 { productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] + traces = append(traces, tracesMeta...) } } From 1c071b6ef295a6310ab710a40c2bcb12326fbf6c Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 20 Sep 2024 16:39:38 -0300 Subject: [PATCH 08/15] Fix adding sponsored products --- services/external/weni/service.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 7a083dd62..1aff83923 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -169,7 +169,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } productRetailerIDMap = make(map[string]struct{}) - allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored) + allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored+"#"+sellerID) } callResult.ProductRetailerIDS = productEntries @@ -189,14 +189,13 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult := &flows.MsgCatalogCall{} finalResult.Traces = callResult.Traces finalResult.ResponseJSON = callResult.ResponseJSON - finalResult.ProductRetailerIDS = allProductsSponsored - for i, productEntry := range callResult.ProductRetailerIDS { + if len(allProductsSponsored) > 0 { + finalResult.ProductRetailerIDS = allProductsSponsored + } + + for _, productEntry := range callResult.ProductRetailerIDS { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} - if allProductsSponsored[i].Product == newEntry.Product && len(allProductsSponsored[i].ProductRetailerIDs) > 0 && hasVtex { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, allProductsSponsored[i].ProductRetailerIDs[0]+"#"+sellerID) - qttProducts = 4 - } for _, productRetailerID := range productEntry.ProductRetailerIDs { if hasSimulation { for _, existingProductId := range existingProductsIds { From ab4508006b4d102c48727b5a6ee313eb675fca07 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 20 Sep 2024 19:25:03 -0300 Subject: [PATCH 09/15] Fix endpoint for vtex ads --- services/external/weni/service.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 1aff83923..a9d131e3f 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -517,9 +517,14 @@ func VtexSponsoredSearch(searchUrl string, productSearch string) ([]string, []*h query.Add("locale", "pt-BR") query.Add("hideUnavailableItems", "true") - urlAfter := strings.TrimSuffix(searchUrl, "/") + parsedURL, err := url.Parse(searchUrl) + if err != nil { + fmt.Println("Erro ao fazer parse da URL:", err) + return nil, nil, err + } + domain := parsedURL.Host - url_ := fmt.Sprintf("%s?%s", urlAfter, query.Encode()) + url_ := fmt.Sprintf("http://%s/api/io/_v/api/intelligent-search/sponsored_products?%s", domain, query.Encode()) req, err := httpx.NewRequest("GET", url_, nil, nil) if err != nil { From 32e8e92c5b05b5c2f55146e9ca4097fcb2fccb8a Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 20 Sep 2024 20:01:32 -0300 Subject: [PATCH 10/15] Fix error --- services/external/weni/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index a9d131e3f..c9f38dbac 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -390,7 +390,7 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri break } - if len(newProductRetailerIDS[0].ProductRetailerIDs) > 0 { + if len(newProductRetailerIDS) > 0 { productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] traces = append(traces, tracesMeta...) } From 4b4f1ae536e6ab5585bae29148bca82ac7df872e Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 20 Sep 2024 20:25:55 -0300 Subject: [PATCH 11/15] Fix meta search for sponsored products --- services/external/weni/service.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index c9f38dbac..e38ea6255 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -377,23 +377,23 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri } productEntries = append(productEntries, productEntry) productRetailerIDS = nil - } - retries := 2 - var newProductRetailerIDS []flows.ProductEntry - var tracesMeta []*httpx.Trace - for i := 0; i < retries; i++ { - newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) - if err != nil { - continue + retries := 2 + var newProductRetailerIDS []flows.ProductEntry + var tracesMeta []*httpx.Trace + for i := 0; i < retries; i++ { + newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(productEntries, fmt.Sprint(catalog), rt.WhatsappSystemUserToken) + if err != nil { + continue + } + break + } + if len(newProductRetailerIDS) > 0 { + productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] + traces = append(traces, tracesMeta...) } - break } - if len(newProductRetailerIDS) > 0 { - productSponsored = newProductRetailerIDS[0].ProductRetailerIDs[0] - traces = append(traces, tracesMeta...) - } } return result, productSponsored, traces, nil From 3b12bfa5c04fe5dddb3a9faadeff002bcbcebcef Mon Sep 17 00:00:00 2001 From: Robi9 Date: Tue, 24 Sep 2024 17:01:21 -0300 Subject: [PATCH 12/15] Check if there is a sponsored product --- services/external/weni/service.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index e38ea6255..11a5f3589 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -169,7 +169,10 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } productRetailerIDMap = make(map[string]struct{}) - allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored+"#"+sellerID) + if len(searchResultSponsored) > 0 { + allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored+"#"+sellerID) + } + } callResult.ProductRetailerIDS = productEntries From 45019a6f7d5c23130b8f64e11de859f2db1c797e Mon Sep 17 00:00:00 2001 From: Robi9 Date: Tue, 24 Sep 2024 18:25:16 -0300 Subject: [PATCH 13/15] Add hasSponsored for verification --- services/external/weni/service.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 11a5f3589..3b2c3d2da 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -135,6 +135,8 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log }, } + hasSponsored := false + for _, product := range productList { if params.SearchType == "default" { searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) @@ -170,6 +172,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log productRetailerIDMap = make(map[string]struct{}) if len(searchResultSponsored) > 0 { + hasSponsored = true allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored+"#"+sellerID) } @@ -192,7 +195,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult := &flows.MsgCatalogCall{} finalResult.Traces = callResult.Traces finalResult.ResponseJSON = callResult.ResponseJSON - if len(allProductsSponsored) > 0 { + if hasSponsored { finalResult.ProductRetailerIDS = allProductsSponsored } From 35125f4d5d7cf98a3926cfeec9eef0663f66c0a7 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Tue, 24 Sep 2024 19:11:52 -0300 Subject: [PATCH 14/15] Rename HasVtex to HasVtexAds --- core/models/assets.go | 4 ++-- core/models/org_context.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- services/external/weni/service.go | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/models/assets.go b/core/models/assets.go index ab0ad32de..1dadd413d 100644 --- a/core/models/assets.go +++ b/core/models/assets.go @@ -408,11 +408,11 @@ func NewOrgAssets(ctx context.Context, rt *runtime.Runtime, orgID OrgID, prev *O if prev == nil || refresh&RefreshContext > 0 { context := oa.org.ConfigValue("description", "") - hasVtex := oa.org.o.Config.Get("has_vtex", false) + hasVtexAds := oa.org.o.Config.Get("vtex_ads", false) c := &OrgContext{} c.c.OrgContext = context c.c.ProjectUUID = oa.org.ProjectUUID() - c.c.HasVtex = hasVtex.(bool) + c.c.HasVtexAds = hasVtexAds.(bool) oa.orgContexts = append(oa.orgContexts, c) if err != nil { return nil, errors.Wrapf(err, "error loading context for org %d", orgID) diff --git a/core/models/org_context.go b/core/models/org_context.go index dd9a25939..43b1f903c 100644 --- a/core/models/org_context.go +++ b/core/models/org_context.go @@ -48,14 +48,14 @@ type OrgContext struct { OrgContext string `json:"context"` ChannelUUID assets.ChannelUUID `json:"channel_uuid"` ProjectUUID uuids.UUID `json:"project_uuid"` - HasVtex bool `json:"has_vtex"` + HasVtexAds bool `json:"vtex_ads"` } } func (c *OrgContext) Context() string { return c.c.OrgContext } func (c *OrgContext) ChannelUUID() assets.ChannelUUID { return c.c.ChannelUUID } func (c *OrgContext) ProjectUUID() uuids.UUID { return c.c.ProjectUUID } -func (c *OrgContext) HasVtex() bool { return c.c.HasVtex } +func (c *OrgContext) HasVtexAds() bool { return c.c.HasVtexAds } type OrgContextService interface { flows.OrgContextService diff --git a/go.mod b/go.mod index eaf0efa2b..14d0bb547 100644 --- a/go.mod +++ b/go.mod @@ -79,4 +79,4 @@ go 1.17 replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni -replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop +replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop diff --git a/go.sum b/go.sum index 183dc4736..f4586296f 100644 --- a/go.sum +++ b/go.sum @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop h1:BhjSLUn9Bv6GPbDG5dk1lNUOf0M5+5+qJJdpiQRkm98= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-1-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop h1:pAFqCT01aWdpttU40VkOzwzQXT2FlEB/clPEHZPKhEs= +github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 3b2c3d2da..59a859e07 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -103,7 +103,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log return callResult, err } - hasVtex := params.HasVtex + hasVtexAds := params.HasVtexAds productRetailerIDS := []string{} productRetailerIDMap := make(map[string]struct{}) var productEntries []flows.ProductEntry @@ -142,7 +142,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) callResult.Traces = append(callResult.Traces, trace) } else if params.SearchType == "vtex" { - searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtex) + searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtexAds) callResult.Traces = append(callResult.Traces, traces...) allProducts = append(allProducts, searchResult...) if searchResult == nil { @@ -342,7 +342,7 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co return products["products"], trace, nil } -func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config, hasVtex bool) ([]string, string, []*httpx.Trace, error) { +func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config, hasVtexAds bool) ([]string, string, []*httpx.Trace, error) { var result []string var traces []*httpx.Trace var err error @@ -359,7 +359,7 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri return nil, productSponsored, traces, err } } - if hasVtex { + if hasVtexAds { resultSponsored, tracesAds, err := VtexSponsoredSearch(searchUrl, productSearch) traces = append(traces, tracesAds...) if err != nil { From 6d087f84937818fb86af870a1eb4e8fe7362cca0 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 26 Sep 2024 12:29:31 -0300 Subject: [PATCH 15/15] Update goflow to v1.0.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 14d0bb547..7b228c529 100644 --- a/go.mod +++ b/go.mod @@ -79,4 +79,4 @@ go 1.17 replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni -replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop +replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v1.0.0 diff --git a/go.sum b/go.sum index f4586296f..6211f130b 100644 --- a/go.sum +++ b/go.sum @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop h1:pAFqCT01aWdpttU40VkOzwzQXT2FlEB/clPEHZPKhEs= -github.com/weni-ai/goflow v0.15.0-goflow-0.144.3-ads-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= +github.com/weni-ai/goflow v1.0.0 h1:19Zm8owCQWoFb+fWUxiKpWyB+KysxwJzk7ZwNolSjlo= +github.com/weni-ai/goflow v1.0.0/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=