question

Nick Montrond avatar image
Nick Montrond asked Nick Montrond commented

How to create post with attachments in a glip team using NodeJS Javascript SDK?

Hi Guys!

I'm trying to create a post with attachments using this Create Post api endpoint in the javascript SDK in NodeJS, but I can't seem to get it working. The endpoint looks like it only accepts attachment Ids but not sure where/how to get them as the files are not uploaded yet.

I tried using the same approach as the this create fax message endpoint but with no luck. I just get 'Error 415 Unsupported Media Type' and I'm trying to upload a PDF file which should be a standard accepted file type as I can upload it directly in Glip. BTW, at the moment I'm doing this in the sandbox, not sure if that is important.

Please any help to point me in the right direction would be much appreciated. If there is any more information that you guys need and think that may help please ask.

Thanks in advance.


Create Post API Endpoint: https://platform.devtest.ringcentral.com/restapi/v1.0/glip/chats/chatId/posts

apimessagejavascriptjavascriptsdknodejs
1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered Nick Montrond commented

Here is some snippet code

// Read your chat or team to get a chat or team id
async function get_glip_chat(){
  try{
    var endpoint = "/restapi/v1.0/glip/chats"
    var resp = await platform.get(endpoint)
    var json = await resp.json()
    for (var record of json.records){
      console.log("Chat name: " + record.name)
      console.log("Chat id: " + record.id)
      if (record.name == "A Chat Name"){
        uploadFile(record.id)
        break
      }
    }
  }catch(e){
    console.log(e)
  }
}
// upload your file
async function uploadFile(groupId){
  var endpoint = `/restapi/v1.0/glip/files?name=test.jpg&groupId=${groupId}`

  var FormData = require('form-data');
  formData = new FormData();

  formData.append('attachment', require('fs').createReadStream('test.jpg'));
  try {
    var resp = await platform.post(endpoint, formData)
    var jsonObj = await resp.json()
    postAttachment(groupId, jsonObj[0].id)
  }catch(e){
    console.log(e)
  }
}
// post text and attachment
function postAttachment(groupId, attachmentId){
  var endpoint = `/restapi/v1.0/glip/chats/${groupId}/posts`
  var params = {
    "text": "Hello there. My first image file",
    "attachments": [ {
      "id": attachmentId,
      "type": "File"
    } ]
  }
  platform.post(endpoint, params)
    .then(function(resp){
        var json = resp.json()
    })
    .catch(function(e){
        console.log(e)
    })
}

The known issue is that you will see 2 posts, one with just an attachment and the second one with text and attachment.

4 comments
1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thank you very much Phong! This was very helpful!

We were able to upload the attachment, the only weird behavior that we saw is that just by uploading a file, it showed up in the team as a post. We didn't have to create a post after uploading the file and then reference the attachment id. This is fine when the attachment doesn't have a text (the user didn't write a message when sending the attachment) but when there is a message/text and we create that post with the text/message and reference the attachmentID, then the file shows up on 2 posts (one without the message and one with the message). Is there somehow we can hide the post created when uploading a file?

0 Likes 0 ·

Sorry, I didn't notice that you pointed out that known issue. Any ideas on how to solve that?

0 Likes 0 ·

Currently, there is no solution for that. I did check with the platform team but still have not heard about a plan to resolve that.

0 Likes 0 ·

Understood. Thank you for your assistance on this matter! I will accept your answer as a solution.

0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys