Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send msg metadata with context to wenichats #229

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/models/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"net/http"
"time"

Expand Down Expand Up @@ -152,7 +153,7 @@ func (t *Ticket) FlowTicket(oa *OrgAssets) (*flows.Ticket, error) {
}

// ForwardIncoming forwards an incoming message from a contact to this ticket
func (t *Ticket) ForwardIncoming(ctx context.Context, rt *runtime.Runtime, org *OrgAssets, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment) error {
func (t *Ticket) ForwardIncoming(ctx context.Context, rt *runtime.Runtime, org *OrgAssets, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage) error {
ticketer := org.TicketerByID(t.t.TicketerID)
if ticketer == nil {
return errors.Errorf("can't find ticketer with id %d", t.t.TicketerID)
Expand All @@ -164,7 +165,7 @@ func (t *Ticket) ForwardIncoming(ctx context.Context, rt *runtime.Runtime, org *
}

logger := &HTTPLogger{}
err = service.Forward(t, msgUUID, text, attachments, logger.Ticketer(ticketer))
err = service.Forward(t, msgUUID, text, attachments, metadata, logger.Ticketer(ticketer))

logger.Insert(ctx, rt.DB)

Expand Down Expand Up @@ -741,7 +742,7 @@ func (t *Ticketer) UpdateConfig(ctx context.Context, db Queryer, add map[string]
type TicketService interface {
flows.TicketService

Forward(*Ticket, flows.MsgUUID, string, []utils.Attachment, flows.HTTPLogCallback) error
Forward(*Ticket, flows.MsgUUID, string, []utils.Attachment, json.RawMessage, flows.HTTPLogCallback) error
Close([]*Ticket, flows.HTTPLogCallback) error
Reopen([]*Ticket, flows.HTTPLogCallback) error
}
Expand Down
2 changes: 1 addition & 1 deletion core/tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
return errors.Wrapf(err, "unable to look up open tickets for contact")
}
for _, ticket := range tickets {
ticket.ForwardIncoming(ctx, rt, oa, event.MsgUUID, event.Text, event.Attachments)
ticket.ForwardIncoming(ctx, rt, oa, event.MsgUUID, event.Text, event.Attachments, event.Metadata)
}

// find any matching triggers
Expand Down
3 changes: 2 additions & 1 deletion services/tickets/intern/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package intern

import (
"encoding/json"
"net/http"

"github.com/nyaruka/gocommon/httpx"
Expand Down Expand Up @@ -33,7 +34,7 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
}

// Forward is a noop
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
return nil
}

Expand Down
1 change: 1 addition & 0 deletions services/tickets/intern/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestOpenAndForward(t *testing.T) {
flows.MsgUUID("ca5607f0-cba8-4c94-9cd5-c4fbc24aa767"),
"It's urgent",
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/media/0123/attachment1.jpg")},
nil,
logger.Log,
)

Expand Down
3 changes: 2 additions & 1 deletion services/tickets/mailgun/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mailgun

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return ticket, nil
}

func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
context := s.templateContext(ticket.Body(), text, ticket.Config(ticketConfigContactUUID), ticket.Config(ticketConfigContactDisplay))
body := evaluateTemplate(forwardBodyTemplate, context)

Expand Down
1 change: 1 addition & 0 deletions services/tickets/mailgun/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestOpenAndForward(t *testing.T) {
flows.MsgUUID("ca5607f0-cba8-4c94-9cd5-c4fbc24aa767"),
"It's urgent",
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/media/0123/attachment1.jpg")},
nil,
logger.Log,
)

Expand Down
3 changes: 2 additions & 1 deletion services/tickets/rocketchat/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rocketchat

import (
"encoding/json"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -106,7 +107,7 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return ticket, nil
}

func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
visitor := Visitor{
Token: VisitorToken(ticket.ContactID()).String(),
}
Expand Down
4 changes: 2 additions & 2 deletions services/tickets/rocketchat/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestOpenAndForward(t *testing.T) {
"contact-display": "Cathy",
})
logger = &flows.HTTPLogger{}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, logger.Log)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, nil, logger.Log)
assert.EqualError(t, err, "error calling RocketChat: unable to connect to server")

logger = &flows.HTTPLogger{}
Expand All @@ -102,7 +102,7 @@ func TestOpenAndForward(t *testing.T) {
"video/mp4:https://link.to/video.mp4",
"audio/ogg:https://link.to/audio.ogg",
}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, logger.Log)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, nil, logger.Log)
require.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))
test.AssertSnapshot(t, "forward_message", logger.Logs[0].Request)
Expand Down
2 changes: 1 addition & 1 deletion services/tickets/twilioflex/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return ticket, nil
}

func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
identity := fmt.Sprint(ticket.ContactID())

