const trv_hp_tia_markdownCoverter = new showdown.Converter({simplifiedAutoLink: true, strikethrough: true, tables:true, tasklists: true, emoji:true});
var trv_hp_tia_loading = false;
var trv_hp_mediaRecorder = null;
var trv_hp_mediaRecorderVisualizer = null;
var trv_hp_audioChunks = [];
function trv_hp_tia_addUserMessage(message) {
$(".trv_hp_tia_message_container .trv_hp_tia_waiting_container").before('
'+message.replace(/(?:\r\n|\r|\n)/g, '
')+'
');
}
function trv_hp_tia_addTiaMessage(message) {
$(".trv_hp_tia_message_container .trv_hp_tia_waiting_container").before(''+trv_hp_tia_markdownCoverter.makeHtml(message)+'
');
}
function trv_hp_scrollTIAToLastQuestion()
{
$(".trv_hp_tia_message_container").animate({
scrollTop: $(".trv_hp_tia_message_container .trv_hp_tia_user_question:last")[0].offsetTop-10
}, 400);
}
function trv_hp_tia_toggleInputField(disabled)
{
if (disabled)
{
$("#trv_hp_tia_input").val("").attr("placeholder", "Anfrage wird bearbeitet ...").prop("disabled", true);
$(".trv_hp_tia_waiting_container").css("display","flex");
}
else
{
$(".trv_hp_tia_waiting_container").css("display","none");
$("#trv_hp_tia_input").attr("placeholder", "Ihr Anliegen ...").prop("disabled", false).focus();
}
}
function trv_hp_tia_initSession()
{
$.trv_postJson({
ajax_type: "INIT_TIA_SESSION",
disableErrorOnReload: true,
blockScreen: false,
full_path: null,
url: "/tia/ajax/ajax_tia.php"
});
}
function trv_hp_tia_sendRequest(text)
{
if (text != "" && !trv_hp_tia_loading)
{
trv_hp_tia_loading = true;
$("#trv_hp_tia_input").val("").trigger("keyup");
trv_hp_animateStartPage();
trv_hp_tia_addUserMessage(text);
trv_hp_tia_toggleInputField(true);
trv_hp_scrollTIAToLastQuestion();
$.trv_postJson({
ajax_type: "SEND_TIA_REQUEST",
blockScreen: false,
full_path: null,
url: "/tia/ajax/ajax_tia.php",
parameter: {
text: text
},
onSuccess: function(data)
{
trv_hp_tia_addTiaMessage(data.answer);
localStorage.setItem("trv_hp_userSuchprofilID", data.suchprofileID);
trv_hp_tia_toggleInputField(false);
$(".trv_hp_tia_answer:last a").attr("target","_blank");
trv_hp_scrollTIAToLastQuestion();
trv_hp_tia_loading = false;
}
});
}
}
function trv_hp_tia_toggleAssistant()
{
if ($(".trv_hp_tia_container:visible").length)
{
$('.trv_hp_tia_container').fadeOut(400);
}
else
{
$('.trv_hp_tia_container').fadeIn(400,function()
{
$('#trv_hp_tia_input').focus();
});
}
}
function trv_hp_init_speachToText() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
{
$(".trv_hp_tia_microphon").show();
}
}
async function trv_hp_startSpeachRecording()
{
$("#trv_hp_tia_input").prop("disabled", true);
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
trv_hp_mediaRecorderVisualizer = await createMicAmplitudeVisualizer("#trv_hp_micWave", {
width: ($("#trv_hp_tia_input").width()-10),
color: "#f2f2f2",
background: "#3d4750",
innerHTML: ''
}).catch(console.error);
trv_hp_mediaRecorder = new MediaRecorder(stream);
trv_hp_audioChunks = [];
trv_hp_mediaRecorder.ondataavailable = function(e) {trv_hp_audioChunks.push(e.data);};
trv_hp_mediaRecorder.start();
$(".trv_hp_tia_microphon").addClass("trv_hp_mic_active");
}
function trv_hp_stopSpeachRecording()
{
return new Promise((resolve) => {
if (!trv_hp_mediaRecorder) return resolve();
trv_hp_mediaRecorder.onstop = function() {
resolve();
};
trv_hp_mediaRecorder.stop();
// Mikrofon freigeben
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
$(".trv_hp_tia_microphon").removeClass("trv_hp_mic_active");
if (trv_hp_mediaRecorderVisualizer) {
trv_hp_mediaRecorderVisualizer.stop();
$("#trv_hp_micWave").hide();
}
$("#trv_hp_tia_input").prop("disabled", false).focus();
});
}
function trv_hp_toggleRecodring()
{
if ($(".trv_hp_tia_microphon").hasClass("trv_hp_mic_active"))
{
trv_hp_sendSpeachRecording();
}
else
{
trv_hp_startSpeachRecording();
}
}
async function trv_hp_sendSpeachRecording()
{
await trv_hp_stopSpeachRecording();
$("#trv_hp_tia_input").prop("disabled", true);
$(".trv_hp_transcription_wait").show();
if (trv_hp_audioChunks.length === 0)
{
alert("Keine Aufnahme vorhanden.");
return;
}
const webmBlob = new Blob(trv_hp_audioChunks, { type: 'audio/webm' });
let formData = new FormData();
formData.append('audio', webmBlob, 'trancription.webm');
$.ajax({
url: '/tia/ajax/tia_transcribe.php',
type: 'POST',
dataType: "json",
data: formData,
processData: false,
contentType: false,
success: function(response) {
if (response.text)
{
if ($("#trv_hp_tia_input").val() == "")
{
$("#trv_hp_tia_input").val(response.text).trigger("keyup").focus();
}
else
{
$("#trv_hp_tia_input").val($("#trv_hp_tia_input").val()+" "+response.text).trigger("keyup").focus();
}
}
},
error: function(xhr) {
$('#trv_hp_tia_input').val("Serverfehler: " + xhr.responseText);
},
complete: function() {
$(".trv_hp_transcription_wait").hide();
$("#trv_hp_tia_input").prop("disabled", false).focus();
}
});
}
function trv_hp_animateStartPage()
{
if ($(".trv_hp_startpage_slogan").length)
{
$(" .trv_hp_top_slogan").fadeOut(100);
$(".trv_hp_tia_message_container").css("position", "absolute");
$(".trv_hp_starpage_buttonbar, .trv_hp_startpage_slogan").fadeOut(400, function(){
$(this).remove();
$(".trv_hp_tia_input_container").addClass("trv_hp_tia_input_container_down").animate({"bottom": 110},400);
});
}
}