• Resolved theonturncloak

    (@theonturncloak)


    Hi,

    Is it possible to access the context variables outside of the plug in?

    I want to be able to use the context variables from chats in a database on my site, so is this possible and if so how do I go about doing this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor cognitiveclass

    (@cognitiveclass)

    Hi,

    In an upcoming update, we’ll be adding WordPress hooks for sending and receiving messages that will allow you to write PHP functions to access the context variables in addition to other data in the HTTP response. Will this satisfy your needs?

    Thread Starter theonturncloak

    (@theonturncloak)

    Hi,

    I believe it would! Any time frame as to when this update will be released?

    Plugin Contributor cognitiveclass

    (@cognitiveclass)

    It should be available by the end of this week. We’ll let you know as soon as it’s released.

    Thread Starter theonturncloak

    (@theonturncloak)

    Hi that’s great to know thank you!

    Do you recommend any tutorials for WordPress hooks?

    Plugin Contributor cognitiveclass

    (@cognitiveclass)

    Hi,

    The update has been released so you can get started with it now. The WordPress documentation has a detailed explanation of how hooks are used, though you can find a lot of other tutorials online.

    For your case, you can use our plugin’s hook by doing the following:

    1. Navigate to the wp-content/themes/yourthemename directory and open the functions.php file.
    2. Add the following line anywhere in the file, but replace ‘function_name’ with a function name of your choosing:
    add_action('watsonconv_message_parsed', 'function_name');
    3. Create a PHP function with the same name. This function will now be called every time a user receives a message from the chatbot, and it will be given the body of the HTTP response as an argument. This argument will be in array format, so you can access the context like this:

    function get_context_variables($response) {
        $context = $response['context'];
    }
    

    Feel free to ask if you have any questions.

    Thread Starter theonturncloak

    (@theonturncloak)

    Hi, so does that mean we can only access one context variable at a time? We can pull it and store it elsewhere on the site?

    Plugin Contributor cognitiveclass

    (@cognitiveclass)

    The “context” field in the HTTP response is an array containing the current value of each of the context variables. For example, if you have context variables called “var1” and “var2”, you can access them using the $context variable like this:

    $val1 = $context['var1'];
    $val2 = $context['var2'];

    You can then use PHP to store these values in your database. As you can tell, this will require some coding.

    Thread Starter theonturncloak

    (@theonturncloak)

    Fantastic, thank you very much!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Access Context Variables outside plugin?’ is closed to new replies.