This JavaScript will recover the message sent by the user if they receive an error:
document.addEventListener('DOMContentLoaded', () => { const startTime = Date.now(); const maxWaitTime = 3000; // 3 seconds const initializeWhenReady = () => { const mwaiInputDivs = document.querySelectorAll('div.mwai-input-text'); if (mwaiInputDivs.length > 0 && typeof MwaiAPI !== 'undefined') { mwaiInputDivs.forEach(mwaiInputDiv => { let lastSent = ''; const textarea = mwaiInputDiv.querySelector('textarea'); textarea?.addEventListener('input', () => { if (textarea.value.trim()) lastSent = textarea.value; }); setInterval(() => { const errorDivs = mwaiInputDiv.closest('.mwai-chatbot').querySelectorAll('div.mwai-error'); errorDivs.forEach(div => { if (div.querySelector('span') && lastSent && !textarea.value.trim()) { const chatbotElement = mwaiInputDiv.closest('.mwai-chatbot'); const botId = chatbotElement ? chatbotElement.id.replace('mwai-chatbot-', '') : ''; (botId ? MwaiAPI.getChatbot(botId) : MwaiAPI.chatbots[0]).ask(lastSent, false); } }); }, 500); }); } else if (Date.now() - startTime < maxWaitTime) { requestAnimationFrame(initializeWhenReady); } }; initializeWhenReady(); }, true);
** Updated **
Now passes through query limit errors and shows the real errors to admins.
This PHP will allow you to change the text that displays for each error:
add_filter( 'mwai_ai_exception', function ( $exception ) { if (current_user_can('administrator')) { return $exception; } // admin should see the real error $error_messages = array( // will look for 'overloaded' in the exception message and return a specific message if found 'Overloaded' => 'The system is currently overloaded. Please try again in 10 seconds.', 'Some Other Error' => 'Some other error message.', ); $use_email = false; // will use support_url link if false $my_email = ''; // set your email address here - leave blank to use admin email $admin_email = $my_email ? $my_email : get_option('admin_email'); $support_url = 'https://www.insightengine.online/contact-me/'; // optional support URL $link_url = $use_email ? 'mailto:' . $admin_email : $support_url; // change this to suit your needs $fallback_message = 'An error has occurred. Please [contact me](' . $link_url . ') if this continues.'; global $mwai_core; $limits = $mwai_core->get_option( 'limits' ); $skip_errors = isset( $limits['enabled'] ) && $limits['enabled'] ? array($limits['system']['overLimitMessage'], $limits['users']['overLimitMessage'], $limits['guests']['overLimitMessage']) : array(); if (in_array($exception, $skip_errors)) { return $exception; } try { foreach ($error_messages as $error_key => $error_message) { if (stripos($exception, $error_key) !== false) { return $error_message; } } return $fallback_message; } catch ( Exception $e ) { error_log( $e->getMessage() ); } return $exception; } );
Edit the commented settings in the PHP before placing it on your website.
Note that $admin_email will use the “Administration Email Address” set in your WordPress settings.
Let’s enhance your business! I can help you develop custom AI engine extensions or broader AI strategies.
Contact me.
Leave a Reply