function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash@salesforceAvinash@salesforce 

Integrate Salesforce with alexa webservice (Parsing the response)

Hi all.

I am integrating salesforce with an Alexa webservices. My scenario is to get the traffic history and save it in account object in salesforce.

I was  able to get the XML Response which consists traffic history, but while parsing i am getting null response.

Below is my class.


I am using the Alexa Web Information Service traffic history API.
Need Help in parsing the response...!

My Class:: 


        req.setEndpoint(endpoint);         
        req.setMethod('GET');
        String responseBody;
        Http http = new Http();
        HttpResponse res = http.send(req);
        String GlobalRank;

        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('BODY: '+res.getBody());
        
        DOM.Document xmlDOC = new DOM.Document();
        xmlDOC.load(res.getBody());
          DOM.XMLNode rootElement = xmlDOC.getRootElement(); 
         for(DOM.XMLNode xmlNodeObj:xmlDOC.getRootElement().getChildElements()){      
Line 47   ========>>  System.Debug('$$$ Child Elements'+xmlNodeObj);
           for(DOM.XMLNode xmlNodeObjChild:xmlNodeObj.getChildren())
           {
           System.Debug('$$$ Childrens'+xmlNodeObjChild.getChildren());           
               system.debug('%%%%%%%%--'+xmlNodeObjChild.getName());  
               if(xmlNodeObjChild.getName()=='Range')
                 System.Debug(xmlNodeObjChild.getText());
                 if(xmlNodeObjChild.getName()=='Site')
                 System.Debug(xmlNodeObjChild.getText());
                 if(xmlNodeObjChild.getName()=='Start')
                 System.Debug(xmlNodeObjChild.getText());
                           
                }             
       }            
      //  return   GlobalRank.trim()<>''? Integer.valueOf(GlobalRank.trim()):null;
      return null;
    }
    private static void calculateTimeStamp(){
        Datetime now = Datetime.now();
        timestamp = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';
        System.Debug('Formatted date : '+timestamp);
    }
    private static void generateAWSSignature(String verb ,String action, String request){
        String canonical = verb+'\n'+
                'awis.amazonaws.com\n'+
                '/\n'+
                request;
        system.Debug('String to Sign:\n'+canonical+'\n');
        Blob bsig = Crypto.generateMac('HmacSHA1', Blob.valueOf(canonical), Blob.valueOf(secretAccessKey));
        String signature = EncodingUtil.base64Encode(bsig);
        AWSSignature = signature;
    }
}


Need Help in parsing the response from alexa.!
pconpcon
What I would recommend is that you use the JSON response from them instead of the XML and use typed deserialization [1].

[1] http://blog.deadlypenguin.com/blog/2015/11/30/json-deserialization-in-salesforce/
Avinash@salesforceAvinash@salesforce
Thanks for your response Pcon,

But the response it is returning is in XML format. How do i use JSON (Or) Is there any way that alexa returns itsresponse in JSON.

Thaanks,
Avinash
pconpcon
It looks like you simply need to set the Accept header to "application/json"

https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference