News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
How to create post with attachments in a glip team using NodeJS Javascript SDK?
Tags: sdk
Jun 23, 2021 at 9:23pm   •   1 replies  •  0 likes
Nick Montrond

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

1 Answer
answered on Jun 28, 2021 at 5:22pm  

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
answered on Jun 25, 2021 at 12:12pm  

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.


 0
on Jun 28, 2021 at 5:26pm   •  0 likes

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

on Jun 29, 2021 at 9:21am   •  0 likes

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.

on Jun 30, 2021 at 9:25am   •  0 likes

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

on Jun 28, 2021 at 5:23pm   •  0 likes

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?



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us