Recreating BT 1471 Call History

rikem

New Member
Oct 3, 2025
4
0
1
35
Hello,

EDIT: I have solved this on the reply below https://www.pbxforums.com/threads/recreating-bt-1471-call-history.8890/post-35728

I am trying to recreate the BT 1471 functionality where a user dials 1471 and the phone system responds with the date/time and telephone number of the last call, followed by "press 1 to return the call". This all works amazingly well, and my custom UK voices are working great but that's a post for another time :)

The problem is with the "play_and_get_digits" function. The sound file plays and I can see in fs_cli that the correct digit is collected. However I can't run actions based on the returned digit. I have attached my dialplan code and the area in question is line 33.

Could someone explain where I am going wrong please?

XML:
<extension name="last_caller" continue="false" uuid="382f0782-2950-43a2-8e5c-3e63e50e886d">
    <condition field="destination_number" expression="^1471$">
        <action application="answer" data=""/>
        <action application="sleep" data="1000"/>
       
        <!-- Retrieve last caller number and time from hash -->
        <action application="set" data="last_caller_number=${hash(select/${domain_name}-1471/last_number)}"/>
        <action application="set" data="last_caller_time=${hash(select/${domain_name}-1471/last_time)}"/>
        <action application="set" data="last_call_datetime=${strftime(%Y-%m-%d %H:%M:%S,${last_caller_time})}"/>
       
       
        <!-- If NO calls have ever been received -->
        <!--
        <condition field="${last_caller_number}" expression="^$">
            <action application="playback" data="voicemail/vm-no_calls_received.wav"/>
            <action application="hangup"/>
        </condition>
        -->
       
       
        <!-- Play date and number files -->
        <action application="playback" data="voicemail/vm-you_were_called.wav"/>
        <action application="say" data="en SHORT_DATE_TIME pronounced ${last_caller_time}"/>
        <action application="sleep" data="300"/>
        <action application="say" data="en TELEPHONE_NUMBER pronounced ${hash(select/${domain_name}-1471/last_number)}"/>
        <action application="sleep" data="300"/>
       
       
        <!-- Play and collect digit -->
        <action application="play_and_get_digits" data="1 1 3 5000 # voicemail/vm-to_return_the_call.wav '' return_digit"/>
       
        <!-- If they pressed 1, call back -->
        <condition field="${return_digit}" expression="^1$">
            <action application="log" data="INFO Callback condition triggered"/>
               <!-- <action application="set" data="last_call_number_int=${regex_replace(${last_call_number}, '^0', '+44')}"/> -->
            <action application="bridge" data="sofia/gateway/AQL/${last_call_number}"/>
        </condition>
       
       
        <!-- Play goodbye and hangup -->
        <action application="sleep" data="300"/>
        <action application="playback" data="voicemail/vm-goodbye.wav"/>
        <action application="hangup" data=""/>
    </condition>
</extension>


Thanks
 
Last edited:
I've not looked at Lua scripts yet, I may give this ago.

I understand now that conditions are evaluated only once so they do not register the change to the play_and_get_digits function, I need to transfer to another extension to process the digits.

Thanks.
 
I've recreated it in Lua and it is working perfectly. Posting here if anyone stumbles across this post.


You will need to add custom voices for:
  • voicemail/vm-no_call_history.wav - "Sorry, no call history"
  • voicemail/vm-called.wav - "called"
  • voicemail/vm-to_return_the_call.wav - "To return the call, press 1"

Set these two hash actions on your inbound call route:

XML:
<action application="hash" data="insert/${domain_name}-1471/last_number/${caller_id_number}"/>
<action application="hash" data="insert/${domain_name}-1471/last_time/${last_call_time}"/>


Add a new dialplan entry for 1471
XML:
<extension name="1471_lua" continue="false">
    <condition field="destination_number" expression="^1471$">
        
        <!-- Answer the call -->
        <action application="answer"/>
        <action application="sleep" data="500"/>

        <!-- Export last caller info from your hash -->
        <action application="export" data="last_caller_number=${hash(select/${domain_name}-1471/last_number)}"/>
        <action application="export" data="last_caller_time=${hash(select/${domain_name}-1471/last_time)}"/>

        <!-- Call the Lua script -->
        <action application="lua" data="custom/1471.lua"/>

    </condition>
</extension>


I added my Lua script to /usr/share/freeswitch/scripts/custom/1471.lua
There doesn't seem to be lua syntax highlighting on here?

Ruby:
-- 1471.lua

-- Use dialplan session
if not session:ready() then
    freeswitch.consoleLog("ERR", "Session is not ready!\n")
    return
end


-- Answer the call
session:answer()
session:sleep(500)

local last_number = session:getVariable("last_caller_number")
local last_time = session:getVariable("last_caller_time")

if not last_number or last_number == "" then
    session:streamFile("voicemail/vm-no_call_history.wav")
    session:hangup()
    return
end


-- Convert unix time to string
local readable_time = os.date("%Y-%m-%d %H:%M:%S", tonumber(last_time))


-- Play 1471 prompt
session:say(last_number, "en", "TELEPHONE_NUMBER", "pronounced")
session:streamFile("voicemail/vm-called.wav")
session:say(readable_time, "en", "SHORT_DATE_TIME", "pronounced")
session:sleep(300)


-- Get digit input
local digits = session:playAndGetDigits(1, 1, 3, 5000, "", "voicemail/vm-to_return_the_call.wav", "", "\\d+")

freeswitch.consoleLog("INFO", "1471 CALLBACK: digits pressed: " .. tostring(digits) .. "\n")
freeswitch.consoleLog("INFO", "1471 CALLBACK: last_caller_number: " .. tostring(last_number) .. "\n")


-- If user pressed 1, bridge to the number
if digits == "1" then
    if string.match(last_number, "^0") then
            last_number = "44" .. string.sub(last_number, 2)
        end
    session:execute("bridge", "sofia/gateway/b4a35359-4185-4gfb-8c73-f6b2583857/" .. last_number)
end

-- Goodbye and hangup
session:streamFile("voicemail/vm-goodbye.wav")
session:hangup()


This probably isn't the best or most efficient way to do this but it is working great now!

Cheers
rikem