Upgrade
Sign In : Sign Up : Blog : Search: Language : Help :
Host your own bot platform with the Bot Libre Corporate Bot Platform
Bot Libre Forum

Connect your Bot to the World - XML, JSON, Web Services, HTML Scraping, Twitter, Facebook, Telegram, Email

by admin posted Jun 27 2016, 12:50

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 Services

A 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 Services

XML 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 Example

This 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.

URL

XML Result
<geonames> <totalResultsCount>6</totalResultsCount> <code> <postalcode>90210</postalcode> <name>Beverly Hills</name> <countryCode>US</countryCode> <lat>34.09011</lat> <lng>-118.40648</lng> <adminCode1>CA</adminCode1> <adminName1>California</adminName1> <adminCode2>037</adminCode2> <adminName2>Los Angeles</adminName2> <adminCode3/> <adminName3/> </code> </geonames>

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

AIML Example

You 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

<aiml> <category> <pattern>Guess where I live</pattern> <template>What is your postal code?</template> </category> <category> <pattern>*</pattern> <that>What is your postal code</that> <template>You live in <sraix service="XML" hint="code/name">http://api.geonames.org/postalCodeSearch?maxRows=1&username=demo&postalcode=<star/></sraix>.</template> </category> </aiml>

XML POST Example

An 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 request() Self API to allow bots to talk to other bots on Bot Libre or other services such as PandoraBots, and PersonalityForge.

Self Example

This 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 Services

JSON 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 Example

This 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.

URL

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

AIML Example

You 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 Scraping

HTML 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 Example

You 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

AIML HTML Example

You 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

<aiml> <category> <pattern>lookup url *</pattern> <template>It is about <sraix service="html" hint="head/meta[@name='description']/@content"><star/></sraix>.</template> </category> </aiml>

Facebook, Twitter, Telegram, Email, SMS, RSS

Bot 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.


by bellts posted Jun 28 2016, 19:33

Does anybody have a clue when I run a self script with an Http.requestXML I recieve the following error:

WARNING: botlibre_bots - Thread[pool-104-thread-1,5,main] -- Language:Error occured in processing state - <47541 {tacosAI} 47549 a:1,c:0> - <47542 {test} 47547 a:1,c:0>
Jun 28, 2016 11:27:12 PM org.botlibre.Bot log
WARNING: botlibre_bots - Thread[pool-104-thread-1,5,main] -- Language:Missing function: requestXML on: <13004 #http a:1,c:0,p>


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 514, today: 1, week: 3, month: 21

by admin posted Jun 28 2016, 21:48
The function is,

Http.requestXML(url, xpath)

The error is most likely because you are calling the function with the wrong number of arguments.
If you include your code, I can correct it.

Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 456, today: 0, week: 0, month: 10

by bellts posted Jun 29 2016, 13:14

Here's an example what I'm trying to do (very simple call out to anywhere really).

Error: 

WARNING: botlibre_bots - Thread[pool-30-thread-1,5,main] -- Language:Error occured in processing state - <172001 {tacosAI} 172011 a:1,c:0,p> - <172002 {test} 172009 a:1,c:0,p>
Jun 29, 2016 5:07:39 PM org.botlibre.Bot log
WARNING: botlibre_bots - Thread[pool-30-thread-1,5,main] -- Language:Missing function: requestJSON on: <13004 #http a:1,c:0,p>

 

state tacosAI {

case input goto test;

state test
{
answer thatsoWordState();

function thatsoWordState() {
var result = Http.requestJSON("http://api.geonames.org/postalCodeSearchJSON?maxRows=1&username=botlibre&postalcode=92590", "postalCodes");
if (result == null) {
return "Something went wrong.";
}
return "I said something.";

}
}
}


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 533, today: 1, week: 2, month: 18

by admin posted Jun 29 2016, 14:57
The requestXML() function takes two parameters, but the requestJSON() only takes one currently (this will soon be fixed to allow a second attribute argument).

Http.requestXML(url, xpath)

Http.requestJSON(url)

The JSON API returns an object converted from the JSON. You can use "." to traverse it.

i.e.
var result = Http.requestJSON("http://api.geonames.org/postalCodeSearchJSON?maxRows=1&username=botlibre&postalcode=92590"); result = result.postalCodes.get(#element, 0).placeName;


The get() can be used to get an element from a JSON array.

Updated: Jun 29 2016, 21:45
Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 551, today: 0, week: 1, month: 21

by bellts posted Jun 29 2016, 15:08

Well.... none of that worked. I'm assuming that this is only available on botlibre.com and NOT available via source code or supplied ai-engine.jar (ver 4.0).

I'm now trying a hack... which seems to work. I just can't figure out how to get the response from the call. I have validated that the call goes out (and I see the proper/valid response in the log). Any clue?

Sample code:

 

state tacosAI {

case input goto test;

state test
{


function thatsoWordState() {
//var result = Http.requestJSON("http://api.geonames.org/postalCodeSearchJSON?maxRows=1&username=botlibre&postalcode=92590");
var result = request (null, { server : "http://api.pathient.com/spectation/hello?nothing="});
if (result == null) {
return "Something went wrong.";
}
return result.code.content + " target";

}
}
}

 

Log:

INFO: botlibre_bots - Thread[pool-65-thread-1,5,main] -- RemoteService:Response - <code><content>hello</content></code>

 


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 544, today: 1, week: 4, month: 26

by admin posted Jun 29 2016, 21:45
The Http class is new, it is available on BotLibre.com, but not available in the BotLibre.org open source AI engine yet.

We will hopefully update the GitHub source to version 4.4 soon. For now you can test your script on BotLibre.com

The Self request() API implements the AIML2 SRAIX functionality. In the Bot Libre 4.0 code it can only be used to call other AIML2 compatible services, like forwarding a question to another bot. In 4.4 it will also be able to access XML and JSON web services, and scrape HTML.

Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 530, today: 0, week: 0, month: 16

by marcosnunes posted Feb 18, 12:05
Why did I got this error?
I am trying make the bot run on facebbok. It is working well but by retrieving wordpress posts I got this error bellow:





2017-02-18 12:02:11.298 - WARNING -- FacebookMessaging:400 : {"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"FChOvtf+1\/x"}}
2017-02-18 12:02:11.299 - WARNING -- FacebookMessaging:400 : {"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"FChOvtf+1\/x"}}
2017-02-18 12:02:11.301 - WARNING -- FacebookMessaging:java.lang.RuntimeException: 400 : {"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"FChOvtf+1\/x"}} at org.botlibre.util.Utils.fetchResponse(Utils.java:496) at org.botlibre.util.Utils.httpPOST(Utils.java:438) at org.botlibre.sense.facebook.Facebook.sendFacebookMessengerMessage(Facebook.java:1665) at org.botlibre.sense.facebook.FacebookMessaging.output(FacebookMessaging.java:328) at org.botlibre.sense.BasicAwareness.output(BasicAwareness.java:207) at org.botlibre.thought.language.Language.think(Language.java:1229) at org.botlibre.thought.BasicMind.processConsciousThoughts(BasicMind.java:343) at org.botlibre.thought.BasicMind$1$1.run(BasicMind.java:270) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
2017-02-18 12:02:11.302 - WARNING -- FacebookMessaging:java.lang.RuntimeException: 400 : {"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"FChOvtf+1\/x"}} at org.botlibre.util.Utils.fetchResponse(Utils.java:496) at org.botlibre.util.Utils.httpPOST(Utils.java:438) at org.botlibre.sense.facebook.Facebook.sendFacebookMessengerMessage(Facebook.java:1665) at org.botlibre.sense.facebook.FacebookMessaging.output(FacebookMessaging.java:328) at org.botlibre.sense.BasicAwareness.output(BasicAwareness.java:207) at org.botlibre.thought.language.Language.think(Language.java:1229) at org.botlibre.thought.BasicMind.processConsciousThoughts(BasicMind.java:343) at org.botlibre.thought.BasicMind$1$1.run(BasicMind.java:270) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 198, today: 1, week: 2, month: 24

by admin posted Feb 19, 16:54
The error has nothing to do with WordPress.
It seems to be an error with the bot trying to send a message to an invalid user. My guess is you sent a message to the page as the page, so Facebook does not allow the page to reply to itself.

Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 160, today: 0, week: 0, month: 8

by marcosnunes posted Feb 21, 18:25

On the conversation log the bots answer appear normally but the facebook messenger dont retrieve its response. I think it may be oAuth config but I don't know how to configure. I've tried in wordpress configure the wp-oAuth api but had the same experience. On the messenger all the responses parses but only the response that come from wordpress rest api doesnt.


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 171, today: 0, week: 1, month: 10

by admin posted Feb 21, 18:30
It sounds like you did not connect your bot to Facebook Messenger correctly.
Ensure you,
  • Created a Facebook app at https://developers.facebook.com
  • Used its app id and secret to authorize your bot
  • Selected the correct Facebook page when you clicked "Connect"
  • Ensure you setup your Facebook app correctly, and registered the webhook correctly
  • Ensure you subscribed your app to the correct page.
  • Ensure you used the apps page token, and saved in it your bot's properties.
See,
https://www.botlibre.com/forum-post?id=12742773

Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 172, today: 0, week: 1, month: 12

Id: 13020078
Tags: how to, rest, xml, json, self, scripting, http, html scraping, api, twitter, facebook, telegram, email, web services, aiml
Posted: Jun 27 2016, 12:50
Updated: Jun 27 2016, 13:03
Replies: 10
Views: 4239, today: 7, week: 29, month: 314
I'm sure
Flag graphic as offensive, or in violation of site rules