question

Kalakonda avatar image
Kalakonda asked ·

calling external web api (azure)

Hello,

Im trying to get data from an API hosted in Microsoft azure as (https://xxxxxxx.azurewebsites.net/api/XXXXXXXX?code=XXXXXXXXXX==), would it be possible to parse this in Alexa ? or should it be in Json format ?

Im tyring to use the code below to get the information from the azure function.

const myAPI = {
  host: 'https://xxxxxxx.azurewebsites.net',
  port: 443,
  path: 'api/XXXXXXX?code=XXXXXXXXXXXXXXXXXXXXX==',
  method: 'GET',
};

function getContactdata(callback) {
  const req = https.request(myAPI, (res) => {
    console.log('statusCode',res.statusCode);
    console.log('header',res.headers);

    res.setEncoding('utf8');
    let returnData = '';

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

    res.on('end', () => {
      const channelObj = JSON.parse(returnData).query.results.channel;
      // Here is the problem 
      let localTime = channelObj.lastBuildDate.toString();
      localTime = localTime.substring(17, 25).trim();

      const currentTemp = channelObj.item.condition.temp;

      const currentCondition = channelObj.item.condition.text;

      callback(localTime, currentTemp, currentCondition);
    });
  });
  req.end();
}


alexa skills kitjavascriptazure
10 |2000 characters needed characters left characters exceeded

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

1 Answer

Amazon_Bernardo Bezerra avatar image
Amazon_Bernardo Bezerra answered ·

Hi @Kalakonda and thank you for your message.

Are you still facing this issue? From the code snippet you posted, you mention that there is a problem, but there is no indication of what the problem is. Can you please specify?

Regards,
Barry

5 comments
10 |2000 characters needed characters left characters exceeded

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

  const PostDataHandler = {
    canHandle(handlerInput) {
      const request = handlerInput.requestEnvelope.request;

      return request.type === 'IntentRequest' && request.intent.name === 'PostDataHandler';
    },
    handle(handlerInput) {
      return new Promise((resolve) => {
        httpsPost((contactname) => {
          const speechOutput = `It is ${contactname
            } which is inserted`;
          resolve(handlerInput.responseBuilder.speak(speechOutput).getResponse());
        });
      });
    },
  };


  function httpsPost(myData, callback) {

    var post_data = { "contactname": myData };

    var post_options = {
      host: 'https://connectorconsole.azurewebsites.net',
      port: '443',
      //path: '/prod/stateresource',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(JSON.stringify(post_data))
      }
    };

    var post_req = https.request(post_options, res => {
      res.setEncoding('utf8');
      var returnData = "";
      res.on('data', chunk => {
        returnData += chunk;
      });
      res.on('end', () => {
        var contactname = JSON.parse(returnData).contactname;
        callback(contactname);

      });
    });
    post_req.write(JSON.stringify(post_data));
    post_req.end();
  }


This is what I get: There was a problem with the requested skill's response
0 Likes 0 ·

also is there any example for How to use httpsGet code if you want myRequest value to change based on user input

0 Likes 0 ·

Hi @Kalakonda and thanks for the information.

The error message "There was a problem with the requested skill's response" means that there is an issue with your node.js code. My guess would be that there are some problems with the way your are collecting the information from you server. Unfortunately, this is not an error specific to Alexa development and I can not help you further. I would focus on understanding how to perform HTTPS requests and then implement it to your code.

Regards,
Barry

0 Likes 0 ·
Kalakonda avatar image Kalakonda Amazon_Bernardo Bezerra ♦♦ ·

Thanks Barry, I I figured out the problem and it was with the Https requests.

0 Likes 0 ·
Show more comments