Source: site.view [edit]
Function name: bingAnswers
Arguments: query
Description: Returns answers matching query
Page type: webl
Render function:  
Module: global

Page source:

var APIKEY = Wub_GetVaultValue("bingkey") ? nil;

var P = GetURL("https://api.cognitive.microsoft.com/bing/v5.0/search",  [. q=query  .], [. "Ocp-Apim-Subscription-Key"=APIKEY .] );

var Compute = Elem(P, "computation");
var Facts = Elem(P, "facts");

var res = nil;
var textAnswer = "";

if (Size(Facts) > 0) then
   var D = Elem(P, "description") inside Facts[0];
   if Size(D) > 0 then
      textAnswer = Str_Trim(Text(D[0]));
   end;
end;
  
if Size(Compute) > 0 then
   var D = Elem(P, "value") inside Compute[0];
   if Size(D) > 0 then
      textAnswer = Str_Trim(Text(D[0]));
   end;
end;

if (textAnswer != "") then
    res = [. .];
    res.text := textAnswer;
    res.html := textAnswer;
end;

res;

/*
<computation>
    <expression>
        1 + 1
    </expression>
    <id>
        https://api.cognitive.microsoft.com/api/v5/#Computation
    </id>
    <value>
        2
    </value>
</computation>

<facts>
    <id>
        https://api.cognitive.microsoft.com/api/v5/#Facts
    </id>
    <value>
        <subjectName>
            China ยท Population, total
        </subjectName>
        <description>
            1.37 billion (2015)
        </description>
    </value>
    <attributions>
        <seeMoreUrl>
            http://www.bing.com/cr?IG=E6B069C8000C47F29C10EDADCB42E783&amp;CID=2AFC29BBDB4F668B0D70233DDA106742&amp;rd=1&amp;h=STxZ65uhzF5iAe_64BqULi4brJ4rYyyZy-MIMHsPQPg&amp;v=1&amp;r=http%3a%2f%2fdata.worldbank.org%2findicator%2fSP.POP.TOTL&amp;p=DevEx,5273.1
        </seeMoreUrl>
        <providerDisplayName>
            World Bank
        </providerDisplayName>
    </attributions>
</facts>
   
   
<facts>
    <id>
        https://api.cognitive.microsoft.com/api/v5/#Facts
    </id>
    <value>
        <subjectName>
            Who is Barack Obama?
        </subjectName>
        <description>
            Barack Obama was the 44th president of the United States,
            and the first African American to serve in the office.
            First elected to the presidency in 2008, he won a second
            term in 2012.
        </description>
    </value>
    <attributions>
        <seeMoreUrl>
            http://www.bing.com/cr?IG=86F0481B5598438E93C5FFE7EB6495B4&amp;CID=334D81D9DC2E677B0DE58B5FDD7166E6&amp;rd=1&amp;h=ftS_SxMU44YoK6CQgesS3VRuArhzDJje1UnrZDD6vlc&amp;v=1&amp;r=http%3a%2f%2fwww.biography.com%2fpeople%2fbarack-obama-12782369&amp;p=DevEx,5293.1
        </seeMoreUrl>
        <providerDisplayName>
            www.biography.com/people/barack-obama-12782369
        </providerDisplayName>
    </attributions>
</facts>

*/