Running a python script when call is answered

I want to make a system in which when I call from one extension to a particular extension, a python script should start running when the call is answered.I have done following changes to my extensions.conf file .But when I dial extension number 1 through asterisk CLI,following is displayed but the python script doesnot run.

[from-internal]
exten => 1,1,Answer()
same => n,system(python3 /home/mariam/Desktop/audio.py)
same => n,Hangup()

You probably should use the ‘AGI’ command instead of a ‘System’ call.

You don’t have an adequate logging verbosity level to see what is happening, an you also seem to have another call in progress at the same time. The recommended verbosity level i 5. Your current log is consistent with both a successful and an unsuccessful execution of System.

I would suggest trying an absolute path name for python3, as the PATH environment may be limited.

I can’t see anything that would make AGI simpler or more likely to work.

Note that extensions don’t make calls in asterisk terminology; devices do that.

Also please provide logs as text files, taken directly from the system. Images cannot be searched and checked for incorrect characters, etc. Useful logs also tend to be too long for an image. All the terminal emulators I have used allow you to copy and paste from the screen, but, in any case, you need to use the actual log files when there is a substantial amount of logging, or the exact time relationships are important.

I’m not sure why answering the call is relevant as the timing of that is completely under your control and the script has no access to your media. Normally “when the call is answered” refers to the remote, called, party answering.

So what dialplan commands should I use?

In my project,I want to call from one softphone to another.On answering the call the python script should run.

You could do that using the U parameter on the Dial to the destination device. See the example headed “Dial with post-answer subroutine executed on outbound channel” in Asterisk 15 Application_Dial - Asterisk Project - Asterisk Project Wiki. Obviously you would need to change Playback.

That doesn’t explain why your python script is failing, but even the limited logging you have provided implies that System has been executed.

log shows the same statements even if I use playback(hello) and nothing happens

That’s because you don’t have adequate verbosity. The fact that it is logging something for priority 3, the Hangup, means it must have executed priority 2, even though you don’t have enough logging to output the line saying what it is executing for that priority.

Also, no-one i likely to have touched the sound card drivers in decades. You should really try this with a phone, if you want to hear the playback.

Can you please tell how to fix the issue for verbosity? I have tried with the phone aswell,nothing happens.

I get this after changing verbosity level from 2 to 3

console dial 1
– Executing [1@default:1] Playback(“Console/default”, “vm-goodbye”) in new stack
— <("<) — Call from Console has been Answered — (>")> —
– <Console/default> Playing ‘vm-goodbye.slin16’ (language ‘en’)
– Executing [1@default:2] NoOp(“Console/default”, “ERROR: FreePBX Does not use the [default] context, confguration error”) in new stack
– Executing [1@default:3] Macro(“Console/default”, “hangupcall”) in new stack
– Executing [s@macro-hangupcall:1] GotoIf(“Console/default”, “1?theend”) in new stack
– Goto (macro-hangupcall,s,3)
– Executing [s@macro-hangupcall:3] ExecIf(“Console/default”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [s@macro-hangupcall:4] NoOp(“Console/default”, " montior file= “) in new stack
– Executing [s@macro-hangupcall:5] GotoIf(“Console/default”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [s@macro-hangupcall:7] Hangup(“Console/default”, “”) in new stack
== Spawn extension (macro-hangupcall, s, 7) exited non-zero on ‘Console/default’ in macro ‘hangupcall’
== Spawn extension (default, 1, 3) exited non-zero on ‘Console/default’
– Executing [h@default:1] Playback(“Console/default”, “vm-goodbye”) in new stack
– <Console/default> Playing ‘vm-goodbye.slin16’ (language ‘en’)
– Executing [h@default:2] NoOp(“Console/default”, “ERROR: FreePBX Does not use the [default] context, confguration error”) in new stack
– Executing [h@default:3] Macro(“Console/default”, “hangupcall”) in new stack
– Executing [s@macro-hangupcall:1] GotoIf(“Console/default”, “1?theend”) in new stack
– Goto (macro-hangupcall,s,3)
– Executing [s@macro-hangupcall:3] ExecIf(“Console/default”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [s@macro-hangupcall:4] NoOp(“Console/default”, " montior file= “) in new stack
– Executing [s@macro-hangupcall:5] GotoIf(“Console/default”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [s@macro-hangupcall:7] Hangup(“Console/default”, “”) in new stack
== Spawn extension (macro-hangupcall, s, 7) exited non-zero on ‘Console/default’ in macro ‘hangupcall’
== Spawn extension (default, h, 3) exited non-zero on ‘Console/default’
— <(”<) — Hangup on Console — (>”)> —

We don’t support FreePBX here. It is actually executing FreePBX code here.

I missed that it was running the default context, when your dialplan was for the from_internal context. I’m not aware that the console default context can be changed.

Okay.Thankyou for your help.

The console driver is configured in console.conf.

The console context can be changed there.

The contents of the default context can be changed in extensions.conf.

But shouldn’t be, if you are using FreePBX, as the OP was.

Thankyou. Can you tell If i dont use freepbx, how can I use only asterisk to achieve the task?

If that is all you are doing, FreePBX is only getting in the way and not contributing anything useful.

what you are trying to do is not easy (for a beginner) but easy one you know the inner workings, you need to start by getting familiar with the multiple ways Asterisk interfaces with the world, it is a fun adventure, could take you couples months but I am sure you will end us with the software you have in mind and not the few dialplan lines you ended up with, so start reading couple Asterisk books, set up couple boxes and build from there, you cannot build the air conditioning until you’ve finish the building !