Hi @tigroumeow,
We are working on this…
The overall objective is to parse the percentage score of a login user, from the reply of the AI Engine plugin in WordPress, using the mwai_chatbot_reply filter. If the score is 60% or higher, the code should award x points to the user, using the Hook API of the myCred plugin. Additionally, a hook code is required in the myCred plugin to receive the call from the above function and award the points to the user. The sub-task is to add error handling to the code so that it can handle situations where the percentage score cannot be extracted or the myCred Hook API call fails.
Question: we have 2 chatbots in each post, how can we identify which mwai_chatbot_reply to be fetched, and how to link it with login user id?
Here is the initial code, but got error…Return type of Meow_MWAI_Reply::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/web/site/public_html/wp-content/plugins/ai-engine/classes/reply.php on line 15
function extract_percentage_score( $reply, $user_id ) {
// Use regular expressions to find the percentage score in the reply
if ( preg_match( '/Score: (\d+)%/', $reply, $matches ) ) {
$percentage_score = intval( $matches[1] );
// Handle case where percentage score cannot be extracted
if ( $percentage_score == 0 ) {
error_log( 'Failed to extract percentage score from AI Engine reply' );
return $reply;
}
// Store the user ID and percentage score in variables
$user_id = get_current_user_id();
$score = $percentage_score;
// Call the myCred Hook API to award points to the user if score is 60% or higher
if ( $score >= 60 ) {
$points = 50; // Change this to the number of points you want to award
$result = mycred_add( 'score', $user_id, $points, 'Scored 60% or higher' );
// Handle case where myCred Hook API call fails
if ( ! $result ) {
error_log( 'Failed to award points using myCred Hook API' );
} else {
// Log the Hook API call and number of points awarded
error_log( 'Awarded ' . $points . ' points to user ID ' . $user_id . ' using myCred Hook API' );
}
}
}
return $reply;
}
add_filter( 'mwai_chatbot_reply', 'extract_percentage_score', 10, 2 );
We are willing to pay for the hours of consulting if you are willing to guide us for this function.
Thanks