{"id":1154,"date":"2024-06-23T23:28:37","date_gmt":"2024-06-23T15:28:37","guid":{"rendered":"https:\/\/crazylab.cloud\/crazylab\/?page_id=1154"},"modified":"2024-06-24T01:35:51","modified_gmt":"2024-06-23T17:35:51","slug":"chatbot","status":"publish","type":"page","link":"https:\/\/crazylab.cloud\/crazylab\/?page_id=1154","title":{"rendered":"chatbot"},"content":{"rendered":"    <div id=\"custom-chatbot\">\n        <form id=\"chatbot-form\">\n            <textarea id=\"question\" name=\"question\" placeholder=\"\u8f38\u5165\u60a8\u7684\u554f\u984c...\" rows=\"3\"><\/textarea> <!-- \u4fee\u6539\u70ba\u591a\u884c\u6587\u672c\u6846 -->\n            <button type=\"button\" id=\"submit-btn\">\u63d0\u4ea4<\/button>\n            <button type=\"button\" id=\"toggle-listening-btn\">\u555f\u7528\u514d\u63d0\u6a21\u5f0f<\/button>\n            <button type=\"button\" id=\"toggle-tts-btn\">\u555f\u7528TTS\u6a21\u5f0f<\/button>\n            <button type=\"button\" id=\"view-history-btn\">\u67e5\u770b\u554f\u7b54\u8a18\u9304<\/button>\n        <\/form>\n        <div id=\"chatbot-reply\"><\/div>\n        <div id=\"history-dialog\" title=\"\u554f\u7b54\u8a18\u9304\">\n            <div id=\"history-content\"><\/div>\n            <button id=\"close-history\">\u95dc\u9589\u8a18\u9304<\/button>\n            <button id=\"clear-history\">\u6e05\u9664\u8a18\u9304<\/button>\n            <button id=\"copy-history\">\u8907\u88fd\u8a18\u9304<\/button>\n        <\/div>\n    <\/div>\n    <script>\n        jQuery(document).ready(function($) {\n            var isListeningModeEnabled = true;\n            var isTtsEnabled = true;\n            var recognition;\n            var userSpeakingTimeout;\n            var speechSynthesis = window.speechSynthesis;\n            var isSpeaking = false;\n            var thinkingMessage = '<span id=\"thinking-message\">\u6b63\u5728\u601d\u8003...<\/span>';\n            const language = ''; \/\/ \u53d6\u5f97\u8a9e\u8a00\u4ee3\u78bc\n            \t\t\t\n\t\t\t\/\/ \u5728 startRecognition \u548c stopRecognition \u51fd\u6578\u4e2d\u52a0\u5165\u5224\u65b7\u908f\u8f2f\n\t\t\tfunction startRecognition() {\n  \t\t\t  if (!isSpeaking) { \/\/ \u78ba\u4fdd\u5728 TTS \u5b8c\u6210\u4e4b\u524d\u4e0d\u555f\u52d5 STT\n  \t\t\t     recognition = new webkitSpeechRecognition();\n \t\t\t     recognition.lang = language;\n  \t\t\t     recognition.continuous = true;\n   \t\t\t     recognition.interimResults = true;\n\n   \t\t\t     recognition.onresult = function(event) {\n   \t\t\t         var finalTranscript = '';\n \t\t\t           for (var i = event.resultIndex; i < event.results.length; ++i) {\n \t\t\t         \t\tif (event.results[i].isFinal) {\n  \t\t\t            \t\tfinalTranscript += event.results[i][0].transcript;\n   \t\t\t             \t}\n \t\t\t           }\n  \t\t\t          if (finalTranscript) {\n  \t\t\t              $('#question').val(finalTranscript);\n   \t\t\t             userSpeakingTimeout = setTimeout(sendQuestion, 2000); \/\/ \u5169\u79d2\u975c\u9ed8\u5f8c\u81ea\u52d5\u63d0\u4ea4\u554f\u984c\n   \t\t\t         }\n  \t\t\t     };\n\n   \t\t\t     recognition.start();\n\n \t\t\t     recognition.onend = function() {\n\t\t\t            if (isListeningModeEnabled && !isSpeaking) {\n  \t\t\t              startRecognition(); \/\/ \u78ba\u4fdd\u5728\u555f\u7528\u514d\u63d0\u6a21\u5f0f\u6642\u6301\u7e8c\u8046\u807d\n \t\t\t           }\n  \t\t\t      };\n\t\t\t  }\n\t\t\t}\n\n\t\t\tfunction stopRecognition() {\n\t\t\t    if (recognition) {\n\t\t\t        recognition.stop();\n\t\t    \t    recognition = null;\n\t\t  \t  }\n\t\t\t}\n\n            function toggleListeningMode() {\n                isListeningModeEnabled = !isListeningModeEnabled;\n                if (isListeningModeEnabled) {\n                    startRecognition();\n                    $('#toggle-listening-btn').text('\u7981\u7528\u514d\u63d0\u6a21\u5f0f');\n                } else {\n                    stopRecognition();\n                    $('#toggle-listening-btn').text('\u555f\u7528\u514d\u63d0\u6a21\u5f0f');\n                }\n            }\n            \n            function toggleTtsMode() {\n                isTtsEnabled = !isTtsEnabled;\n                if (isTtsEnabled) {\n                    $('#toggle-tts-btn').text('\u7981\u7528TTS\u6a21\u5f0f');\n                } else {\n                    $('#toggle-tts-btn').text('\u555f\u7528TTS\u6a21\u5f0f');\n                }\n            }\n            \n            function speakText(text) {\n                if (!isSpeaking && isTtsEnabled) {\n                    var utterance = new SpeechSynthesisUtterance(text);\n                    \/\/utterance.lang = language; \/\/ \u8a2d\u7f6e\u8a9e\u8a00 --> This variable is NOT working (NOT put correctly)\n\t\t\t\t\tutterance.lang = 'zh-HK'; \/\/ \u5ee3\u6771\u8a71\u8a9e\u8a00\u4ee3\u78bc\n\/*\t\t\t\t\n\t\t\t    \t\/\/ \u8a2d\u7f6eTTS\u8a9e\u97f3 - \u9078\u64c7\u7279\u5b9a\u7684\u5ee3\u6771\u8a71\u8a9e\u97f3 - Added by Cloud - START\n\t\t\t    \t\/\/ No sournd after putting the block of following code !!!\n\t\t\t\t\t\/\/ Refer to: https:\/\/chatgpt.com\/c\/2c06c5da-e0e0-4396-86b8-beb0cc27ec75\n\t\t\t        var voices = speechSynthesis.getVoices();\n\t\t\t        var selectedVoice = voices.find(function(voice) {\n\t\t\t            return voice.lang === language || (voice.name && voice.name.includes(voicelang));\n \t\t\t        });\n\n\t\t\t        if (selectedVoice) {\n \t\t\t            utterance.voice = selectedVoice;\n\t\t\t        }\n\t\t\t\t\t\/\/ Added by Cloud - END\n*\/\n\t\t\t\t\t\/\/ New Code START\n\t\t\t\t\t\/\/ \u5728 speakText \u51fd\u6578\u4e2d\u52a0\u5165\u5ef6\u6642\u91cd\u555f STT \u7684\u908f\u8f2f\n        \t\t\tutterance.onstart = function() {\n      \t\t\t    \tisSpeaking = true;\n     \t\t\t    \tstopRecognition(); \/\/ \u5728 TTS \u958b\u59cb\u6642\u505c\u6b62 STT\n    \t\t\t    };\n     \t\t\t\tutterance.onend = function() {\n    \t\t\t\t\tisSpeaking = false;\n       \t\t\t\t\tsetTimeout(function() {\n        \t\t        if (isListeningModeEnabled) {\n                  \t\t  startRecognition(); \/\/ \u5728 TTS \u7d50\u675f\u5f8c\u5ef6\u6642\u91cd\u555f STT\n                \t\t}\n            \t\t\t}, 500); \/\/ 500 \u6beb\u79d2\u5ef6\u6642\n        \t\t\t};\n \t\t\t\t\tutterance.onerror = function(event) {\n            \t\tconsole.error('TTS error:', event);\n\t\t\t        };\n        \t\t\tspeechSynthesis.speak(utterance);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\/\/ New Code END\t\t\t\n\n            $('#submit-btn').click(function() {\n                sendQuestion();\n            });\n            \n            function sendQuestion() {\n                var question = $('#question').val();\n                if (!question.trim()) {\n                    alert('\u8acb\u8f38\u5165\u554f\u984c');\n                    return;\n                }\n                \n                $('#chatbot-reply').html(thinkingMessage);\n                \n                $.ajax({\n                    url: 'https:\/\/crazylab.cloud\/crazylab\/wp-admin\/admin-ajax.php',\n                    type: 'POST',\n                    data: {\n                        action: 'custom_chatbot_query',\n                        question: question\n                    },\n                    success: function(response) {\n                        if (response.success) {\n                            $('#chatbot-reply').html(response.data);\n                            speakText(response.data);\n                        } else {\n                            $('#chatbot-reply').html('\u932f\u8aa4: ' + response.data);\n                        }\n                    },\n                    error: function(xhr, status, error) {\n                        $('#chatbot-reply').html('\u932f\u8aa4: ' + error);\n                    }\n                });\n            }\n\n            $('#toggle-listening-btn').click(function() {\n                toggleListeningMode();\n            });\n\n            $('#toggle-tts-btn').click(function() {\n                toggleTtsMode();\n            });\n            \n            $('#view-history-btn').click(function() {\n                $.ajax({\n                    url: 'https:\/\/crazylab.cloud\/crazylab\/wp-admin\/admin-ajax.php',\n                    type: 'POST',\n                    data: {\n                        action: 'get_chatbot_history'\n                    },\n                    success: function(response) {\n                        if (response.success) {\n                            var historyContent = '<div class=\"history-records\">';\n                            response.data.forEach(function(record) {\n                                historyContent += '<p><strong>\u554f\u984c:<\/strong> ' + record.question + '<\/p>';\n                                historyContent += '<p><strong>\u56de\u7b54:<\/strong> ' + record.reply + '<\/p><hr>';\n                            });\n                            historyContent += '<\/div>';\n                            $('#history-content').html(historyContent);\n                            $('#history-dialog').show();\n                        } else {\n                            alert('\u7121\u6cd5\u7372\u53d6\u6b77\u53f2\u8a18\u9304');\n                        }\n                    },\n                    error: function(xhr, status, error) {\n                        alert('\u932f\u8aa4: ' + error);\n                    }\n                });\n            });\n\n            $('#close-history').click(function() {\n                $('#history-dialog').hide();\n            });\n\n            $('#clear-history').click(function() {\n                if (confirm('\u60a8\u78ba\u5b9a\u8981\u6e05\u9664\u6240\u6709\u6b77\u53f2\u8a18\u9304\u55ce\uff1f')) {\n                    $.ajax({\n                        url: 'https:\/\/crazylab.cloud\/crazylab\/wp-admin\/admin-ajax.php',\n                        type: 'POST',\n                        data: {\n                            action: 'clear_chatbot_history'\n                        },\n                        success: function(response) {\n                            if (response.success) {\n                                $('#history-content').html('');\n                                alert('\u6b77\u53f2\u8a18\u9304\u5df2\u6e05\u9664');\n                            } else {\n                                alert('\u7121\u6cd5\u6e05\u9664\u6b77\u53f2\u8a18\u9304');\n                            }\n                        },\n                        error: function(xhr, status, error) {\n                            alert('\u932f\u8aa4: ' + error);\n                        }\n                    });\n                }\n            });\n\n            $('#copy-history').click(function() {\n                var historyText = '';\n                $('#history-content .history-records').each(function() {\n                    historyText += $(this).text() + '\\n';\n                });\n\n                var $temp = $('<textarea>');\n                $('body').append($temp);\n                $temp.val(historyText).select();\n                document.execCommand('copy');\n                $temp.remove();\n\n                alert('\u6b77\u53f2\u8a18\u9304\u5df2\u8907\u88fd');\n            });\n\n            if (isListeningModeEnabled) {\n                startRecognition();\n            }\n\n            if (isTtsEnabled) {\n                $('#toggle-tts-btn').text('\u7981\u7528TTS\u6a21\u5f0f');\n            }\n        });\n    <\/script>\n    \n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"page-templates\/template-full.php","meta":{"h5ap_radio_sources":[],"footnotes":""},"class_list":["post-1154","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/pages\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1154"}],"version-history":[{"count":1,"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/pages\/1154\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=\/wp\/v2\/pages\/1154\/revisions\/1155"}],"wp:attachment":[{"href":"https:\/\/crazylab.cloud\/crazylab\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}