Stream Audio into a Call

This flow streams an audio file into an outbound phone call.

There is a step-by-step tutorial on this, if you'd like to follow along.

Prerequisites

Before getting started, you’ll need a couple of things:

  • A Nexmo account - create one for free if you haven't already
  • A way to expose your server to the internet. This either means you're running a hosted version of Node-RED, or in case you're developing locally, using a tunneling service like ngrok - get up to speed with this Getting Started with Ngrok in Node-RED tutorial
[{"id":"74d62543.741fac","type":"tab","label":"Stream Audio into a Call","disabled":true,"info":""},{"id":"aa4bfebc.ee746","type":"ngrok","z":"74d62543.741fac","port":"1880","creds":"","region":"us","subdomain":"","name":"","x":430,"y":220,"wires":[["a108e107.e5804"]]},{"id":"c81249e2.d8e24","type":"inject","z":"74d62543.741fac","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["aa4bfebc.ee746"]]},{"id":"5239b018.da6058","type":"inject","z":"74d62543.741fac","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":240,"wires":[["aa4bfebc.ee746"]]},{"id":"a108e107.e5804","type":"debug","z":"74d62543.741fac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":220,"wires":[]},{"id":"67d1121a.f08c5c","type":"http in","z":"74d62543.741fac","name":"","url":"/event","method":"post","upload":false,"swaggerDoc":"","x":170,"y":400,"wires":[["a9888f77.4ed76","67b36a02.50552c"]]},{"id":"a9888f77.4ed76","type":"http response","z":"74d62543.741fac","name":"","statusCode":"","headers":{},"x":430,"y":400,"wires":[]},{"id":"67b36a02.50552c","type":"debug","z":"74d62543.741fac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":460,"wires":[]},{"id":"641fc020.889068","type":"http response","z":"74d62543.741fac","name":"","statusCode":"","headers":{},"x":750,"y":600,"wires":[]},{"id":"5c9bdb51.91e6bc","type":"http in","z":"74d62543.741fac","name":"","url":"/FILENAME.mp3","method":"get","upload":false,"swaggerDoc":"","x":200,"y":600,"wires":[["f0a20856.401c78"]]},{"id":"e59fa7cb.573cd8","type":"change","z":"74d62543.741fac","name":"Set Headers","rules":[{"t":"set","p":"headers","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"headers.content-type","pt":"msg","to":"audio/mp3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":600,"wires":[["641fc020.889068"]]},{"id":"f0a20856.401c78","type":"file in","z":"74d62543.741fac","name":"Music","filename":"ABSOLUTE_PATH_TO_FILENAME.mp3","format":"","chunk":false,"sendError":true,"encoding":"none","x":390,"y":600,"wires":[["e59fa7cb.573cd8"]]},{"id":"96ea794d.809fc8","type":"createcall","z":"74d62543.741fac","creds":"","endpoint":"phone","to":"{{msg.payload}}","dtmfanswer":"","wsuri":"","headers":"","sipuri":"","extension":"","from":"YOUR_NEXMO_NUMBER","ringingtimer":"","lengthtimer":"","machinedetection":"","answer":"","answertype":"fixed","eventurl":"https://YOUR_URL/event","x":570,"y":780,"wires":[["78d824f5.ed8df4"]]},{"id":"ba4168df.0a9d5","type":"stream","z":"74d62543.741fac","streamurl":"https://YOUR_URL/FILENAME.mp3","bargein":false,"loop":"","level":"","x":380,"y":780,"wires":[["96ea794d.809fc8"]]},{"id":"29fc9e0b.9654ba","type":"inject","z":"74d62543.741fac","name":"TO NUMBER","topic":"","payload":"RECIPIENT_PHONE_NUMBER","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":780,"wires":[["ba4168df.0a9d5"]]},{"id":"78d824f5.ed8df4","type":"debug","z":"74d62543.741fac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":780,"wires":[]},{"id":"38f44aad.7d2046","type":"comment","z":"74d62543.741fac","name":"📖 Check out the tutorial","info":"Go to the step-by-step [tutorial](https://www.nexmo.com/blog/2019/07/15/stream-audio-node-red-dr).","x":150,"y":40,"wires":[]},{"id":"b55c0281.d0813","type":"comment","z":"74d62543.741fac","name":"❓About This Path - Exposing Your Local Server to the Internet","info":"You'll need to [expose your local server to the internet](https://www.nexmo.com/blog/2019/07/03/ngrok-in-node-red-dr/), so that Nexmo can access it. If you’re running Node-RED on a public webserver instead of your local machine, you can skip this stage. \n\nOtherwise, a convenient way to do this is by using a tunneling service like [ngrok](https://ngrok.com).\n\nFirst, you'll need to install the ngrok node. To do so, open up _Manage palette_ from the hamburger menu, search for the `node-red-contrib-ngrok` package and click install. After restarting your editor, the **`ngrok`** node should appear in the node palette.\n\nOpen up the **`ngrok`** node properties and specify the port number. In case of Node-RED, the default value is `1880`. The default ngrok Region is US but you can also set it to Europe or Asia. You can also add your authtoken for your [ngrok account](https://ngrok.com) if you have one. Don't worry if you don't, just skip this step for now. The node will warn that it is not fully configured but this is not an issue.\n\nAnd you're all set! Once you hit deploy and click on the **on** `inject` node's button, navigate to the URL displayed in the debug sidebar (*YOUR_URL* for future reference) to find your Node-RED editor at a public address.","x":270,"y":120,"wires":[]},{"id":"af73847f.d55f38","type":"comment","z":"74d62543.741fac","name":"❓About This Path - Event Webhook","info":"If you'd like to receive events about the progress of your call, you can also setup an event webhook.\nThe `http` input node is connected to a `http response` node, as well as to a `debug` node, so that you can view your call events in the debug sidebar too.\n\nThe `http response` node should have `200` set as `Status code`, but don't worry about it, this is the default value as well.","x":180,"y":343,"wires":[]},{"id":"3c9a5a7e.168b4e","type":"comment","z":"74d62543.741fac","name":"❓About This Path - Serving Up the Audio File","info":"If you have the audio file somewhere online, make sure it is in either MP3 or WAV format, take note of the URL where it's hosted and continue with the next path. Feel free to delete the one below.\n\nAnother option is serving it up from your computer. To do this, you need an `http` input node, a `file in`, a `change` and an `http response` node. \n\n### **`http in`**\nIn the `http` input node, select `GET` as a `Method` and fill in the `URL` field with something like `/filename.mp3`, so that you can find your audio file at _YOUR-URL/filename.mp3_.\n### **`file in`**\nNext, open up the `file in` node properties, provide the absolute local path to the audio file you're serving up in the `Filename` field and set the `Output` to `a single Buffer object`. This will read the contents of the file as a binary buffer.\n### **`change`**\nYou also need to specify what type of file you're serving up, and that's where the `change` node comes into play. Set `msg.headers` to `{}` and `msg.headers.content-type` to `audio/mp3`.\n\nNow after hitting **Deploy**, point your browser at `YOUR_URL/filename.mp3` and you should hear your audio file playing.","x":210,"y":540,"wires":[]},{"id":"cbf5e22c.57fbe","type":"comment","z":"74d62543.741fac","name":"❓About This Path - Making the Outbound Call and Streaming the Audio File","info":"### **`inject`**\nYou can select `Number` from the `Payload` drop-down menu, and fill in the text field next to it with the phone number you wish to call in E.164 format. For example 447401234567. In this case, don't forget to reference this number using `{{msg.payload}}` in the **`createcall`** node properties as the value of `Number{}`.\n\n### **`stream`**\nOpen the node properties and set the `Stream URL {}` field to the link where your audio file is hosted -- `YOUR_URL/filename.mp3`. Note the `{}` sign next to the label, indicating that this value can be set dynamically, using [Mustache templating](https://mustache.github.io/). You can also set `Barge In`, `Loop`and `Level` values, although these are not required. See the [Stream section](https://developer.nexmo.com/voice/voice-api/ncco-reference#stream) of the NCCO reference to find out more.\n\n### **`createcall`**\nNext to the `Nexmo Credentials`, select \"Add new nexmovoiceapp...\" from the drop-down menu and click the edit button. Fill in the details below and click `Create New Application`.\n\n| KEY           | DESCRIPTION |\n| --------------- | --- |\n| `Name`     | Choose a name for your Voice Application, for example `Stream audio`.\n| `API Key`     | Your Nexmo API key, shown in your [account overview](https://dashboard.nexmo.com/getting-started-guide). |\n| `API Secret`  | Your Nexmo API secret, shown in your [account overview](https://dashboard.nexmo.com/getting-started-guide).    |\n| `Answer URL`     | The URL that Nexmo makes a request to when handling [inbound calls](https://www.nexmo.com/blog/2019/05/09/receive-phone-calls-node-red-dr/). In case you're only interested in making an outbound call, just use http://example.com - you won't be needing it.\n| `Event URL`     | Nexmo will send call events (e.g. ringing, answered) to this URL. If you’d like to receive events about the progress of your call, make sure your server is exposed to the internet, then use `YOUR_URL/event` for this field. Otherwise, feel free to use http://example.com - this will respond with 200 OK. You could also override this eventURL for a specific `createCall` node in its node properties. | \n\nNode-RED will then create a new Nexmo Application on your account and fill in the App ID and Private Key fields for you to save. Now you can find this application in your Nexmo Dashboard under _Voice_ > _[Your Applications](https://dashboard.nexmo.com/voice/your-applications)_.\n\nSelect `Phone` from the `Endpoint` drop-down menu. This will forward the call to a phone number, which you can specify in the text field next to the `Number{}` label. \nNote the `{}` sign, which means that [Mustache templating](https://mustache.github.io/) is supported for this field. You could hardcode a phone number in here, or pass it in dynamically using an **`inject`** node, and in this case, referencing it with `{{msg.payload}}`.\nNext, set one of your [virtual numbers](https://dashboard.nexmo.com/your-numbers) as the `From{}` number.","x":310,"y":725,"wires":[]}]

Flow Info

Created 5 years, 9 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • comment (x5)
  • debug (x3)
  • file in (x1)
  • http in (x2)
  • http response (x2)
  • inject (x3)
Other

Tags

  • nexmo
  • phone
  • calls
  • audio
  • streaming
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option