I have a question

Hi everyone!
I have a question,
I want to make an extension, that when you call it, it tells you the time remaining until a specified time and date.

How would I make such an extension in freepbx?

Thank you everyone in advance!

Asterisk is well aware of unixtime and after you do some date math you can use

To say it. So choose your language, add any libraries needed and write your ‘custom-context’

I don’t want it to read the current time and date, I want to make it read the time left until a specified date

As I said, do the date math needed to find the seconds difference and use that as your unixtime ‘days’ ‘hours’ seconds’ are constructed into the string you want use those in the call to the function which assemble the words grammatically

Ok, I got what you meant now, but I couldn’t figure out how to code it? (I don’t know really how to code) I tried to ask ChatGPT to help me, and I got a working custom context:

// Include the AGI class
// Create an AGI object
$agi = new AGI();
// Answer the call
// Get the current date and time
$now = date(‘Y-m-d H:i:s’);
// Set the deadline to 2023-08-20 06:30:00
$deadline = ‘2023-08-20 16:30:00’;
// Calculate the difference in seconds
$diff = strtotime($deadline) - strtotime($now);
// Convert the difference to days, hours, minutes, and seconds
$days = floor($diff / (24 * 60 * 60));
$hours = floor(($diff % (24 * 60 * 60)) / (60 * 60));
$minutes = floor(($diff % (60 * 60)) / 60);
$seconds = $diff % 60;
// Say the result using text2wav
$agi->text2wav(“The time left until the event is $days days, $hours hours, $minutes minutes, and $seconds seconds.”);
// Hang up

And it’s not really working as I expected

What were you expecting and what did you get?

You haven’t got a context. You have an AGI script. As an AGI script, I can’t see anything obviously wrong, other than that doing

and then:

is significantly over-engineered, given that


should do it directly.

I expected it to work, but it would just hang up and do nothing

As stated above, what you provided was not a custom context. You would need a custom context to call the AGI. Or you could call it in the GUI using a Dynamic Route.

Dynamic Routes User Guide - PBX GUI - Documentation (freepbx.org)

Using Dynamic Routes with an AGI file - FreePBX / Tips and Tricks - FreePBX Community Forums

9. The Asterisk Gateway Interface (AGI) - Asterisk: The Future of Telephony, 2nd Edition [Book] (oreilly.com)

You would need to make sure the user asterisk has permissions to run the AGI.

exten => _X.,1,Answer()
same => n,Wait(1)
same => n,Set(TARGET_DATE=2023-06-01 00:00:00)
same => n,Set(SECONDS_LEFT=${MATH(${EPOCH} - ${STRFTIME(${TARGET_DATE},,%s)})})
same => n,Set(DAYS_LEFT=${MATH(${SECONDS_LEFT} / 86400)})
same => n,GotoIf($[${DAYS_LEFT}>0]?has_days:has_hours)

same => n(has_days),Set(HOURS_LEFT=${MATH(${SECONDS_LEFT} % 86400 / 3600)})
same => n,GotoIf($[${HOURS_LEFT}>0]?has_hours:has_minutes)

same => n(has_hours),Set(MINUTES_LEFT=${MATH(${SECONDS_LEFT} % 3600 / 60)})
same => n,GotoIf($[${MINUTES_LEFT}>0]?has_minutes:play_end)

same => n(has_minutes),Playback(countdown) ; Change the sound file to your preferred countdown audio
same => n,SayNumber(${DAYS_LEFT})
same => n,GotoIf($[${DAYS_LEFT}>1]?plural_day:singular_day)
same => n(plural_day),Playback(days)
same => n,GotoIf($[${HOURS_LEFT}>0]?has_hours:play_end)

same => n(singular_day),Playback(day)
same => n,GotoIf($[${HOURS_LEFT}>0]?has_hours:play_end)

same => n(has_hours),SayNumber(${HOURS_LEFT})
same => n,GotoIf($[${HOURS_LEFT}>1]?plural_hour:singular_hour)
same => n(plural_hour),Playback(hours)
same => n,GotoIf($[${MINUTES_LEFT}>0]?has_minutes:play_end)

same => n(singular_hour),Playback(hour)
same => n,GotoIf($[${MINUTES_LEFT}>0]?has_minutes:play_end)

same => n(has_minutes),SayNumber(${MINUTES_LEFT})
same => n,GotoIf($[${MINUTES_LEFT}>1]?plural_minute:singular_minute)
same => n(plural_minute),Playback(minutes)

same => n(singular_minute),Playback(minute)

same => n(play_end),Hangup()


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