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

How to store precise user variable in distant DB
 
 

Hello people,
by reading the different elements of the documentation (in particular concerning JSON and OOB) I can not determine what would be the easiest way to recover user variables _0 stored in $XXX and insert them in the columns of a database provided for this purpose.
I need a REST API on the DB side, but on the side of ChatScript (which is a server), is it possible to simply create a JSON object collecting my $name and $city directly in a topic and doing POST ?
After I imagine that a modification of the file simpletopic.top is necessary to declare the modalities of connection with the database?

Am I going in the right direction?
I am surprised to find only information about the receipt of OOB data in the documentation and almost nothing on the mail, including in the forum.
Maybe I read it wrong ...

 

 
  [ # 1 ]

Simple answer, yes.

If your remote store has a REST api then you can just build the payload in JSON in ChatScript and use ^jsonopen() to interact with that API. We use this extensively, not only to retrieve additional data but also to insert/update/delete.

 

 
  [ # 2 ]

Thanks for your reply Andy. What you say confirms what I thought I understood. It is astonishing that the documentation, although very detailed and very precise, does not give examples on this subject. Maybe a “Howto” could do it.

So let’s go: how to store a user variable memorized as $myvar =_0 in a database.

ChatScript manuals invoked:
- Installing-and-Updating-ChatScript
- ChatScript-Json
- ChatScript-System-Functions-Manual

Prerequisites :Have a ChatScript server, a Postgres database with a REST API based.

ChatSCript side:

1. prepare JSON to build the payload (issue a JSON string of the content of the variable to be broadcast)
In case your $myvar variable should be stored in your mycolumn column

$$jsonmyvar = ^jsonparse("{ mycolumn: $myvar }"

CS should build JSON structure like

{"mycolumn""myvarcontent"

2. Post the message to the REST API.
- open a rule to react u : ()
- set URL of your REST API
- Set your login
- declare header
- POST your data through the API

u: ()  
$$url = ^”\“http://myHost:myIP/\”“
$$user_agent = ^“myUserName User-AgentMozilla/5.0 (compatibleMSIE 10.0Windows NT 6.2WOW64Trident/6.0)
$header 
= ^” ~Acceptapplication/json “
^jsonopen(transient POST $$url $$jsonmyvar $header $$user_agent


Can it work this way ?
sorry for the “beginner aspect” of the thing

 

 
  [ # 3 ]

1. prepare JSON
I am not a huge fan of using ^jsonparse() to create a JSON object from a string directly in CS script because it can get messy with nulls and escaping characters and ungainly with deep structures.
I prefer:

$$jsonmyvar = ^jsoncreate(transient object)
$
$jsonmyvar.mycolumn $myvar 

Note: Json dotted syntax never generates a fail in CS

2. Post the message
I don’t know your full situation but a rule is not strictly necessary here. You could build an utility topic that you trigger by a ^respond() to invoke some common functionality, but that is a bit of overkill. It is easier to write your own macro to do a common function that can then be called from anywhere.

outputmacro: ^postMyMessage($_data)
{
    $_url 
= ^"http://myHost:myIP"
    
$_userAgent = ^"User-Agent: %bot, ChatScript”
    
$_header = ^” ~Accept: application/json 
                  
$_userAgent
                  ~Content-Type: application/json “
    
$_response = ^jsonopen(transient POST $_url $_data $_header $_userAgent
    ^return(
$_response)

Note: that CS can now allow the data parameter be passed in ^jsonopen() as a JSON object, instead of a string. The engine will do the serialization correctly. This is useful if your payload exceeds the CS output buffer size (about 80K).

So now that you’ve constructed your payload object, got a general function (though the above version is minus error handling and could be made even more general purpose), you just need to call it after you have prepared your JSON.

$_response = ^postMyMessage($$jsonmyvar

And there you go.

 

 

 
  [ # 4 ]

Thank you so much for your help Andy I will test all that.

Perhaps a series of Howto (or FAQ) could complement the documentation effectively, what do you say?

 

 
  [ # 5 ]

I’ll add it to my list grin

 

 
  login or register to react