Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Fix the marketting service

Acho Arnolda19fb0da0605
diff --git a/api/go.mod b/api/go.mod
index 9ee54702..2e86b4e7 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -18,7 +18,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/dgraph-io/ristretto v1.0.0
github.com/dustin/go-humanize v1.0.1
- github.com/getbrevo/brevo-go v1.1.3
github.com/gofiber/contrib/otelfiber v1.0.10
github.com/gofiber/fiber/v2 v2.52.8
github.com/gofiber/swagger v1.1.1
@@ -83,7 +82,6 @@ require (
github.com/PuerkitoBio/goquery v1.10.3 // indirect
github.com/andybalholm/brotli v1.2.0 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
- github.com/antihax/optional v1.0.0 // indirect
github.com/cenkalti/backoff/v5 v5.0.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect
diff --git a/api/go.sum b/api/go.sum
index 3b7f85bb..f6aefe8f 100644
--- a/api/go.sum
+++ b/api/go.sum
@@ -64,8 +64,6 @@ github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwTo
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
-github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
-github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
@@ -109,8 +107,6 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
-github.com/getbrevo/brevo-go v1.1.3 h1:8TYrhhxbfAJLGArlPzCDKzbNfzvjIykBRhTDzLJqmyw=
-github.com/getbrevo/brevo-go v1.1.3/go.mod h1:ExhytIoPxt/cOBl6ZEMeEZNLUKrWEYA5U3hM/8WP2bg=
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go
index 7fc94e67..2cfd447c 100644
--- a/api/pkg/di/container.go
+++ b/api/pkg/di/container.go
@@ -9,8 +9,6 @@ import (
"strconv"
"time"
- brevo "github.com/getbrevo/brevo-go/lib"
-
"github.com/pusher/pusher-http-go/v5"
"github.com/NdoleStudio/httpsms/docs"
@@ -427,21 +425,6 @@ func (container *Container) FirebaseAuthClient() (client *auth.Client) {
return authClient
}
-// BrevoClient creates a new instance of brevo.APIClient
-func (container *Container) BrevoClient() (client *brevo.APIClient) {
- container.logger.Debug(fmt.Sprintf("creating %T", client))
-
- apiKey := os.Getenv("BREVO_API_KEY")
- if apiKey == "" {
- container.logger.Info("BREVO_API_KEY environment variable is not set")
- return nil
- }
-
- cfg := brevo.NewConfiguration()
- cfg.AddDefaultHeader("api-key", apiKey)
- return brevo.NewAPIClient(cfg)
-}
-
// CloudTasksClient creates a new instance of cloudtasks.Client
func (container *Container) CloudTasksClient() (client *cloudtasks.Client) {
container.logger.Debug(fmt.Sprintf("creating %T", client))
@@ -919,7 +902,7 @@ func (container *Container) MarketingService() (service *services.MarketingServi
container.Logger(),
container.Tracer(),
container.FirebaseAuthClient(),
- container.BrevoClient(),
+ os.Getenv("BREVO_API_KEY"),
)
}
diff --git a/api/pkg/services/marketting_service.go b/api/pkg/services/marketting_service.go
index 28d1bb3c..56645dfd 100644
--- a/api/pkg/services/marketting_service.go
+++ b/api/pkg/services/marketting_service.go
@@ -5,10 +5,11 @@ import (
"fmt"
"strings"
+ "github.com/carlmjohnson/requests"
+
"firebase.google.com/go/auth"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
- brevo "github.com/getbrevo/brevo-go/lib"
"github.com/gofiber/fiber/v2"
"github.com/palantir/stacktrace"
)
@@ -18,7 +19,7 @@ type MarketingService struct {
logger telemetry.Logger
tracer telemetry.Tracer
authClient *auth.Client
- brevoClient *brevo.APIClient
+ brevoAPIKey string
}
// NewMarketingService creates a new instance of the MarketingService
@@ -26,13 +27,13 @@ func NewMarketingService(
logger telemetry.Logger,
tracer telemetry.Tracer,
authClient *auth.Client,
- brevoClient *brevo.APIClient,
+ brevoAPIKey string,
) *MarketingService {
return &MarketingService{
logger: logger.WithService(fmt.Sprintf("%T", &MarketingService{})),
tracer: tracer,
authClient: authClient,
- brevoClient: brevoClient,
+ brevoAPIKey: brevoAPIKey,
}
}
@@ -41,17 +42,16 @@ func (service *MarketingService) DeleteUser(ctx context.Context, userID entities
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
- if service.brevoClient == nil {
- ctxLogger.Warn(stacktrace.NewError("brevo client is not initialized, skipping adding user to list"))
- return
- }
-
- response, err := service.brevoClient.ContactsApi.DeleteContact(ctx, userID.String())
+ err := requests.URL(fmt.Sprintf("https://api.brevo.com/v3/contacts/%s?identifierType=ext_id", userID)).
+ Header("api-key", service.brevoAPIKey).
+ Delete().
+ CheckStatus(fiber.StatusNoContent).
+ Fetch(ctx)
if err != nil {
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot delete user with id [%s] from brevo list", userID)))
}
- ctxLogger.Info(fmt.Sprintf("deleted user with ID [%s] from brevo list with status [%s]", userID, response.Status))
+ ctxLogger.Info(fmt.Sprintf("deleted user with ID [%s] from brevo list with status [%s]", userID, fiber.StatusNoContent))
return nil
}
@@ -60,11 +60,6 @@ func (service *MarketingService) AddToList(ctx context.Context, user *entities.U
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
- if service.brevoClient == nil {
- ctxLogger.Warn(stacktrace.NewError("brevo client is not initialized, skipping adding user to list"))
- return
- }
-
userRecord, err := service.authClient.GetUser(ctx, string(user.ID))
if err != nil {
msg := fmt.Sprintf("cannot get auth user with id [%s]", user.ID)
@@ -72,20 +67,27 @@ func (service *MarketingService) AddToList(ctx context.Context, user *entities.U
return
}
- contact, _, err := service.brevoClient.ContactsApi.CreateContact(ctx, brevo.CreateContact{
- Email: userRecord.Email,
- Attributes: service.brevoAttributes(userRecord),
- ExtId: userRecord.UID,
- ListIds: []int64{9},
- UpdateEnabled: true,
- })
+ var response string
+ err = requests.URL("https://api.brevo.com/v3/contacts").
+ Header("api-key", service.brevoAPIKey).
+ Post().
+ BodyJSON(fiber.Map{
+ "email": userRecord.Email,
+ "ext_id": userRecord.UID,
+ "attributes": service.brevoAttributes(userRecord),
+ "listIds": []int64{9},
+ "updateEnabled": true,
+ }).
+ CheckStatus(fiber.StatusCreated, fiber.StatusNoContent).
+ ToString(&response).
+ Fetch(ctx)
if err != nil {
msg := fmt.Sprintf("cannot add user with id [%s] to brevo list", user.ID)
ctxLogger.Error(stacktrace.Propagate(err, msg))
return
}
- ctxLogger.Info(fmt.Sprintf("user [%s] added to list brevo list with brevo ID [%d]", user.ID, contact.Id))
+ ctxLogger.Info(fmt.Sprintf("user [%s] added to list brevo list with brevo response [%s]", user.ID, response))
}
func (service *MarketingService) brevoAttributes(user *auth.UserRecord) map[string]any {
@@ -96,11 +98,11 @@ func (service *MarketingService) brevoAttributes(user *auth.UserRecord) map[stri
parts := strings.Split(name, " ")
if len(parts) == 1 {
- return fiber.Map{"FNAME": name}
+ return fiber.Map{"FIRSTNAME": name}
}
return fiber.Map{
- "FNAME": strings.Join(parts[0:len(parts)-1], " "),
- "LNAME": parts[len(parts)-1],
+ "FIRSTNAME": strings.Join(parts[0:len(parts)-1], " "),
+ "LASTNAME": parts[len(parts)-1],
}
}