How can i hang up calls or disconnect call in agi python by command pass

I am using if in python and there is a case where if user dont have the number we will disconnect the call. Sadly I cant run any function inside if command for dont know what reason so . I need to pass a hang up and disconnect call. How will i do that.

i tried the command " EXEC Dial hangup()"
In the dial plan i have

inside agi i tried executing the command
cmd_record2 = “EXEC Dial hangup()”

my dial plan is
exten=>4114,1,Answer()
same=>n,AGI(/root/code_base/Communication/comm2.py, beng)
same=>n,hangup() <<<< this is what i want to execute through command or disconnect

You do a goto to a context/extension/ that has hangup :wink:

https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+AGICommand_hangup

Setting the return code non-zero when exiting, should result in dialplan execution terminating, which will result in a hangup, unless you don’t have auto fallthrough enabled.

It has been my experience that when Asterisk displays:

    -- <PJSIP/anonymous-00000000>AGI Script example-agi completed, returning 0

It is displaying the result of Asterisk trying to execute your AGI, not how you exit your AGI. For example, in C, exiting an AGI via:

  1. return(0);
  2. return(1);
  3. return(-1);

all display ‘completed, returning 0’ and none result in a hangup, regardless of the setting of autofallthrough.

As noted by PitzKey, this is not a valid command.

Your attempt implies you are not using an AGI library. This is the first mistake most ‘new to AGI’ users make. A library will save you a boatload of time.

The second mistake most users make is not understanding the AGI protocol. It is ‘you request Asterisk to do something’ (via STDOUT) and ‘Asterisk returns a response’ (via STDIN) that you must read. Any other I/O on STDIN/STDOUT (like a quick printf to see the value of a variable) will break the protocol.

My experience has been different than yours.

Tell me more…

I created 3 test executables:

  1. returns-zero:
int	main()
	{
	return(0);
	}
  1. returns-one:
int	main()
	{
	return(1);
	}
  1. returns-negative-one:
int	main()
	{
	return(-1);
	}

and on the console, Asterisk displays:

    -- <SIP/poly-77a1-00000000>AGI Script returns-zero completed, returning 0
    -- <SIP/poly-77a1-00000000>AGI Script returns-one completed, returning 0
    -- <SIP/poly-77a1-00000000>AGI Script returns-negative-one completed, returning 0

regardless of the setting of autofallthrough.

What are you doing differently? Do you get non-zero returns from my examples?

I was assuming that the return code from the script would be passed by to the PBX. It seems not.

It would appear that setting a no-zero code sets the variable AGISTATUS to FAILURE, and the actual value is discarded. One should probably ask whether the OP is checking AGISTATUS.

Thinking about it, getting an AGISTATUS failure, and checking in the dialplan is better, as you are not hiding a major side effect of the AGI execution.

Confirmed:

    -- <SIP/poly-77a1-00000002>AGI Script returns-zero completed, returning 0
    -- Executing [*@newline:7] Verbose("SIP/poly-77a1-00000002", "1,AGISTATUS = SUCCESS") in new stack
    -- <SIP/poly-77a1-00000002>AGI Script returns-one completed, returning 0
    -- Executing [*@newline:9] Verbose("SIP/poly-77a1-00000002", "1,AGISTATUS = FAILURE") in new stack
    -- <SIP/poly-77a1-00000002>AGI Script returns-negative-one completed, returning 0
    -- Executing [*@newline:11] Verbose("SIP/poly-77a1-00000002", "1,AGISTATUS = FAILURE") in new stack

I don’t understand what you mean here.

Terminating dialplan execution is a major flow control action which isn’t explicit in the dialplan.

Why are you getting zero for all three?
Maybe try Environment.ExitCode = -1 or similar for your compiler?

The examples above are returning exit status:

-pbx10::sedwards:~$ ./returns-negative-one 
-pbx10::sedwards:~$ echo $?
255

How are you getting a non-zero exit status returned?

If an AGI exits early from an error (eg the exit code is not cleared).

This discussion is about how an AGI setting the exit status is handled, not how an AGI aborting is handled.

No it’s not (or, same thing!)
You can set the exit status by simply forcing an error (maybe with a divide by zero).

The status code returned from the waitpid for the AGI process (i.e. the return value from the AGI script itself (modified by any signal causing termination)) isn’t passed back as the return code from app_agi. Instead, it is used to set an AGI failed status, which is, eventually written into the AGISTATUS channel variable.

The actual return code from app_agi is set based on whether an internal flag, indicating the controlling channel hung up, is set.