RocketChat / Rocket.Chat.js.SDK

Utility for apps and bots to interact with Rocket.Chat via DDP and/or API
MIT License
136 stars 95 forks source link

How to upload files? #22

Closed xvilo closed 2 years ago

xvilo commented 6 years ago

Is it possible to upload files through the SDK? Couldn't find any related methods

timkinnane commented 6 years ago

Hi @xvilo - You can use the SDK to send custom message objects via driver.sendMessage() (see the Message interface).

So you can add URL for attachments, but to actually "upload" like a user dragging a file into the client, would require the server the SDK is running on to host and stream a file to the server. That's theoretically possible but not a common use case so I don't think we'll provide helpers. You could still use the SDK and driver.asyncCall() to make a custom server method call if you can figure out the streaming requirements.

djlazz3 commented 4 years ago

just a note, i was able to get that "upload" functionality with just driver.sendMessage() like so...

return driver.sendMessage({
  msg: "Message goes here",
  rid: 'RoomId goes here',
  attachments: [{
    fields: [{
      value: "File to upload"
    }],
    text: "File description goes here"
  }]
})

this helped me figure this out: https://github.com/RocketChat/Rocket.Chat.js.SDK/blob/e507e86faf049751218a08f6488545ec77d33309/src/config/messageInterfaces.ts#L3

solanha1999 commented 3 years ago

just a note, i was able to get that "upload" functionality with just driver.sendMessage() like so...

return driver.sendMessage({
  msg: "Message goes here",
  rid: 'RoomId goes here',
  attachments: [{
    fields: [{
      value: "File to upload"
    }],
    text: "File description goes here"
  }]
})

this helped me figure this out: https://github.com/RocketChat/Rocket.Chat.js.SDK/blob/e507e86faf049751218a08f6488545ec77d33309/src/config/messageInterfaces.ts#L3

Hi @djlazz3 I wanna know how a get the file to upload I tryed to put the directory path to file, but it didn't work, it sent its own string path. Could you help me please?

felipetomm commented 3 years ago

@solanha1999 , you could make the upload with api/v1/rooms.upload/:rid. In the same request, you can send an message, or, after this, you have the path of file hosted on server, and you can send an message separately.

The attachments prop is only an path to an file. The path of the file can be a external and acessible link or an "internal" file on server.

solanha1999 commented 3 years ago

@felipetomm Thanks thats great, but i dont know how do i do that (https://developer.rocket.chat/api/rest-api/endpoints/rooms/upload#example-call) in nodeJS i use the curl conversor (https://onlinedevtools.in/curl), but it don't work. Do u know what i can do? I thank you in advance!

felipetomm commented 3 years ago

@solanha1999, the code converter result that's incomplete, in node you need to use something like a form-data. Can you see more here, has an example of upload local file to an POST API using form-data. I use this method and is functionally. I hope that you get it :)

solanha1999 commented 3 years ago

Sorry @felipetomm, but It's not working for me here's my code: image do u know what I'm doing wrong?

felipetomm commented 3 years ago

In my case i use axios to make a post. I think that you need to specify form-data in headers, something like this:

const FormData = require('form-data');
let form = new FormData();

form.append('file', fs.createReadStream('PATH_OF_FILE'), 'NameOfFile.pdf');
const config = {
    headers: {
        'Content-Type': 'multipart/form-data; boundary=' + form.getBoundary(),
        'X-Auth-Token': authToken,
        'X-User-Id': userId
    }
}
await axios.post(`${ROCKET_CHAT_HOST}/api/v1/rooms.upload/${roomId}`, form, config);

Try this, and if is possible, post the status code or result from your try

solanha1999 commented 3 years ago

@felipetomm thanks, my code works very well, i just have to add the Room or User ID to work, so now i trying to get the User and Room ID by the function getDirectMessageRoomId and getRoomId, that returns a [object Promise]

felipetomm commented 3 years ago

if return a [object Promise], use await before call method:


// you have
driver.getDirectMessageRoomId('something_here')

// you need
await driver.getDirectMessageRoomId('something_here')

// or then
driver.getDirectMessageRoomId('something_here')
 .then((result) => {
     // do something
})
solanha1999 commented 3 years ago

Thank you!!!! That's work very well!!!