I am attempting to migrate the part of my app for creating a custom user greeting from using a local file to using a file publicly hosted online. Here is my current code:
async function create_greeting() { try { let FormData = require('form-data'); formData = new FormData(); let body = { type: "Introductory", answeringRule: {id: "3839201036"} } formData.append('json', new Buffer.from(JSON.stringify(body)), { filename: 'request.json', contentType: 'application/json' }); formData.append('binary', fs.createReadStream('URL_to_file')); let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData); let jsonObj = await resp.json() .then(() => console.log(JSON.stringify(jsonObj))); } catch(e) { console.log(e.message) } }
When I changed the file reference on line 15 from a local file path to a public URL I got the following response: "request to https://platform.ringcentral.com/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true failed, reason: socket hang up". How do I change the function to use a web-hosted file? Language is Node.js and I am using Google Cloud Storage to host the file.
Google does not allow accessing its Cloud Storage that way. Ask Google support or search on the stack overflow such as this one to learn how to download or read to a stream.
https://stackoverflow.com/questions/71097537/how-to-download-file-from-google-cloud-storage
After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:
- async function downloadIntoMemory() {
- // Downloads the file into a buffer in memory.
- const contents = await storage.bucket(bucketName).file(destFileName).download();
- console.log(
- `Contents have been downloaded`
- );
- //console.log(Buffer.from(contents.toString()).toString());
- create_greeting(contents.toString('binary'));
- }
- async function create_greeting(data) {
- try {
- let FormData = require('form-data');
- formData = new FormData();
- let body = {
- type: "Introductory",
- answeringRule: { id: "3839201036"}
- }
- formData.append('json', new Buffer.from(JSON.stringify(body)), {
- filename: 'request.json',
- contentType: 'application/json'
- });
- formData.append('binary', fs.createReadStream(data));
- let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
- let jsonObj = await resp.json()
- .then(() => console.log(JSON.stringify(jsonObj)));
- } catch(e) {
- console.log(e.message)
- }
- }
- downloadIntoMemory().catch(console.error);
When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes". How can I change the data buffer from the Cloud Storage download into an acceptable format?
I am not an expert on GC and I don't have an account to try it. But this may help
Is it possible to pass raw data into the request for the "binary" parameter? I've tried passing the audio as a Buffer, arrayBuffer, base64 string, Blob, and Blob string and none of them have worked. Almost all of them fail with the message "Invalid attachment media type". How come using the CreateReadStream() method on a local file works for passing the audio, but passing the binary of that file doesn't?
After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:
- async function downloadIntoMemory() {
- // Downloads the file into a buffer in memory.
- const contents = await storage.bucket(bucketName).file(destFileName).download();
- console.log(
- `Contents have been downloaded`
- );
- //console.log(Buffer.from(contents.toString()).toString());
- create_greeting(contents.toString('binary'));
- }
- async function create_greeting(data) {
- try {
- let FormData = require('form-data');
- formData = new FormData();
- let body = {
- type: "Introductory",
- answeringRule: { id: "3839201036"}
- }
- formData.append('json', new Buffer.from(JSON.stringify(body)), {
- filename: 'request.json',
- contentType: 'application/json'
- });
- formData.append('binary', fs.createReadStream(data));
- let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
- let jsonObj = await resp.json()
- .then(() => console.log(JSON.stringify(jsonObj)));
- } catch(e) {
- console.log(e.message)
- }
- }
- downloadIntoMemory().catch(console.error);
When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3\x04\x00\x00\x00\x00\x00\x1CTSSE\x00\x00\x00\b\x00\x00\x03Google\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00...". How can I change the data buffer from the Cloud Storage download into an acceptable format?
After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:
async function downloadIntoMemory() { // Downloads the file into a buffer in memory. const contents = await storage.bucket(bucketName).file(destFileName).download(); console.log( `Contents have been downloaded` ); //console.log(Buffer.from(contents.toString()).toString()); create_greeting(contents.toString('binary')); } async function create_greeting(data) { try { let FormData = require('form-data'); formData = new FormData(); let body = { type: "Introductory", answeringRule: {id: "3839201036"} } formData.append('json', new Buffer.from(JSON.stringify(body)), { filename: 'request.json', contentType: 'application/json' }); formData.append('binary', fs.createReadStream(data)); let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData); let jsonObj = await resp.json() .then(() => console.log(JSON.stringify(jsonObj))); } catch(e) { console.log(e.message) } } downloadIntoMemory().catch(console.error);
When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3\x04\x00\x00\x00\x00\x00\x1CTSSE\x00\x00\x00\b\x00\x00\x03Google\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00...". How can I change the data buffer from the Cloud Storage download into an acceptable format?
After reviewing the article and the Google Cloud Storage documentation, I can't get the FormData to accept the data downloaded into a buffer. I've tried the raw buffer data, converting to string, converting to binary, and I can't get it to accept the data format. For the purposes of this app, downloading to a file is not an option. Here is my current code:
async function downloadIntoMemory() { // Downloads the file into a buffer in memory. const contents = await storage.bucket(bucketName).file(destFileName).download(); console.log( `Contents have been downloaded` ); //console.log(Buffer.from(contents.toString()).toString()); create_greeting(contents.toString('binary')); } async function create_greeting(data) { try { let FormData = require('form-data'); formData = new FormData(); let body = { type: "Introductory", answeringRule: {id: "3839201036"} } formData.append('json', new Buffer.from(JSON.stringify(body)), { filename: 'request.json', contentType: 'application/json' }); formData.append('binary', data); let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData); let jsonObj = await resp.json() .then(() => console.log(JSON.stringify(jsonObj))); } catch(e) { console.log(e.message) } }
When I run this code, I get the error: "Invalid attachment media type". When I run the code but with fs.createReadStream(data) instead, I get this error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3\x04\x00\x00\x00\x00\x00\x1CTSSE\x00\x00\x00\b\x00\x00\x03Google\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00...". How can I get it to accept the data type without creating 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