Skip to content

Commit

Permalink
Merge branch 'staging' of https://github.com/weni-ai/courier into sta…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
paulobernardoaf committed Nov 27, 2024
2 parents 5637143 + 8a276de commit 6896274
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 52 deletions.
23 changes: 23 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
1.18.5
----------
* Fix: document name on template message for whatsapp cloud

1.18.4
----------
* Fix: Handle error for Whatsapp messages without text but with quick replies and attachments

1.18.3
----------
* Hotfix: MsgParts index out of range

1.18.2
----------
* Feat: configuration for billing queue name on env var

1.18.1
----------
* Fix: document name when message have quick replies on whatsapp handler
* Fix: document name when message have quick replies on facebookapp handler for whatsapp cloud api
* Fix: backslashes for headerText, footer and ctaMessage
* Fix: handle empty flow data to not be sent as empty object in request

1.18.0
----------
* Search for contact and update a Teams contact with the serviceURL
Expand Down
41 changes: 41 additions & 0 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,3 +1020,44 @@ func (m *DBMsg) OrderDetailsMessage() *courier.OrderDetailsMessage {

return nil
}

func (m *DBMsg) Buttons() []courier.ButtonComponent {
if m.Metadata_ == nil {
return nil
}

var metadata map[string]interface{}
err := json.Unmarshal(m.Metadata_, &metadata)
if err != nil {
return nil
}

if metadata == nil {
return nil
}

if buttonsData, ok := metadata["buttons"].([]interface{}); ok {
buttons := make([]courier.ButtonComponent, len(buttonsData))
for i, button := range buttonsData {
buttonMap := button.(map[string]interface{})
buttons[i] = courier.ButtonComponent{
SubType: buttonMap["sub_type"].(string),
Parameters: []courier.ButtonParam{},
}

if buttonMap["parameters"] != nil {
parameters := buttonMap["parameters"].([]interface{})
for _, parameter := range parameters {
parameterMap := parameter.(map[string]interface{})
buttons[i].Parameters = append(buttons[i].Parameters, courier.ButtonParam{
Type: parameterMap["type"].(string),
Text: parameterMap["text"].(string),
})
}
}
}
return buttons
}

return nil
}
10 changes: 5 additions & 5 deletions billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/sirupsen/logrus"
)

const QUEUE_NAME = "billing-backup"

// Message represents a object that is sent to the billing service
//
// {
Expand Down Expand Up @@ -61,10 +59,11 @@ type Client interface {
type rabbitmqRetryClient struct {
publisher rabbitroutine.Publisher
conn *rabbitroutine.Connector
queueName string
}

// NewRMQBillingResilientClient creates a new billing service client implementation using RabbitMQ with publish retry and reconnect features
func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int) (Client, error) {
func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int, queueName string) (Client, error) {
cconn, err := amqp.Dial(url)
if err != nil {
return nil, err
Expand All @@ -77,7 +76,7 @@ func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int)
}
defer ch.Close()
_, err = ch.QueueDeclare(
QUEUE_NAME,
queueName,
false,
false,
false,
Expand Down Expand Up @@ -126,6 +125,7 @@ func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int)
return &rabbitmqRetryClient{
publisher: pub,
conn: conn,
queueName: queueName,
}, nil
}

Expand All @@ -135,7 +135,7 @@ func (c *rabbitmqRetryClient) Send(msg Message) error {
err := c.publisher.Publish(
ctx,
"",
QUEUE_NAME,
c.queueName,
amqp.Publishing{
ContentType: "application/json",
Body: msgMarshalled,
Expand Down
22 changes: 12 additions & 10 deletions billing/billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/stretchr/testify/assert"
)

const billingTestQueueName = "testqueue"

func TestInitialization(t *testing.T) {
connURL := "amqp://localhost:5672/"
conn, err := amqp.Dial(connURL)
Expand All @@ -25,7 +27,7 @@ func TestInitialization(t *testing.T) {
}
defer conn.Close()
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)
}

func TestBillingResilientClient(t *testing.T) {
Expand All @@ -37,7 +39,7 @@ func TestBillingResilientClient(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -53,14 +55,14 @@ func TestBillingResilientClient(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
err = billingClient.Send(msg)
assert.NoError(t, err)

msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
true,
false,
Expand Down Expand Up @@ -101,7 +103,7 @@ func TestBillingResilientClientSendAsync(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -117,14 +119,14 @@ func TestBillingResilientClientSendAsync(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
billingClient.SendAsync(msg, nil, nil)

assert.NoError(t, err)
msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
true,
false,
Expand Down Expand Up @@ -165,7 +167,7 @@ func TestBillingResilientClientSendAsyncWithPanic(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -181,15 +183,15 @@ func TestBillingResilientClientSendAsyncWithPanic(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
time.Sleep(1 * time.Second)
billingClient.SendAsync(msg, nil, func() { panic("test panic") })

assert.NoError(t, err)
msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
false,
false,
Expand Down
2 changes: 1 addition & 1 deletion cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {

if config.RabbitmqURL != "" {
billingClient, err := billing.NewRMQBillingResilientClient(
config.RabbitmqURL, config.RabbitmqRetryPubAttempts, config.RabbitmqRetryPubDelay)
config.RabbitmqURL, config.RabbitmqRetryPubAttempts, config.RabbitmqRetryPubDelay, config.BillingQueueName)
if err != nil {
logrus.Fatalf("Error creating billing RabbitMQ client: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
RabbitmqURL string `help:"rabbitmq url"`
RabbitmqRetryPubAttempts int `help:"rabbitmq retry attempts"`
RabbitmqRetryPubDelay int `help:"rabbitmq retry delay"`
BillingQueueName string `help:"billing queue name"`

EmailProxyURL string `help:"email proxy url"`
EmailProxyAuthToken string `help:"email proxy auth token"`
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewConfig() *Config {
WaitMediaChannels: []string{},
RabbitmqRetryPubAttempts: 3,
RabbitmqRetryPubDelay: 1000,
BillingQueueName: "billing-backup",
EmailProxyURL: "http://localhost:9090",
EmailProxyAuthToken: "",
}
Expand Down
1 change: 1 addition & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *dummyHandler) Initialize(s Server) error {
"amqp://localhost:5672/",
3,
100,
s.Config().BillingQueueName,
)
if err != nil {
logrus.Fatalf("Error creating billing RabbitMQ client: %v", err)
Expand Down
Loading

0 comments on commit 6896274

Please sign in to comment.