question

Kamron Clark avatar image
Kamron Clark asked Phong Vu commented

Set a custom user greeting using RingCentral SDK in Node.js

I'm trying to set a custom user greeting using the RingCentral SDK but every method I try gives me the error "unsupported media type". It would be very helpful if someone could provide an example of how to generate a request for this endpoint using the RingCentral SDK in Node.js. My current code is attempting to use another Node.js package to read file contents.

platform.on(platform.events.loginSuccess, function(e){
    console.log("User logged in successfully");
    set_greeting(get_file());
});

async function set_greeting(binary) {
    console.log(binary);
    try {
      var resp = await platform.post('/restapi/v1.0/account/~/extension/~/greeting', {
        'type': 'Introductory',
        'binary': binary
      });
      

      var jsonObj = await resp.json();
      console.log('Greeting set successfully.');
      console.log(jsonObj);
    } catch (e) {
      console.log(e.message);
    }
  }

async function get_file()
    let binaryData = fs.readFileSync('C:/Users/user/Desktop/RingCentralApps/project/example.mp3', "binary");
    
    return binaryData;
}
rest apicustom greeting
1 |3000

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 Phong Vu commented

Here you go for using the RingCentral JS SDK

async function create_greeting(){
  try{
    var FormData = require('form-data');
    formData = new FormData();
    var body = {
        type: "HoldMusic"
    }

    formData.append('json', new Buffer.from(JSON.stringify(body)), {
          filename: 'request.json',
          contentType: 'application/json'
          });

    formData.append('binary', require('fs').createReadStream('your-audio_file.mp3'));
    var resp = await platform.post('/restapi/v1.0/account/~/extension/~/greeting?apply=false', formData)
    var jsonObj = await resp.json()
    console.log(JSON.stringify(jsonObj))
  }catch(e){
    console.log(e.message)
  }
}
4 comments
1 |3000

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

Kamron Clark avatar image Kamron Clark commented ·

Thank you! Do you know of a type of a type of user greeting that will play the media as soon as the users call is answered? I'm trying to use the RingOut endpoint and have the custom greeting play as soon as the call is accepted. I've tried Introductory and Announcement and neither of them worked as intended.

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ Kamron Clark commented ·

I don't think that is possible with current greeting settings. In that case, you may have to implement your own SIP phone and down play the audio to the line. You can read this series blogs to learn more how to build a SIP phone.

https://medium.com/ringcentral-developers/pjsip-and-ringcentral-part-1-get-started-67df16b10956

https://medium.com/ringcentral-developers/pjsip-and-ringcentral-part-2-handle-audio-medias-c26b8ea61319

https://medium.com/ringcentral-developers/pjsip-and-ringcentral-part-3-dtmf-30b9cab12599

0 Likes 0 ·
Kamron Clark avatar image Kamron Clark Phong Vu ♦♦ commented ·

I think the custom greeting is giving me the behavior I desire at the moment. However, the create custom user greeting endpoint isn't overwriting the current greeting the user has. Is that the intended functionality, or is there something I'm missing? I'd like to use the endpoint to change the user's custom greeting to a new one.

0 Likes 0 ·
Show more comments

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