AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Unable to access JSON object
 
 

Hi, trying to learn to hook up CS to APIs ahead of a Meetup to demo and promote ChatScript to the PHP community.

$_tmp = ^jsonopenget $_url NULL $_userAgent)
$_tmp.RestResponse.result.capital is the answer to $_statename 

In the above example my query works and :trace jason shows the full json object.

....,...JSON response200 size288 {
  
"RestResponse" {
    
"messages" "Total [1] records found." ],
    
"result" [ {
      
"id" 1,
      
"country" "USA",
      
"name" "Alabama",
      
"abbr" "AL",
      
"area" "135767SKM",
      
"largest_city" "Birmingham",
      
"capital" "Montgomery"
    
} ]
  }
}
....,... 

Seeking $_tmp.RestResponse gives me a Jo-t1000 object.
Seeking tmp.RestResponse.result give me a Ja-t1002 array.

But seeking a $_tmp.RestResponse.result.capital gives me a blank, even though it is a definite array key. This is an issue I have bumped into with other json objects.

 

 

 
  [ # 1 ]

So?  $_tmp.RestResponse.result is an array, as you point out.  Have you indexed it at all?

$_tmp.RestResponse.result[0].capital

 

 
  [ # 2 ]

Thanks for that. I tried a different syntax before ($_tmp.RestResponse.[0]; $_tmp.RestResponse.result.[1] and a few others). I did not try your suggestion. Was about to when my bot stopped responding. I see this:

Luke:
   >
Luke:
   >
Luke:
   >:
reset
Luke
:
   >:
why
Luke
:
   >:
why
Luke
:
   >
ll
Luke
:
   >:
why
Luke
:
   > 

I have tried :build Luke (it builds but in the above non-responsive way); I have deleted all User logs and logs and done build 0 and build Luke again. Of course I also tried :reset.

I have reverted to code that was definitely working before and done all the above. Sometimes I find the CS engine mysterious. Any pointers? Thanks a lot.

 

 
  [ # 3 ]

so… delete TOPIC folder contents entirely and USERS folder contents entirely.  THen do :build 0
then do :build Luke

if that all seems normal, then try to run the bot.
If that doesnt work, run again and enter as login data

bruce:trace

then look in the bruce log and see what you see or send it to me

 

 
  [ # 4 ]

Hello,

I deleted TOPIC folder contents and USER folder contents (left folders intact), ran :build 0, got the “end of folder” line, exited, restarted, ran :build Luke = behaviour unchanged, bot unresponsive.

Restarted the bot, entered bruce:trace as login, got this:

Enter user namebruce:trace

Incoming data
bruce |   |
New 
User
User call 
^luke():()
.
$cs_token 4096(4096)
.    
$cs_token(4096) | 255(255) => 4351/0x00000000000010ff
.    $cs_token(4351) | 512(512) => 4863/0x00000000000012ff
.    $cs_token(4863) | 131072(131072) => 135935/0x00000000000212ff
.    $cs_token(135935) | 1024(1024) => 136959/0x00000000000216ff
.    $cs_token(136959) | 2048(2048) => 139007/0x0000000000021eff
.    $cs_token(139007) | 24576(24576) => 163583/0x0000000000027eff
.$cs_token(163583) += 67108864() =>  67272447/0x0000000004027eff
.    $cs_token(67272447) | 65536(65536) => 67337983/0x0000000004037eff
 Luke
:
.
$botprompt = ^"Luke: "(Luke: )
.
$singleresponse 1(1)
.
NOPROBLEM ^luke() => ``


Input0 to luke:


Input:
TokenControlDO_SUBSTITUTE_SYSTEM DO_NUMBER_MERGE DO_PROPERNAME_MERGE DO_DATE_MERGE DO_SPELLCHECK DO_INTERJECTION_SPLITTING DO_PARSE NO_WITHIN SPLIT_QUOTE


Original User Input
:
Tokenized into:
Actual used input:

After parse TokenFlagsUSERINPUT


Reply input
:
  
Pending topics:
Topic  is already current
Start
user:bruce bot:luke iprand:918 () ==>   When:Feb03'19-20:20:57  Version:9.0 Build0:Feb03'19-20:19:47 Build1:Feb03'19-20:19:40 0:Feb03'19-20:20:57 F:0 P:0
write out cache 
(0)
Luke:
   > 

 

 

 
  [ # 5 ]

In case it helps, this is the log when I input “test”:

Startuser:bruce bot:luke iprand:918 () ==>   When:Feb03'19-20:20:57  Version:9.0 Build0:Feb03'19-20:19:47 Build1:Feb03'19-20:19:40 0:Feb03'19-20:20:57 F:0 P:0
write out cache 
(0)
Luke:
   >
test
Incoming data
bruce luke test
read in USERS
/topic_bruce_luke.txt cache (0)

Loading user bruce bot luke
No rejoinder pending
uservar
$singleresponse=1
uservar
$botprompt=Luke:
uservar$cs_token=67337983
uservar
$cs_login=bruce
uservar
$cs_bot=Luke
uservar
$cs_randindex=918
uservar
$cs_trace=-1
uservar
$cs_time=0
user load completed normally


Input
1 to luketest


Input
test
TokenControl
DO_SUBSTITUTE_SYSTEM DO_NUMBER_MERGE DO_PROPERNAME_MERGE DO_DATE_MERGE DO_SPELLCHECK DO_INTERJECTION_SPLITTING DO_PARSE NO_WITHIN SPLIT_QUOTE


Original User Input
test
Tokenized into
test
Actual used input
test

  
*** Sentence starttest (1to test (1)
tag from 1 to 1
Not a sentence
Xref
1:test
1
:test
badparse Tagged POS 1 words
test (Noun_infinitive +Noun_singular Verb_infinitive Verb_present)
  
MainSentence:  PRESENT


Concepts
:

1test (raw):
   +~
noun_abstract
       
+~pos
         
+~internal_concepts
   
+~noun_infinitive
   
+~verb_present
       
+~verb_bits
   
+~verb_infinitive
   
+~noun
   
+~verb
   
+~noun_singular
       
+~normal_noun_bits
       
+~noun_bits
   
+~kindergarten
   
+~noun_phrase
   
+test
     
+trial~1
       
+effort~3
         
+activity~1
           
+act~1
             
+event~1
               
+psychological_feature~1
                 
+abstract_entity~1
                   
+entity~1
     
+trial~2
     
+test~3
       
+mental_measurement~1
         
+measure~3
     
+test~4
       
+cover~9
         
+natural_object~1
           
+whole~1
             
+object~1
               
+physical_entity~1
     
+tryout~2
       
+experimentation~2
         
+research~2
           
+problem_solving~1
             
+thinking~1
               
+higher_cognitive_process~1
                 
+operation~8
                   
+knowledge~1
     
+examination~5
       
+communication~2
     
+test~7
     
+test~8
     
+test~9
     
+test~10
     
+prove~8
     
+screen~16
     
+test~13
 
//
1test (canonical):  //

Sequences:

After parse TokenFlagsPRESENT USERINPUT FAULTY_PARSE NOT_SENTENCE


Reply input
test
  Pending topics
:
Topic  is already current
Respond
user:bruce bot:luke ip: () 1   test ==>   When:Feb03'19-20:29:41   F:6902
write out cache (0)
Luke:
   > 
 

 
  [ # 6 ]

I dont see any starting topic defined in the bot macro.  So CS has no idea where to go after instantiating the bot.

In Harry its this line:

# This sets the default topic to be using when running the system
^addtopic(~introductions)

 

 
  [ # 7 ]

I do have that though. My version of simple control:

# I used the harry bot as a template, but streamlined the code to purely deliver the basic functions of my microbot, 
# rather than be an actual Harry clone. Luke has text to speech enabled.

# this function is executed once for every new user chatting with Luke
outputmacroLuke()  # you get harry by default
$cs_token #DO_INTERJECTION_SPLITTING  | #DO_SUBSTITUTE_SYSTEM   | #DO_NUMBER_MERGE | #DO_DATE_MERGE  | #DO_PROPERNAME_MERGE  | #DO_SPELLCHECK  | #DO_PARSE 
$cs_token += #SPLIT_QUOTE | #NO_WITHIN
# outputmacro: ^getheader()
#   return (^"Content-Type: application/json
#   ~User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64)
#   ~Cache-Control: no-cache
#   ~Accept: application/json
#   ~Accept-Encoding: identity,*;q=0
#   ~Accept-Language: en-US,en;q=0.9")

^addtopic(~Free_Time)
$cs_control_main = ~control
# $cs_control_post = ~XPOSTPROCESS  # uncomment to enable talk
$userprompt = ^"%user: >"
$botprompt = ^"Luke: "
$singleResponse # issue only 1 output message
tabledefaultbot (^name)
^
createfact(^name defaultbot defaultbot)
DATA:
Luke

topic
: ~control system ()

u
: () 
 
gambit(~Free_Time)

u: () # main per-sentence processing
 
$_responseCount = %response
 
if ($singleResponse{$_responseCount 0} # disable this to respond to each sentence in a volley

 
$$currenttopic = %topic  # get the current topic at start of volley

 
if ( %response == $_responseCount {nofail(TOPIC ^rejoinder())# try for rejoinders. might generate an answer directly from what we are looking for.

 
if (%length == AND %response == $_responseCount )
 
{
  nofail
(TOPIC ^gambit($$currenttopic))  # gambit current topic since no input (usually start of conversation)
 
}

 
if (%response == $_responseCount{ nofail(TOPIC ^respond($$currenttopic)) # current topic tries to respond to his input

 
if (%response == $_responseCount# see if some other topic has keywords matching his input (given we have no response yet)
 
{
  
@= ^keywordtopics() # get topics referred in input
  
loop()
  
{
   
$$topic first(@8subject)
   
nofail(TOPIC ^respond($$topic))
   if (%
response != $_responseCount# stop when we find something to say
   
{
    
^end(RULE# we are done, this terminates the loop (not the rule)
   
}
  }
 } 


 
# if we have rejoinders for what we said OR we asked a question, stop here
 
if (%outputrejoinder OR %lastquestion
 
{
  end
(TOPIC)
 
}


 
if (%response == $_responseCount){ nofail(TOPIC ^gambit($$currenttopic)) # gambit from current topic even though no keywords matched
 
 
if (%response == $_responseCount
 

  
@= ^GambitTopics()  # all topics with gambits (excluding system topics)
  
loop()
  
{
   
$$topic pick(@8subject)
   
nofail(TOPIC ^Gambit($$topic)) # try a topic at random
   
if (%response != $_responseCount)
   
{
    
^end(RULE)
   
}
  }
 }
 
 
if (%response == $_responseCount
 
{
   
^repeat() 
   
[Luke nods off and starts to snore] [You suspect Luke can hear you but is ignoring you...] [Do you hear thatIt's the sound of absence.]
 }


topic: ~XPOSTPROCESS system () # gambits only - not allowed to write to  output - generates speech on windows -- for Linux you'
d want to install Festival (or equivalent) and call that instead.

t: (^query(direct_v chatoutput ? -? @9  ))  # get the sentences  (why is the object) -- no longer has quotes around it
 
loop()
 
{
  
$$tmp = ^last(@9subject# note last output which is quoted string
  
if (!%server AND %os == windows  AND !$SHUTUP)
  
{
   
$$tmp = ^"talk \"$$tmp\" "
   
popen($$tmp null)
  
}
        
if (!%server AND %os == mac  AND !$SHUTUP)
        
{
            
$$shellerror system(^"say \"$$tmp\" ")
        }
 } 
 

 
  [ # 8 ]

You have a commented out function getheader. Remove it entirely. somehow (engine bug ) its having issues with quotes swallowing multiple lines.

 

 
  [ # 9 ]

That was it. So the issue was that quotes were de-commenting the commented out code?

All good, and syntax of course works. Should unlock me for sundry JSON tasks, ta.

Thanks for investing the time in this quality of support. I hope to make it worthwhile by passing it on and encouraging the use of ChatScript.

One thing that might be worthwhile at some point would be to look through all the support queries here and privately that you may have had and create a Gotchas/Troubleshooting Wiki file. Might take a while but I imagine it would not be hard to get a couple of volunteers and it may well save you a lot of time in future and reduce the learning curve.

Gratefully,

Ismael

 

 
  [ # 10 ]

There is an interaction with the system looking at the code in your comments, seeing the double quotes that you extend across multiple lines, and not detecting the end until a later rule with double quotes.

There is a doc on common mistakes.  But how to troubleshoot would be another good manual

 

 
  login or register to react