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

Fixed uploadFile #26

Merged
merged 1 commit into from
Jul 17, 2023
Merged
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
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