context main {
  _X. => {
	Set(file_to_play=${RAND(10000,999999)});
	// I call an AGI that create the wav SLIN file in tmp/${file_to_play}
	Gosub(playback_time_tts,_X.,1("/var/lib/asterisk/sounds/tmp/${file_to_play}"));
  }
}

context detect_stop {
  _X. => {
	NoOp(In detect_stop context);
	Set(DB(${EXTEN}/p_chan)=${CHANNEL});
	Answer();
	Set(AGIEXITONHANGUP=yes);
	Set(AGISIGHUP=yes);
	Set(trigger_dictionary=detect_stop.list);
	//EAGI(trigger_word_google.py);
	// the crash also happens if EAGI runs an empty file
	EAGI(empty.py);
	SIPDtmfMode(inband);
	sendDTMF(1,,80,${DB(${EXTEN}/p_source)});
  }
h =>{ 
	NoOp(In detect_stop Hangup context ); 
	}
}

context playback_time_tts {
  _X. => {
	NoOp(In playback_time_tts context file_to_play=${file_to_play} ARG1=${ARG1});
	Set(DB(${file_to_play}/p_source)=${CHANNEL});
	Set(DB(${file_to_play}/exited_playback)=false);
	Originate(local/${file_to_play}@detect_stop,app,ChanSpy,"${CHANNEL},qwoE");
	SIPDtmfMode(inband);
	ControlPlayback(${ARG1},,,,1);
	Set(DB(${file_to_play}/exited_playback)=true);
	SoftHangup(${DB(${file_to_play}/p_chan)});
	return;
  }
  h => {
	NoOp(In playback_time_tts Hangup context );
	SoftHangup(${DB(${file_to_play}/p_chan)});
  }
}