How to connect IBM Watson Assistant to a MySQL Database?

202 Views Asked by At

So I have created a basic Watson Assistant Chatbot and a basic MySQL Database and I want to connect them together, for the Watson Assistant to extract something from it.

Here's the scenario as follows:

The Watson Assistant will ask the user to authenticate himself with his Client_ID and his password, if the Chatbot recognizes these credentials, it should great the user by his first name.

ex: "Welcome <first_name(from MySQL DB that I created)> how my I help you?".

Note that my MySQL DB contains these columns: client_id, first_name, last_name, password, email. I couldn't find anything none other than people using IBM Cloud resources such as DB2 which are not free to use.

I even tried this php code in the IBM Cloud Functions to provide a webhook:

<?php
function main(array $args) : array {

    // Retrieve client_id from Watson Assistant
    $clientid = $args['clientid'];

    // Set up MySQL database credentials
    $host = '';
    $port = ;
    $dbname = '';
    $username = '';
    $password = '';

    // Connect to MySQL database
    $conn = new mysqli($host, $username, $password, $dbname);

    // Check for connection errors
    if ($conn->connect_error) {
        return ['error' => 'Failed to connect to MySQL database: '.$conn->connect_error];
    }

    // Construct and execute MySQL query
    $sql = "SELECT first_name FROM clients WHERE client_id = $clientid";
    $result = $conn->query($sql);

    // Check for query errors
    if (!$result) {
        return ['error' => 'Failed to execute MySQL query: '.$conn->error];
    }

    // Retrieve result and return as array
    $row = $result->fetch_assoc();
    if ($row) {
        $response = ['first_name' => $row['first_name']];
    } else {
        $response = ['error' => 'Client ID not found in database'];
    }
    return $response;
}
?>

it showed this error:

{ "error": "The action did not return a dictionary or array." }

Please if there's a solution for my problem or if any other way to do what I asked for I would much appreciate it.

I tried everything and I got nothing.

0

There are 0 best solutions below