if len(attachments) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions services/tickets/twilioflex/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ func TestOpenAndForward(t *testing.T) {
"contact-display": "Cathy",
})
logger = &flows.HTTPLogger{}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, logger.Log)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, nil, logger.Log)
assert.EqualError(t, err, "error calling Twilio: unable to connect to server")

logger = &flows.HTTPLogger{}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, logger.Log)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, nil, logger.Log)
assert.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))
test.AssertSnapshot(t, "forward_message", logger.Logs[0].Request)
Expand All @@ -390,7 +390,7 @@ func TestOpenAndForward(t *testing.T) {
"video/mp4:https://link.to/dummy_video.mp4",
"audio/ogg:https://link.to/dummy_audio.ogg",
}
err = svc.Forward(dbTicket2, flows.MsgUUID("5ga340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, logger.Log)
err = svc.Forward(dbTicket2, flows.MsgUUID("5ga340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, nil, logger.Log)
assert.NoError(t, err)
assert.Equal(t, 7, len(logger.Logs))
}
Expand Down
11 changes: 6 additions & 5 deletions services/tickets/wenichats/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ type RoomResponse struct {
}

type MessageRequest struct {
Room string `json:"room"`
Text string `json:"text"`
CreatedOn time.Time `json:"created_on"`
Direction string `json:"direction"`
Attachments []Attachment `json:"attachments"`
Room string `json:"room"`
Text string `json:"text"`
CreatedOn time.Time `json:"created_on"`
Direction string `json:"direction"`
Attachments []Attachment `json:"attachments"`
Metadata json.RawMessage `json:"metadata,omitempty"`
}

type MessageResponse struct {
Expand Down
4 changes: 3 additions & 1 deletion services/tickets/wenichats/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wenichats

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -257,14 +258,15 @@ func parseMsgAttachments(atts []utils.Attachment) []Attachment {
return msgAtts
}

func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
roomUUID := string(ticket.ExternalID())

msg := &MessageRequest{
Room: roomUUID,
Attachments: []Attachment{},
Direction: "incoming",
CreatedOn: dates.Now(),
Metadata: metadata,
}

if len(attachments) != 0 {
Expand Down
8 changes: 5 additions & 3 deletions services/tickets/wenichats/service_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wenichats_test

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -381,11 +382,12 @@ func TestOpenAndForward(t *testing.T) {
"contact-display": "Cathy",
})
logger = &flows.HTTPLogger{}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, logger.Log)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, nil, logger.Log)
assert.EqualError(t, err, "error send message to wenichats: unable to connect to server")

logger = &flows.HTTPLogger{}
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, logger.Log)
metadata := json.RawMessage(`{"context":{"from": "12345","id": "98765"}}`)
err = svc.Forward(dbTicket, flows.MsgUUID("4fa340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", nil, metadata, logger.Log)
assert.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))
test.AssertSnapshot(t, "forward_message", logger.Logs[0].Request)
Expand All @@ -401,7 +403,7 @@ func TestOpenAndForward(t *testing.T) {
"video/mp4:https://link.to/dummy_video.mp4",
"audio/ogg:https://link.to/dummy_audio.ogg",
}
err = svc.Forward(dbTicket2, flows.MsgUUID("5ga340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, logger.Log)
err = svc.Forward(dbTicket2, flows.MsgUUID("5ga340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, nil, logger.Log)
assert.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
POST /v1/external/msgs/ HTTP/1.1
Host: chats-engine.dev.cloud.weni.ai
User-Agent: Go-http-client/1.1
Content-Length: 144
Content-Length: 197
Authorization: Bearer ****************
Content-Type: application/json
Accept-Encoding: gzip

{"room":"8ecb1e4a-b457-4645-a161-e2b02ddffa88","text":"It's urgent","created_on":"2019-10-07T15:22:42Z","direction":"incoming","attachments":[]}
{"room":"8ecb1e4a-b457-4645-a161-e2b02ddffa88","text":"It's urgent","created_on":"2019-10-07T15:22:42Z","direction":"incoming","attachments":[],"metadata":{"context":{"from":"12345","id":"98765"}}}
3 changes: 2 additions & 1 deletion services/tickets/zendesk/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zendesk

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -177,7 +178,7 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return ticket, nil
}

func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, metadata json.RawMessage, logHTTP flows.HTTPLogCallback) error {
contactUUID := ticket.Config("contact-uuid")
contactDisplay := ticket.Config("contact-display")

Expand Down
1 change: 1 addition & 0 deletions services/tickets/zendesk/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestOpenAndForward(t *testing.T) {
flows.MsgUUID("ca5607f0-cba8-4c94-9cd5-c4fbc24aa767"),
"It's urgent",
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/media/0123/attachment1.jpg")},
nil,
logger.Log,
)

Expand Down
Loading