Calls being answered and then disconnected after several seconds

After several days, I found the right solution to my problem! The problem was that a call is being disconnected after several seconds if there is no audio stream. In my case, it happens in both directions - when the caller is speaking for a long time without interruption (and being recorded with Record() func), and when the asterisk is handling logic for a long time, in my case 10+ seconds.

The solution is fairly simple:

  1. Use Local channels (Dial(Local…)) to handle the external logic (calling 3rd party services and stuff). This also helps with debugging.

  2. In order to keep the audio stream ongoing, I have to play some audio no matter if the user is speaking or asterisk is running some CURL requests. The MusicOnHold() func is great but it stops the dialplan. By chance, I found that there is another func called StartMusicOnHold() which doesn’t stop the dialplan and continue its execution! It does not persist through channel switching (point 1 above) but you can start it in main and sub (local) channel independently. Also, it can be stopped with the same command StopMusicOnHold().
    Code:
    exten => s,n,StartMusicOnHold(silence)

  3. In my case, in order to have a ‘silence’ music playing almost all the time, I had to have a recording of ‘silence’ and then modify the musiconhold.conf to include a class of [silence] and where that .wav file is stored. In my case, that is:
    [silence]
    mode = files
    directory = moh/silence


which effectively gets to /var/lib/asterisk/moh/silence/silence.wav

Again, thank you david551, you pointed me in the right direction!

I hope this helps someone in the future.