Connect your Bot to the World - XML, JSON, Web Services, HTML Scraping, Twitter, Facebook, Telegram, Email |
Bot Libre now supports integrating with XML and JSON web services, HTML scraping, and access to Twitter, Facebook, Telegram, email, and other services. From our scripting languages Self, and AIML you can now access any web service that returns XML or JSON data. You can also scrape information from HTML web pages, and send emails, tweets, Facebook posts, Telegram posts, and more. Web ServicesA web service is an Internet service that provides information on the web. There are countless APIs available on the web that provide every type of service imaginable, such as Google Maps, Twitter, Twilio SMS, and eBay. There also many Internet directories to help you find and use web services such as ProgrammableWeb. Bot Libre provides web service support through its Self scripting language, and through the AIML scripting language. Bot Libre also provides a script library of common scripts to access many useful web services. XML Web ServicesXML is the parent markup language to HTML. XML defines a text based data format consisting of the data's attributes, elements, and text data. To call an XML webservice you use a web URL with either a GET request, or a POST request. Bot Libre supports both GET and POST request with Self, and supports GET requests with AIML. XML GET ExampleThis example uses the GeoNames web service from GeoNames.org. Geonames can return place names and other geographic information, given a postal code, or other geographic information. You can apply the same pattern to access many other XML web services. URLhttp://api.geonames.org/postalCodeSearch?postalcode=90210&maxRows=1&username=botlibre XML Result
6
<code>
90210
Beverly Hills
US
34.09011
-118.40648
CA
California
037
Los Angeles
</code>
Self Example You can access web services from Self using a Template object in a response, or from a script. The Http class is used calling the requestXML method and passing the URL, and an optional XPath expression that extract the desired data from the XML document. XPath is a standard navigation query syntax to access or extract data from an XML document. If an XPath is not given, the call will automatically convert the XML data to a Self object. You can access the object's data using the standard JavaScript dot notation. It is much more efficient to use an XPath, so the entire XML document does not need to be converted and persisted as a Self object. This example is also available in our script library here Guess where I live? What is your postal code? default: Template("You live in {Http.requestXML("http://api.geonames.org/postalCodeSearch?maxRows=1&username=demo&postalcode=" + star, "code/name")}.") require previous: What is your postal code?
AIML ExampleYou can access a web service from AIML using the SRAIX tag. SRAIX is an AIML tag used to call other services, or other bots. SRAIX has several attributes, the service must be set to XML to access an XML service, and the URL is given as the SRAIX body. An XPath expression must be provided to the call to extract the desired text data from the XML document. This example is also available in our script library here
Guess where I live
What is your postal code?
*
What is your postal code
You live in http://api.geonames.org/postalCodeSearch?maxRows=1&username=demo&postalcode=.
XML POST ExampleAn XML POST request sends, and receives XML data. Many web services use POST requests so the application can provide data to the call.
This example will actually call the Bot Libre web API using a POST request. This is somewhat unusual, as the bot is already on Bot Libre, but this lets one bot talk to another bot. Be careful doing this, as you do not want bots to get into loops or cycles. You can use this same pattern to call any other web service that takes and returns XML data, even call bots on other servers or APIs. Bot Libre also provides the sraix AIML tag and the Self ExampleThis example uses a Self state to forward a question to Brain Bot when the user says "ask Brain Bot". This example is also available in our script library here
state AskBrainBot {
pattern "ask brain bot" template askBrainBot();
function askBrainBot() {
// Get what the user said previously, and ask Brain Bot the question.
var previous = conversation.input[-2].input;
var message = new Object();
message.message = previous;
message.root = "chat";
message.@instance = "165";
message.@application = "yourappid";
var result = Http.postXML("http://www.botlibre.com/rest/api/chat", message);
if (result == null) {
return "Brain Bot is not available";
}
return "He says: " + result.message;
}
}
JSON Web ServicesJSON is the standard text base object format for JavaScript. JSON can be used to convert any JavaScript object to text, and included attributes, nested objects, and arrays. To call a JSON webservice you use a web URL with either a GET request, or a POST request. Bot Libre supports both GET and POST request with Self, and supports GET requests with AIML. JSON GET ExampleThis example uses the GeoNames web service from GeoNames.org. Geonames can return place names and other geographic information, given a postal code, or other geographic information. You can apply the same pattern to access many other JSON web services. URLhttp://api.geonames.org/postalCodeSearchJSON?postalcode=90210&maxRows=1&username=botlibre JSON Result
{"postalCodes":[{"adminCode2":"037","adminCode1":"CA","adminName2":"Los Angeles County","lng":-118.406477,"countryCode":"US","postalCode":"90210","adminName1":"California","placeName":"Beverly Hills","lat":34.090107}]}
Self Example You can access web services from Self using a Template object in a response, or from a script. The Http class is used calling the requestJSON method and passing the URL, and an optional attribute that extract the desired data from the JSON document. This example is also available in our script library here Guess where I live? What is your postal code? default: Template("You live in {Http.requestJSON("http://api.geonames.org/postalCodeSearchJSON?maxRows=1&username=demo&postalcode=" + star, "code")}.") require previous: What is your postal code?
AIML ExampleYou can access a web service from AIML using the SRAIX tag. SRAIX is an AIML tag used to call other services, or other bots. SRAIX has several attributes, the service must be set to JSON to access a JSON service, and the URL is given as the SRAIX body. A object attribute must be provided to the call to extract the desired text data from the JSON document. Only basic JSON web services can be called with AIML, as AIML is texted based, not object based. Only direct attributes can be accessed. Since our GeoNames examples returns complex data, an AIML example is not possible. HTML ScrapingHTML is the markup language used in all websites on the Internet. You can access data from an HTML page using the URL for the page, and an XPath expression to extract the data. Refer to the XML example for a description of XPath. Bot Libre supports HTML scraping with Self, and AIML. HTML ExampleYou can access HTML data from Self using a Template object in a response, or from a script. The Http class is used calling the requestHTML method and passing the URL, and an XPath expression that extracts the desired data from the HTML document. This example is also available in our script library here Lookup URL * Template("It is about {Http.requestHTML(star, "head/meta[@name='description']/@content")}.")
AIML HTML ExampleYou can access a web service from AIML using the SRAIX tag. SRAIX is an AIML tag used to call other services, or other bots. SRAIX has several attributes, the service must be set to HTML to access an XML service, and the URL is given as the SRAIX body. An XPath expression must be provided to the call to extract the desired text data from the XML document. This example is also available in our script library here
lookup url *
It is about .
Facebook, Twitter, Telegram, Email, SMS, RSSBot Libre provides several other classes in Self for accessing other services, such as Facebook, Twitter, Telegram, SMS, email, RSS feed, and more. For example, if your bot is connected to a Twitter account, you can have the bot send a tweet using: Twitter.tweet("hello world");This can be used to forward posts from one service to another, or escalate a chat session to email, or notify a human agent using SMS. There lots of examples scripts, here. When you can connect your bot to the whole world, the possibilities are endless. We can't wait to see what you will build.If you encountered any issues, or would like help setting up your bot please email us at [email protected] or upgrade to our Platinum service and we can build your bot for you. |
|
|
|
|