Skip to content

Commit

Permalink
Merge pull request #75 from communityconnectlabs/fix/attachment-with-…
Browse files Browse the repository at this point in the history
…text

feat: fetchMessage on media message sent
  • Loading branch information
teehamaral authored Jan 9, 2024
2 parents 26e3273 + 865649e commit 6874671
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
12 changes: 12 additions & 0 deletions services/tickets/twilioflex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ func (c *Client) FetchMedia(mediaSid string) (*Media, *httpx.Trace, error) {
return response, trace, err
}

// FetchMessage fetch a twilio flex Message by this sid.
func (c *Client) FetchMessage(messageSid string, channelSid string) (*ChatMessage, *httpx.Trace, error) {
fetchUrl := fmt.Sprintf("https://chat.twilio.com/v2/Services/%s/Channels/%s/Messages/%s", c.serviceSid, channelSid, messageSid)
response := &ChatMessage{}
data := url.Values{}
trace, err := c.get(fetchUrl, data, response)
if err != nil {
return nil, trace, err
}
return response, trace, err
}

// https://www.twilio.com/docs/chat/rest/user-resource#user-properties
type ChatUser struct {
AccountSid string `json:"account_sid,omitempty"`
Expand Down
42 changes: 42 additions & 0 deletions services/tickets/twilioflex/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,45 @@ func TestFetchMedia(t *testing.T) {
assert.Equal(t, "ME59b872f1e52fbd6fe6ad956bbb4fa9bd", response.Sid)
assert.Equal(t, "HTTP/1.0 200 OK\r\nContent-Length: 1342\r\n\r\n", string(trace.ResponseTrace))
}

func TestFetchMessage(t *testing.T) {
messageSid := "IM40d3a2b3404b4a10a1c914e8d780b3cc"
channelSid := "CHc73a3d9cf9f44afc8500ab01ce8ee0b9"
defer httpx.SetRequestor(httpx.DefaultRequestor)
httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
fmt.Sprintf("https://chat.twilio.com/v2/Services/IS83760ec293f6814bc6f5de5612f21fc2/Channels/%s/Messages/%s", channelSid, messageSid): {
httpx.MockConnectionError,
httpx.NewMockResponse(400, nil, `{"message": "Something went wrong", "detail": "Unknown", "code": 1234, "more_info": "https://www.twilio.com/docs/errors/1234"}`),
httpx.NewMockResponse(200, nil, `{
"sid": "IM40d3a2b3404b4a10a1c914e8d780b3cc",
"account_sid": "AC81d44315e19273181bdaefac12cd3c12",
"service_sid": "IS83760ec293f6814bc6f5de5612f21fc2",
"to": "CHc73a3d9cf9f44afc8500ab01ce8ee0b9",
"channel_sid": "CHc73a3d9cf9f44afc8500ab01ce8ee0b9",
"date_created": "2016-03-24T20:37:57Z",
"date_updated": "2016-03-24T20:37:57Z",
"last_updated_by": null,
"was_edited": false,
"from": "system",
"attributes": "{}",
"body": "Hello",
"index": 0,
"type": "text",
"media": null,
"url": "https://chat.twilio.com/v2/Services/IS83760ec293f6814bc6f5de5612f21fc2/Channels/CHc73a3d9cf9f44afc8500ab01ce8ee0b9/Messages/IM40d3a2b3404b4a10a1c914e8d780b3cc"
}`),
},
}))

client := twilioflex.NewClient(http.DefaultClient, nil, authToken, accountSid, serviceSid, workspaceSid, flexFlowSid)

_, _, err := client.FetchMessage(messageSid, channelSid)
assert.EqualError(t, err, "unable to connect to server")

_, _, err = client.FetchMessage(messageSid, channelSid)
assert.EqualError(t, err, "Something went wrong")

response, _, err := client.FetchMessage(messageSid, channelSid)
assert.NoError(t, err)
assert.Equal(t, "Hello", response.Body)
}
23 changes: 23 additions & 0 deletions services/tickets/twilioflex/testdata/event_callback.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@
"status": 200,
"body": "IMAGE"
}
],
"https://chat.twilio.com/v2/Services/IS83760ec293f6814bc6f5de5612f21fc2/Channels/CH1880a9cde40c4dbb88dd97fc3aedac08/Messages/IM8c57eaf105f34905883b1192e9499641": [
{
"status": 200,
"body": {
"sid": "IM40d3a2b3404b4a10a1c914e8d780b3cc",
"account_sid": "AC81d44315e19273181bdaefac12cd3c12",
"service_sid": "IS83760ec293f6814bc6f5de5612f21fc2",
"to": "CHc73a3d9cf9f44afc8500ab01ce8ee0b9",
"channel_sid": "CHc73a3d9cf9f44afc8500ab01ce8ee0b9",
"date_created": "2016-03-24T20:37:57Z",
"date_updated": "2016-03-24T20:37:57Z",
"last_updated_by": null,
"was_edited": false,
"from": "system",
"attributes": "{}",
"body": "Hello",
"index": 0,
"type": "text",
"media": null,
"url": "https://chat.twilio.com/v2/Services/IS83760ec293f6814bc6f5de5612f21fc2/Channels/CH1880a9cde40c4dbb88dd97fc3aedac08/Messages/IM8c57eaf105f34905883b1192e9499641"
}
}
]
},
"status": 200,
Expand Down
12 changes: 9 additions & 3 deletions services/tickets/twilioflex/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

"github.com/go-chi/chi"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/services/tickets"
"github.com/nyaruka/mailroom/web"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -95,10 +95,16 @@ func handleEventCallback(ctx context.Context, rt *runtime.Runtime, r *http.Reque
}
file, err := tickets.FetchFile(mediaContent.Links.ContentDirectTemporary, nil)
file.ContentType = mediaContent.ContentType

message, _, err := client.FetchMessage(request.MessageSid, request.ChannelSid)
if err != nil {
return err, http.StatusBadRequest, nil

Check warning on line 101 in services/tickets/twilioflex/web.go

View check run for this annotation

Codecov / codecov/patch

services/tickets/twilioflex/web.go#L101

Added line #L101 was not covered by tests
}

if err != nil {
return errors.Wrapf(err, "error fetching ticket file '%s'", mediaContent.Links.ContentDirectTemporary), http.StatusBadRequest, nil
}
_, err = tickets.SendReply(ctx, rt, ticket, request.Body, []*tickets.File{file})
_, err = tickets.SendReply(ctx, rt, ticket, message.Body, []*tickets.File{file})
if err != nil {
return err, http.StatusBadRequest, nil
}
Expand Down

0 comments on commit 6874671

Please sign in to comment.