AI Zone Admin Forum Add your forum

CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP CHATBOT CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP CHATBOT MEETUP

On June 28th, 2017 we're organizing our next Chatbot Meetup in Amsterdam, The Netherlands. Feel free to join!'

CS 5.7 released-  ChatScript Meets JSON
 
 

ChatScript now supports easy access to JSON data from a website (using CURL to access). Just what you needed to respond on the fly from your ChatBot using the web. Lots of websites support JSON structured data, including IBM’s Watson, airlines, weather, etc.

 

 
  [ # 1 ]

I am trying out the new version of chatscript and can issue a ^jsonopen command and a ^jsonprint command but cannot figure out the ^jsonpath command.

I tried ^jsonpath( [0].weather.id $$jsonid )
^jsonpath( [0].weather.id $$jsonid )
^jsonpath( [0].weather.main $$jsonid )
^jsonpath( [0].main $$jsonid )
^jsonpath( [1].main $$jsonid )
^jsonpath( [1].weather.main $$jsonid )
^jsonpath( .id $$jsonid )

The last generates an error complaining that only 2 of 3 arguments were supplied.


Printing the $$jsonid and the output of the ^jsonpath of my call yields the following.  What would be the correct syntax to obtain the weather fields?  Thx.


jsonid: jo-0
{
"coord": {
"lon": -0.13

"lat": 51.51

}

“weather”: <
  {
"id": 801

"main": Clouds

"description": few clouds

"icon": 02n

}

  >
...

}

 

 
  [ # 2 ]

I expect there is an error in json path that I have fixed but not released. I will review this this weekend and release a new version as appropriate.

 

 
  [ # 3 ]

Try CS 5.72 now released.

 

 
  [ # 4 ]

Does anyone know how to compile 5.72 for the MAC?  Bruce tells me to ask here. His note about what is happening is below:

.... Message from Bruce about the Mac compile——

The mac compile command does not name the curl library so it is missing functions. I don’t know how it is supposed to be listed. I added it to the linux make file as LDFLAGS= -lrt -lcurl which for linux requires that CURL be installed as a package.  If MAC were synonymous with LINUX, then one would install curl the same way (using package install appropriate to the linux variant). But we are outside my area now, on mac. You’d need to inquire of mac people in IT or on chatbots.org.

—end message from bruce—-

Thanks to MAC uses that know the answer!

John Bourne

 

 

 
  [ # 5 ]

^jsonpath is fixed now in release 5.72

I created the following sample code to test a weather api:

outputmacro: ^KelvinToFahrenheit(^value)
  $$answer = ^compute(^value - 273.15)
  $$answer = ^compute($$answer * 1.80)
  $$answer = ^compute($$answer + 32.00)
  # $$answer = ^compute($$answer mod 100)
  # $$answer = ^compute($$answer * 100)
  $$answer = ^join($$answer f)
  $$answer

outputmacro: ^GetLocalWeather (^Town) 
$$APIKey = 
$$url = ^join(api.openweathermap.org/data/2.5/weather?q= ^Town)  #limit 1000 requests/day
$$url_apikey = ^join($$url &appid;= $$APIKey)
$$jsonid = ^jsonopen(GET $$url_apikey “” “”)

# Uncomment to troubleshoot and see json response
# ^JSONPrint($$jsonid)

$$Weather = ^jsonpath( .main jo-2)
$$WeatherDesc = ^jsonpath( .description jo-2)
$$Temp = ^jsonpath( .temp jo-3)
$$TempMin = ^jsonpath( .temp_min jo-3)
$$TempMax = ^jsonpath( .temp_max jo-3)
$$TempF = ^KelvinToFahrenheit($$Temp)
$$TempMinF = ^KelvinToFahrenheit($$TempMin)
$$TempMaxF = ^KelvinToFahrenheit($$TempMax)
You can expect $$WeatherDesc.  It is $$TempF and will range from $$TempMinF to $$TempMaxF today.

topic: ~MyWeather ( weather )

u: ( what is the weather today ) ^keep() ^repeat()
$$MyTown = Los_Angelos
^GetLocalWeather($$MyTown)


Sample Output:

Welcome to ChatScript.
  >what is the weather today?
You can expect sky is clear. It is 91.06f and will range from 84.20f to 99f today.

The weather descriptions returned from the api are not consistently structured so making one generic sentence to return the weather description results does not work.  Another sample description is “scattered clouds” instead of “sky is cloudy”.  Different custom responses to the user would need to be created for each weather description returned to sound more natural.

TIP: ^jsonpath can take a first argument with spaces if you use an underscore.  In the sample above “temp min” was an object attribute name in the returned json but ^jsonpath( .temp_min jo-3) worked in referencing it and retrieving the value.

(Anyone know how to round to the nearest whole number in Chatscript?  Also, basic math was not working so had to use a series of ^compute functions for converting Kelvin to Fahrenheit.)

 

 
  [ # 6 ]

An alternative to using json path is to do a query on the field name as a verb. Typically it is unique and thus you can do this   @0 = query(direct_v ? temp_min ? 1)  @0object
This gets around the randomness of their structures.

 

 
  [ # 7 ]

Mac compilation solved. 

I found a website somewhere that said that only LINUX machines required the rt library.  So, the compilation statement given by Bruce: 

g++  -funsigned-char src/*.cpp -o chat -L/usr/lib -lrt -lcurl -lpthread 2>err.txt

is fixed for the mac by using:

g++  -funsigned-char src/*.cpp -o chat -L/usr/lib -lcurl -lpthread 2>err.txt

The code compiles fine and runs.

Now back to trying out JSON:cheese:

 

 
  [ # 8 ]

Hi, Alaric.

What seems to work for you is not responding a thing for me. I’ve no idea how else I can access the json with ^jsonpath()
I took your macros and add an app key. 

But the lines with jsonpath is leading the CS to go to the gambit rules instead.
          $$Weather = ^jsonpath( .main jo-2)

When I changed it to something like $$Weather = ^jsonpath($$jsonid[0].id.main) didn’t work as well.

Anything that I am missing here? Hope you can this thread after more than a year. Thanks.

 

 
  [ # 9 ]

Eskender,

I would review the latest documentation in the Json.pdf that comes with the chatscript download.  Mine is located in the following path:
C:\Users\username\Documents\Chatscript\ChatScript-6.84\PDFDOCUMENTATION\

For some troubleshooting suggestions see below:

Uncomment the line:
# ^JSONPrint($$jsonid)

to see the raw json response.

Remove or comment your code in your topic and just print a response to confirm your topic works.  To avoid the gambit rules you need to make sure your input includes the keywords in your topic.  Consider adding xxweather for testing and see if using xxweather in the input directs control to your topic.  You need to have the ^keep() and ^repeat() added to your match pattern or after the first response you may be directed to your gambit topics.  If you have any syntax error your topic may be skipped and you might be directed to your gambits.  if you add a
u: (*) Hi. 
to the end of your topic at least you will know that processing control reached your topic but did not match on the
u: ( what is the weather today ) ^keep() ^repeat()

If you get that far then comment out the output macro and see if that works.  Then comment the lines in the output macro and add one line at a time back until you find the line with the issue that causes the whole pattern to be skipped.

There are ways of debugging from the prompt using commands that are discussed in this document:
ChatScript-Debugging-Manual.pdf

for instance you can force the use of a topic by using the command
:testtopic ~myweather

and you can issue the command
:trace ~myweather
to see a trace of the processing flow for that topic

Type JSON in the search box on this page and look at some other more recent examples of people using JSON with chatscript and see if there might be an easier way to test with another URL in order to rule out an issue that might be specific to this weather URL.

Let us now how your testing goes.

 

 

 
  [ # 10 ]

Thanks so much for the followup, Alaric. It was some problems I made hastily and it’s all working now. But the web documentation would be a big help to understand the whole thing as I am still in early stages of grasping the real power of CS behind the basics. Thanks a lot.

 

 
  [ # 11 ]

BTW, it’s now available on the ChatScript github repo a WIKI containing all documentation available:

https://github.com/bwilcox-1234/ChatScript/tree/master/WIKI

About JSON, the doc is:

https://github.com/bwilcox-1234/ChatScript/blob/master/WIKI/ChatScript-Json.md


giorgio

 

 

 
  login or register to react