[extensions.con] Checking if ZAP off-hook?

Hello

At some point in extensions.conf, I need to check if the FXO card is off-hook, but didn’t find an example of how to do this.

Here’s the code, where HERE shows where I’d like to put the GotoIf():

exten => s,1,Wait(2)
exten => s,n,GotoIf($[${LEN(${CALLERID(num)})} = 0]?nocid,1:cid,1)
       
;Prompt user for CID number
exten => nocid,1,Read(CALLERID(num),no_cid,10)
;At least 10 digits
exten => nocid,n,GotoIf($[${LEN(${CALLERID(num)})} < 10]?cid,1)
exten => nocid,n,Wait(1)
exten => nocid,n,Hangup()

;If found in DB, rewrite CID name on the fly
exten => cid,1,AGI(check_cid.phpcli|${CALLERID(num)}|${CALLERID(name)})
exten => cid,n,Goto(main_menu,s,1)

[main_menu]

;HERE : Check for channel status: If on-hook, go off-hook

exten => s,n,Answer()
exten => s,n,Background(main_menu)
exten => s,n,WaitExten(5)
exten => s,n,Hangup()

Thank you.

Apparently, it’s OK to call Answer() even if the channel is already open:

voip-info.org/wiki/view/Asterisk+cmd+Answer

So I guess I can simplify things this way:

[my-ivr]
HELLO=false

exten => s,1,GotoIf($[${LEN(${CALLERID(num)})} = 0]?nocid,1:cid,1)
      
exten => nocid,1,Set(HELLO=true);
exten => nocid,n,Answer()
exten => nocid,n,Playback(my_sound_files/hello)
exten => nocid,n,Read(CALLERID(num),my_sound_files/no_cid,10)
exten => nocid,n,GotoIf($[${LEN(${CALLERID(num)})} < 10]?cid,1)
exten => nocid,n,Hangup()

;If number in DB, rewrite CID name on the fly
exten => cid,1,AGI(check_cid.phpcli|${CALLERID(num)}|${CALLERID(name)})
exten => cid,n,Goto(main_menu,s,1)

[main_menu]
;OK to call Answer() even if line already off-hook
exten => s,1,Answer()
exten => s,n,ExecIf($[${HELLO} = true],Playback,my_sound_files/hello)           
exten => s,n,Background(my_sound_files/main_menu)
exten => s,n,WaitExten(5)
exten => s,n,Hangup()

Thank you.