Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/weni-ai/courier into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobernardoaf committed Oct 17, 2024
2 parents 4b9aea8 + fe8dde6 commit 7d0f75c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.16.0
----------
* Upload documents to telegram
* Feat: WhatsApp order details

1.15.0
----------
* Add button support with url in WA
Expand Down
6 changes: 3 additions & 3 deletions handlers/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ func sendUploadDocument(msg courier.Msg, token string, mediaURL string, fileName
return "", nil, err
}

url := fmt.Sprintf(apiURL, token)
req, err := http.NewRequest("POST", url, body)
sendURL := fmt.Sprintf("%s/bot%s/%s", apiURL, token, "sendDocument")
req, err := http.NewRequest("POST", sendURL, body)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -510,7 +510,7 @@ func downloadFileToBytes(fileURL string) ([]byte, error) {

resp, err := utils.MakeHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("erro ao baixar o arquivo: %v", err)
return nil, fmt.Errorf("error downloading file: %v", err)
}

return resp.Body, nil
Expand Down
32 changes: 24 additions & 8 deletions handlers/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,6 @@ var defaultSendTestCases = []ChannelSendTestCase{
Attachments: []string{"application/pdf:https://foo.bar/doc1.pdf", "application/pdf:https://foo.bar/document.pdf"},
Status: "W", ExternalID: "133",
ResponseBody: `{ "ok": true, "result": { "message_id": 133 } }`, ResponseStatus: 200,
PostParams: map[string]string{
"chat_id": "12345",
"document": "https://foo.bar/document.pdf",
"reply_markup": `{"keyboard":[[{"text":"Yes"},{"text":"No"}]],"resize_keyboard":true,"one_time_keyboard":true}`,
},
SendPrep: setSendURL},
{Label: "Quick Reply with Slashes",
Text: "Are you happy?", URN: "telegram:12345", QuickReplies: []string{"\\\\Yes", "/No"},
Expand Down Expand Up @@ -640,8 +635,7 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "My document!", URN: "telegram:12345", Attachments: []string{"application/pdf:https://foo.bar/document.pdf"},
Status: "W",
ResponseBody: `{ "ok": true, "result": { "message_id": 133 } }`, ResponseStatus: 200,
PostParams: map[string]string{"caption": "My document!", "chat_id": "12345", "document": "https://foo.bar/document.pdf"},
SendPrep: setSendURL},
SendPrep: setSendURL},
{Label: "Unknown Attachment",
Text: "My pic!", URN: "telegram:12345", Attachments: []string{"unknown/foo:https://foo.bar/unknown.foo"},
Status: "E",
Expand All @@ -663,11 +657,33 @@ var parseModeTestCases = []ChannelSendTestCase{
SendPrep: setSendURL},
}

func buildMockDownloadFile(testCases []ChannelSendTestCase) (*httptest.Server, []ChannelSendTestCase) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("%PDF-1.4\n%mocked PDF content\n%%EOF"))
}))

apiURL = server.URL

// update our tests media urls
for i := range testCases {
for j := range testCases[i].Attachments {
if testCases[i].Attachments != nil && strings.HasPrefix(testCases[i].Attachments[j], "application/pdf") {
filename := strings.Split(testCases[i].Attachments[j], "/")
testCases[i].Attachments[j] = (fmt.Sprintf("application/pdf:%s/%s", apiURL, filename[len(filename)-1]))
}
}
}
return server, testCases
}

func TestSending(t *testing.T) {
var defaultChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TG", "2020", "US",
map[string]interface{}{courier.ConfigAuthToken: "auth_token"})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
server, testCases := buildMockDownloadFile(defaultSendTestCases)
defer server.Close()

RunChannelSendTestCases(t, defaultChannel, newHandler(), testCases, nil)

var parseModeChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TG", "2020", "US",
map[string]interface{}{courier.ConfigAuthToken: "auth_token"})
Expand Down

0 comments on commit 7d0f75c

Please sign in to comment.