Reading Notifications from Dynamics 365

Reading Notifications from Dynamics 365 using a Raspberry Pi

In my two previous howto articles I gave examples of how to turn IoT to AIoT, using Azure AI cognitive services, to do both face detection and speak in a human like voice. In this article and the next ones, I’m going to show you how to turn your Raspberry Pi into an Alexa like device.

You see, wouldn’t it be nice to have voice notifications from Raspberry Pi about your Dynamics 365 leads, opportunities and orders? In a similar fashion to how Alexa notify you of your Amazon Prime orders.

In this howtwo, you will be connecting your Raspberry Pi to Dynamics 365 CDS WebAPI, get latest updates using REST calls and then announce it over a speaker via Raspberry Pi. Hope you enjoy it, let’s start.

Setting Up Raspberry Pi and Azure Speech

First thing first, you will need to set up you Raspberry Pi, so you will need to follow the instruction on the first article, then you will need to set up Azure Speech API as detailed on the second article. When done you will need a Dynamics 365 environment, if you don’t have one already you might want to sign up for trial.

For simplicity, we are going to notify end users of the latest lead created on Dynamics 365. In order to query the CDS WebAPI, where leads data is stored, you will need to obtain an authorization token. You can do so by following the steps on this Microsoft documenation.

Testing Raspberry Pi Connection to Dynamics 365

To test your access to Dynamics 365 via Web API, replace the organization URL and authorization token placeholders with your values. Save it as cds.js and run it on your own machine using this command.

npm install request

node cds.js

var request = require('request');
//this is a get request to your Dynamics 365 WebAPI to get latet lead
var options = {
  'method': 'GET',
//populate your organization url here
  'url': 'https://<orgurl>/api/data/v9.0/leads?$orderby=createdon desc&amp;$select=subject&amp;$top=1',
  'headers': {
    'Authorization': 'Bearer <Token>'  //populate the token here
request(options, function (error, response) { 
  if (error) throw new Error(error);

You should get a json message with the topic if your last created lead on Dynamics 365. The request URL basically calls for the leads list, order it by creation time. Then it selects the subject field for the top record on the list.

When ready replace the placeholders in the below nodejs script with your organization url, authorization token and Speech API key.

// pull in the required packages.
var sdk = require("microsoft-cognitiveservices-speech-sdk");
var fs = require("fs");
var player = require('play-sound')(opts = {})

var request = require('request');
var options = {
  'method': 'GET',
  //getting the latest lead topic from your D365 organization
  'url': '<orgurl>/api/data/v9.0/leads?$orderby=createdon desc&amp;$select=subject&amp;$top=1',
  'headers': {
    'Authorization': 'Bearer <Token>'

request(options, function (error, response) { 
  if (error) throw new Error(error);
  //upon receiving the response from our WebAPI call we parse the json response and use the results to build the message
  message = "Hello, you have a new lead with a topic saying " + JSON.parse(response.body).value[0].name;
  //then we call our say() and pass the message to it

var subscriptionKey = "<Key>"; //place your subscription key here
var serviceRegion = "northeurope"; // place your azure location here
var filename = "notification.wav"; // This is the file name which is going to be downloaded

// create the pull stream we need for the speech sdk.
var pullStream = sdk.AudioOutputStream.createPullStream();

// open the file and write it to the pull stream.
fs.createWriteStream(filename).on('data', function(arrayBuffer) {;
}).on('end', function() {

// now create the audio-config pointing to our stream and
// the speech config specifying the language.
var speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);

// setting the recognition language to English.
speechConfig.speechRecognitionLanguage = "en-US";

var audioConfig = sdk.AudioConfig.fromStreamOutput(pullStream);

// create the speech synthesizer.
var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);

function say(text){
  // start the synthesizer and wait for a result.
    function (result) {

      //when the wav file is ready, play it'notification.wav', function(err){
          if (err) throw err
      synthesizer = undefined;
    function (err) {
      console.trace("err - " + err);

      synthesizer = undefined;

When done, save the script as notification.js and copy it to your Raspberry Pi. Connect into your Raspberry Pi over ssh and type the following command to install the required npms

npm install microsoft-cognitiveservices-speech-sdk fs play-sound request

Reading Notifications from Dynamics 365 over Raspberry Pi

Now in order to test this you will need to create a new lead on Dynamics 365 with topic “Hello World!”. When all is done now you can execute your new nodejs script like following

node notification.js

This is going to run your script so it will call the CDS WebAPI and get the latest lead, build the message and send it for Speech API. Then it will download the audio to play it for you over the speaker.

If you did hear the voice telling you have new lead saying hello world. Then congratulations! You just connected a Raspberry Pi to Dynamics 365. Hope you did enjoy this quick howto article. Stay tuned for the next one where I’m having a treat for you.

Leave a Reply

Your email address will not be published. Required fields are marked *