call-bot / app /static /test.html
userIdc2024's picture
deploy call autoresponder
198045e verified
Raw History Blame Contribute Delete
8.41 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Call</title>
<style>
:root{--bg:#f6f7f9;--panel:#fff;--ink:#16181d;--muted:#667085;--line:#e4e7ec;--accent:#2f6fed;--live:#d92d20;--bot:#eef3fe;--caller:#f2f4f7}
@media (prefers-color-scheme:dark){:root{--bg:#0f1115;--panel:#171a21;--ink:#e8eaee;--muted:#98a2b3;--line:#272b35;--accent:#6b9bff;--live:#ff6b5e;--bot:#1b2540;--caller:#20242d}}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:14px/1.45 system-ui,-apple-system,Segoe UI,Roboto,sans-serif}
header{display:flex;gap:12px;align-items:center;padding:14px 20px;border-bottom:1px solid var(--line);background:var(--panel)}
header h1{font-size:16px;margin:0 auto 0 0}header a{color:var(--accent);text-decoration:none}
main{max-width:760px;margin:0 auto;padding:20px 16px}
.card{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:16px;margin-bottom:16px}
.row{display:flex;flex-wrap:wrap;gap:10px;align-items:center}
button,select,input[type=text]{font:inherit;padding:8px 12px;border-radius:8px;border:1px solid var(--line);background:var(--panel);color:var(--ink)}
button{cursor:pointer}button.primary{background:var(--accent);border-color:var(--accent);color:#fff}button.danger{background:var(--live);border-color:var(--live);color:#fff}
button:disabled{opacity:.45;cursor:default}
input[type=text]{flex:1;min-width:180px}
.meter{flex:1;min-width:120px;height:8px;background:var(--caller);border-radius:99px;overflow:hidden}
.meter i{display:block;height:100%;width:0;background:var(--accent);transition:width .08s}
.status{color:var(--muted);font-size:13px}
.ev{display:flex;gap:10px;padding:6px 0;border-bottom:1px dashed var(--line)}
.ev time{color:var(--muted);font-variant-numeric:tabular-nums;min-width:64px}
.bubble{padding:6px 10px;border-radius:10px;background:var(--caller);max-width:80%}.bubble.bot{background:var(--bot)}
.meta{color:var(--muted);font-size:12px}
.tip{color:var(--muted);font-size:13px;margin:8px 0 0}
</style>
</head>
<body>
<header><h1>Test Call</h1><a href="/dashboard">Dashboard →</a></header>
<main>
<div class="card">
<div class="row">
<select id="mode">
<option value="bot">Caller on bot list</option>
<option value="voicemail">Unknown caller (voicemail)</option>
</select>
<button class="primary" id="start">📞 Start test call</button>
<button class="danger" id="end" disabled>End</button>
<div class="meter" title="mic level"><i id="lvl"></i></div>
</div>
<div class="row" style="margin-top:10px">
<label><input type="checkbox" id="mute" checked> Mute mic while the bot is talking (use if you're not on headphones)</label>
</div>
<p class="status" id="status">Not connected.</p>
</div>
<div class="card">
<div class="row">
<input type="text" id="say" placeholder="…or type what a caller would say, e.g. aap kaun bol rahe ho" disabled>
<button id="send" disabled>Send</button>
</div>
<p class="tip">Speaking tests the full chain (voice detection → speech-to-text → matching → your clip). Typing skips speech-to-text, so it works even without any API key.</p>
</div>
<div class="card"><div id="events"><div class="meta">Transcript appears here.</div></div></div>
</main>
<script>
const $=s=>document.querySelector(s);
const esc=s=>(s||'').replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));
const fmtT=ts=>new Date(ts*1000).toLocaleTimeString([], {hour:'2-digit',minute:'2-digit',second:'2-digit'});
let ws=null, ctx=null, stream=null, node=null, src=null, nextT=0, playing=[], first=true;
function evHtml(e){
const t=`<time>${fmtT(e.ts)}</time>`;
if(e.kind==='caller_said') return `<div class="ev">${t}<div><div class="bubble">🗣 ${esc(e.text)}</div><div class="meta">→ ${e.intent?'<b>'+esc(e.intent)+'</b>':'no match'} (${esc(e.detail)})</div></div></div>`;
if(e.kind==='voicemail') return `<div class="ev">${t}<div class="bubble">📩 ${esc(e.text)}</div></div>`;
if(e.kind==='bot_played') return `<div class="ev">${t}<div class="bubble bot">▶ bot played <b>${esc(e.intent)}</b> <span class="meta">${esc(e.detail)}</span></div></div>`;
return `<div class="ev">${t}<div class="meta">${esc((e.kind||'').replace(/_/g,' '))} ${esc(e.intent||'')} ${esc(e.detail||'')}</div></div>`;}
function addEv(e){if(first){$('#events').innerHTML='';first=false}$('#events').insertAdjacentHTML('beforeend',evHtml(e));$('#events').lastElementChild?.scrollIntoView({block:'nearest'})}
const WORKLET=`class Cap extends AudioWorkletProcessor{process(i){const c=i[0]&&i[0][0];if(c)this.port.postMessage(c.slice(0));return true}}registerProcessor('cap',Cap);`;
// mic (any rate) -> 8 kHz PCM16, sent in 100 ms packets
let acc=[], ratio=1, pos=0;
function onMic(f32){
let peak=0; for(let i=0;i<f32.length;i++){const a=Math.abs(f32[i]); if(a>peak)peak=a}
$('#lvl').style.width=Math.min(100,peak*140)+'%';
const botTalking=ctx.currentTime < nextT+0.3;
const silence=$('#mute').checked && botTalking;
for(; pos<f32.length; pos+=ratio){
const i=Math.floor(pos), fr=pos-i, a=f32[i], b=f32[Math.min(i+1,f32.length-1)];
acc.push(silence?0:Math.max(-1,Math.min(1,a+(b-a)*fr)));
}
pos-=f32.length;
if(acc.length>=800){
const out=new Int16Array(acc.length); for(let i=0;i<acc.length;i++)out[i]=acc[i]*32767;
acc=[]; if(ws&&ws.readyState===1)ws.send(out.buffer);
}
}
function playPcm(buf){
const pcm=new Int16Array(buf), b=ctx.createBuffer(1,pcm.length,8000), ch=b.getChannelData(0);
for(let i=0;i<pcm.length;i++)ch[i]=pcm[i]/32768;
const s=ctx.createBufferSource(); s.buffer=b; s.connect(ctx.destination);
if(nextT<ctx.currentTime+0.05)nextT=ctx.currentTime+0.08;
s.start(nextT); nextT+=b.duration; playing.push(s); s.onended=()=>{playing=playing.filter(x=>x!==s)};
}
function clearPlayback(){playing.forEach(s=>{try{s.stop()}catch(e){}});playing=[];nextT=ctx?ctx.currentTime:0}
async function start(){
$('#start').disabled=true; $('#status').textContent='Asking for microphone…';
try{
stream=await navigator.mediaDevices.getUserMedia({audio:{echoCancellation:true,noiseSuppression:true,autoGainControl:true}});
}catch(e){$('#status').textContent='Microphone blocked: '+e.message+' (you can still type).'; stream=null}
ctx=new AudioContext(); await ctx.resume(); ratio=ctx.sampleRate/8000; pos=0; acc=[];
if(stream){
await ctx.audioWorklet.addModule(URL.createObjectURL(new Blob([WORKLET],{type:'text/javascript'})));
src=ctx.createMediaStreamSource(stream); node=new AudioWorkletNode(ctx,'cap');
node.port.onmessage=e=>onMic(e.data); src.connect(node);
}
first=true; $('#events').innerHTML='<div class="meta">Connecting…</div>';
ws=new WebSocket((location.protocol==='https:'?'wss://':'ws://')+location.host+'/ws/testcall?mode='+$('#mode').value);
ws.binaryType='arraybuffer';
ws.onopen=()=>{$('#status').textContent=stream?'Connected — speak like the caller would.':'Connected — type below.';
$('#end').disabled=false;$('#say').disabled=false;$('#send').disabled=false;$('#mode').disabled=true};
ws.onmessage=m=>{
if(typeof m.data!=='string'){playPcm(m.data);return}
const e=JSON.parse(m.data);
if(e.event==='clear'){clearPlayback();return}
if(e.event==='hangup'){$('#status').textContent='Bot hung up.';return}
if(e.event==='ready')return;
addEv(e);
};
ws.onclose=e=>{$('#status').textContent=e.code===4401?'Session expired — reload the page and log in again.':'Call ended. Full log is on the dashboard.';stop(false)};
}
function stop(closeWs=true){
if(closeWs&&ws)ws.close(); ws=null;
if(node){node.port.onmessage=null;node.disconnect();node=null} if(src){src.disconnect();src=null}
if(stream){stream.getTracks().forEach(t=>t.stop());stream=null}
setTimeout(()=>{if(ctx){ctx.close();ctx=null}},1500);
$('#start').disabled=false;$('#end').disabled=true;$('#say').disabled=true;$('#send').disabled=true;$('#mode').disabled=false;$('#lvl').style.width='0';
}
function send(){const t=$('#say').value.trim(); if(!t||!ws)return; ws.send(JSON.stringify({text:t})); $('#say').value=''}
$('#start').onclick=start; $('#end').onclick=()=>stop(true);
$('#send').onclick=send; $('#say').onkeydown=e=>{if(e.key==='Enter')send()};
</script>
</body>
</html>