Skip to content

Commit

Permalink
Merge pull request #26 from green-api/SW-1775
Browse files Browse the repository at this point in the history
Fixed uploadFile
  • Loading branch information
Amele9 authored Jul 17, 2023
2 parents 672fcd5 + 20aaf66 commit 9306892
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README_RUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ import bodyParser from 'body-parser';

```

Полный список примеров [здесь](examples/).
Полный список примеров [здесь](examples).

## Разворачивание окружения разработки

Expand Down
10 changes: 5 additions & 5 deletions examples/createGroupAndSendMessage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const whatsAppClient = require('@green-api/whatsapp-api-client')
const whatsAppClient = require("@green-api/whatsapp-api-client");

const idInstance = "";
const apiTokenInstance = "";
const idInstance = "1101000001";
const apiTokenInstance = "d75b3a66374942c5b3c019c698abc2067e151558acbd412345";


(async () => {
const restAPI = whatsAppClient.restAPI(({
idInstance,
apiTokenInstance
idInstance, apiTokenInstance
}))

try {
const createGroupResponse = await restAPI.group.createGroup("Group Name", ["11001234567@c.us"], null)

Expand Down
28 changes: 28 additions & 0 deletions examples/uploadFileAndSendFileByURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const whatsAppClient = require("@green-api/whatsapp-api-client");

const idInstance = "1101000001";
const apiTokenInstance = "d75b3a66374942c5b3c019c698abc2067e151558acbd412345";

const filePath = "example.png";

(async () => {
const restAPI = whatsAppClient.restAPI(({
idInstance, apiTokenInstance
}))

try {
const uploadFileResponse = await restAPI.file.uploadFile(filePath)

console.log(uploadFileResponse)

try {
const sendFileByURLResponse = await restAPI.file.sendFileByUrl("11001234567@c.us", null, uploadFileResponse.urlFile, filePath)

console.log(sendFileByURLResponse)
} catch (error) {
console.error(error)
}
} catch (error) {
console.error(error)
}
})()
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"axios": "0.21.2",
"fs": "0.0.1-security",
"mime": "^3.0.0",
"rollup": "^2.79.1"
}
}
20 changes: 14 additions & 6 deletions src/utils/FileAPI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'
import axios from 'axios';
import CommonUtils from './CommonUtils.js'
import axios from "axios";
import fs from "fs";
import mime from "mime";

import CommonUtils from "./CommonUtils.js"

class FileAPI {

Expand Down Expand Up @@ -35,15 +38,20 @@ class FileAPI {
}

/**
* @param {FormData} formData
* @param {String} filePath
*/
async uploadFile(formData) {
async uploadFile(filePath) {
CommonUtils.validateString("filePath", filePath)

const method = "uploadFile";

const fileData = fs.readFileSync(filePath)

const response = await axios({
method: "post",
url: CommonUtils.generateMethodURL(this._restAPI.params, method),
data: formData,
headers: formData.getHeaders()
data: fileData,
headers: {"Content-Type": mime.getType(filePath)}
})
return response.data;
}
Expand Down

0 comments on commit 9306892

Please sign in to comment.