question

Kalakonda avatar image
Kalakonda asked

External API information through account linking

Hello,

I could successfully link my account to Dynamics 365. But when I tried to access the external api and get the information alexa says: There was a problem with the requested skill's response

any suggestions ? Im not sure where did I go wrong.

function httpGet(callback) {
  return new Promise(((resolve, reject) => {
      const BcApi = {
          host: "api.businesscentral.dynamics.com",
          path: "/v1.0/011a0b69-cf53-4de9-bdd2-2548f2f870b1/ODataV4/Company('CRONUS%20DE')/contacts/",
          port: 443,
          method: 'GET',
  };

 const request =  http.request(BcApi,(res) => {
  res.setEncoding('utf8');
  let returnData = '';

  res.on('data', (chunk) => {
      returnData += chunk;
  });

  res.on('end', () => {
      const channelObj = JSON.parse(returnData).value[4];
      let contactname = channelObj.Company_Name.toString();
      callback(contactname)
  });

  res.on('error', (error) => {
      reject(error);
    });
  });
  request.end();
}));
}



const GetinfoHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'GetInfoIntent';
      console.log("Get Info Handler")
  },

  async handle(handlerInput) {
    const { accessToken } = handlerInput.requestEnvelope.context.System.user;
    let speechText = '';
    if (!accessToken) {
      speechText = 'authenticate here';
      return handlerInput.responseBuilder
        .speak(speechText)
        .withLinkAccountCard()
        .getResponse();
    } else {
      httpGet((contactname)=> {
        const speechOutput = `The contact you are looking for is ${contactname}`;
        return handlerInput,responseBuilder
        .speak(speechOutput)
        .withShouldEndSession(true)
        .getResponse();
       });
          
    }
  }
}



alexa skills kitaccount linkingapinode_jsnodejs
10 |5000

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

1 Answer

Rokas avatar image
Rokas answered

There is something wrong with your http get function. I'll give you an example that works:

const https = require('https')

function googleProfile(accessToken) {
  const url = `https://www.googleapis.com/plus/v1/people/me?access_token=${accessToken}`
  return new Promise(resolve => {
    https.get(url, res => {
      res.setEncoding('utf8')
      let body = ''
      res.on('data', data => {
        body += data
      })
      res.on('end', () => {
        resolve(JSON.parse(body))
      })
    })
  })
}

module.exports = googleProfile

I used it to get google profile with an accesstoken. I think you can adjust your example to this.

One more very important thing. You have to either resolve this promise using 'await' or using 'then()'.

So it either could look like this:

const googleProfileInfo = await googleProfile(accessToken)
responseBuilder.speak(googleProfileInfo.name) // or smth

Or like this:

googleProfile(accessToken).then((r) => {
  console.log(r)
  responseBuilder.speak(r.name) // or smth
}

Also, there is a mistake in line 53, there is comma after handlerInput, where it should be a dot.

Also if you don't use await in the function, there is no need to add 'async': line 41. If you decide to use await, then it is needed.

3 comments
10 |5000

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

Thanks for your input, I still got stuck with the problem.

0 Likes 0 ·

Have you tried my code? What error are you getting? Can you take a look at your cloudwatch logs and post some of the errors here?

1 Like 1 ·

Thank you @rokas it worked.

0 Likes 0 ·