Using a command or bash script to transfer a call

Re: this thread which is sadly closed now, a Reddit user pointed me to a bash script which did the trick

Here’s the slightly modified script which works for PJSIP.

#!/bin/bash
context="from-internal" # a destination context
exten=22 # an operator's number
num=23 # a destination number
bridge=$(asterisk -rx 'core show channels concise' | grep PJSIP/$exten | cut -d '!' -f13 ) # gets a channel bridge-id, where its name is like SIP/xx-aaaaa
channel=$(asterisk -rx 'core show channels concise' | grep $bridge | grep -v PJSIP/$exten- | cut -d '!' -f1) # gets a connected channel, where its name is $
result=$(asterisk -rx "channel redirect $channel $context,$num,1") # transfers an extracted channel into the destination context
echo $result
exit 0
3 Likes

Does anyone know how this could be modified so that it works for calls which haven’t yet been answered? I feel like this must surely be doable but so far eludes me! Thanks all!

This question begs some help. If the call isn’t established, you need to answer it in order to “pull it in” so the PBX can deal with it. One option would be to set up something like a repeating announcement and answer the call there. You aren’t going to be able to do anything with an unanswered call, but you can answer it in a ton of other places.

Thanks Dave. Apparently this script worked for someone helping me on Reddit

#!/bin/bash
context="from-internal" 
exten=extennum
num=destnum 
channel=$(asterisk -rx "core show channels" | grep "Dial(PJSIP/$exten" | cut -d ' ' -f1) 
result=$(asterisk -rx "channel redirect $channel $context,$num,1")
echo $result
exit 0

but it doesn’t work for me. :frowning:

One hates to argue with results, but this script is a bit of a hack. The output from core show channels may be truncated depending on how many characters long your channels are, so your results will be inconsistent. The original script uses core show channels concise for this reason.

The Proper™ non-hack way to do this is to connect via AMI, query for the desired channel then send the redirect thru the AMI.

2 Likes

Sound advice Mr Lorne! And choice phrasing, too! :heart_eyes:

I worked out why the script isn’t working, I think…

root@raspbx:~# (asterisk -rx ‘core show channels concise’ | grep -v PJSIP/4002- | cut -d ‘!’ -f1)

PJSIP/4001-000000c2
SIP/1-pstn-0000007e
SIP/SIPGATE_UK-00000083
Local/FMPR-4002@from-internal-000000a4;2
Local/FMPR-4002@from-internal-000000a4;1
Local/FMGL-07511232121#@from-internal-000000a5;2
Local/FMGL-07511232121#@from-internal-000000a5;1

root@raspbx:~# (asterisk -rx “core show channels” | grep “Dial(PJSIP/4002” | cut -d ’ ’ -f1)

Local/FMPR-4002@from

root@raspbx:~# (asterisk -rx ‘core show channels concise’ | grep -v PJSIP/4002- | cut -d ‘!’ -f1)

PJSIP/4001-000000c2
SIP/1-pstn-0000007e
SIP/SIPGATE_UK-00000083
Local/FMPR-4002@from-internal-000000a4;2
Local/FMPR-4002@from-internal-000000a4;1

so (to me) it looks like when I execute the grep, the correct channel is extracted but is cut to Local/FMPR-4002@from instead of Local/FMGL-07511232121#@from-internal-000000a5;1 this doesn’t seem to happen with INTERNAL calls, which always seems to have the format

PJSIP/4001-000000c2

I’m using the AMI here… using a function node to try to pare back the extremely verbose AMI

19

Just working on how to drill down a bit more…

Figured it out! This script works for unanswered calls AND answered calls. It means I can tap a button on my wall tablet (which is next to the phone in my kitchen) and transfer calls if the caller ID is someone who really might be wanting to talk to my wife in the attic, or if, mid conversation, it turns out they need to talk to my wife in the attic. The mad woman in the attic.

SO

this script does BOTH all in one. Brilliant! :smiley: :smiley:

#!/bin/bash
context="from-internal" 
exten=4005 
num=4002 
channel=$(asterisk -rx 'core show channels concise'  | grep -v PJSIP/4005- | cut -d '!' -f1)
result=$(asterisk -rx "channel redirect $channel $context,$num,1") 
echo $result
exit 0
3 Likes

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.