Time-Zone Processing with Asterisk, Part I

 in
Use Asterisk to adjust for your daylight hours.
Step 3: Letting Callers Know the Time

At this point, Asterisk has all the data it needs to restrict calls based on the time of day. The Asterisk dial plan can be used to check the time at the remote site; if it is before, say, 8 am or after 10 pm, the phone plays the remote time to the caller and asks whether the extension should ring anyway.

First, Asterisk needs to pick up the phone and compare the current local time to the 8 am to 10 pm window. Asterisk's STRFTIME function converts an epoch into a time of day. By adjusting the current epoch time with the offset value, the STRFTIME function returns a time of day. A call that is too early or too late jumps to code that silences the ringer. When a call's ring is silenced, this example code allows the caller an override:


      
300 => {
     Answer;
     Playback(msg/remote-extension-greeting);
     Set(TZ_OFFSET=${DB(tz/${EXTEN}-TIMESKEW)});
     Set(RMT_EPOCH=$[${EPOCH}+${TZ_OFFSET}*60*60]);
     Set(REMOTE_CLOCK=${STRFTIME(${RMT_EEPOCH},
          Europe/London,%H:%M)});

     if("${REMOTE_CLOCK}" < "08:00") {
          goto 300|too-early;
     };
     if("${REMOTE_CLOCK}" >= "22:00") {
          goto 300|too-late;
     };

     goto 300|normal-ring;

The confirmation code starts by silencing the ring, but allows the user to enable ringing by pressing one. I've allowed any caller the flexibility to override my hours, but this conceivably could be handled by allowing only select callers to be able to override. The “standard” way to set the ring tone is to set the ALERT_INFO channel variable. My ATA is a Sipura, which allows the definition of eight ring cadences. I have defined a cadence called silence, which is a ring that never uses the bell:

too-early:
    NoOp(Too early to ring);
too-late:
    NoOp(Too late to ring);
    Set(__ALERT_INFO=silence);
override-silence:
    Playback(msg/my-remote-time-is);
    SayUnixTime(${RMT_EPOCH},Europe/London,
         A \'digits/at\' IMp);
    Read(CONFIRM,msg/press-1-to-confirm-call,1);
    if (${CONFIRM}=1) {
         Set(__ALERT_INFO=Bellcore-r1);
    };

Ringing the phone is straightforward, because it requires using only the Dial application, and the ring cadence has been set elsewhere. Alternatively, the call can be sent straight to voice mail:

normal-ring:
    Dial(SIP/300,20);
vm-only:
    VoiceMail(umatthew);
Hangup;
};

For enhanced code re-use, the time-of-day checks could be incorporated into a macro that is called as part of every extension.

Using This Project

As a first step, install the GeoLiteCity database from MaxMind, and install the time-zone lookup script into /var/lib/asterisk/agi-bin. To call that script, add the time-zone configuration extension *89 to your dial plan. Every extension that requires time-of-day-specific treatment needs to have its dial plan modified with code similar to that shown in Step 3 of this article.

Then, as a user of the PBX, every time a SIP extension is registered, you need to call *89 to set up the time zone. This need to initiate the configuration process manually is somewhat annoying. Asterisk does provide an interface that can be used to set up the call to the user automatically, which I will describe in a follow-up article.

On a PBX that supports multiple users, several items would benefit from centralized storage. In this example, the time-of-day comparison is coded into the target extension's dial plan. By storing this data in the Asterisk database, it would be possible to let users change their time-of-day schedules without administrator intervention.

Finally, I also have configured my PBX with a “friends and family” override feature that allows selected callers to complete a call even if it normally would be blocked. Callers on the privileged list can request the time of day at my location and are allowed to ring a call through even if it would ordinarily be blocked.

Matthew Gast is the author of the leading technical book on wireless LANs, 802.11 Wireless Networks: The Definitive Guide (O'Reilly Media). He can be reached at matthew.gast@gmail.com, but only when he is close to sea level.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

for accurate local time...

Anonymous's picture

I'd recommend interfacing to the World Time Engine API.

You can check the timezone info that you're likely to get on the main website @ http://worldtimeengine.com.

It works a treat for one of our IVR services we've got running.

excellent article

directory submission's picture

Calling people across time zones might prove to be inconveniencing to the receiving party, especially if it’s late at night or very early in the morning. This is an excellent article on how to make Asterisk redirect inbound callers to a message informing them that they are calling at inconvenient hours.

Very well written

SoftArea51's picture

I find this article to be very useful and I think more articles like this can help us go deeper into Asterisk.

i think so too

schnäpel's picture

my thoughts ... a little bit more informations and i will be an "asterix-magican" :-) ok i´m far away from that but i´m willing to learn and linuxjournal is allways a good resource.

i agree

Oman's picture

i agree with you schnäpel - linuxjournal is for me the ultimative ressource i only find here the stuff if searched for. excellent website i love it.

linüx

evden eve nakliyat's picture

I hope linüx come number one

Asterisk

rbytes reviews's picture

evden, i hope too :)
To Bruce Byfield: good article, thanks!

linux is good but ..

online gewinnen's picture

i hope not! there far to less software development to be number one.

Yes, Great article, I agree

softsea's picture

Yes, Great article, I agree with this feature of asterisk seem cool, but I don't think it is very necessary.

A simpler suggestion

Tanner Lovelace's picture

Very cool use of Asterisk, but I might suggest a much simpler way of dealing with the problem:

Turn your cell phone off when you don't want people to call you on it. I recently went to Spain and took my GSM cell phone with me, but when I wasn't using it, I turned it off. That way no random calls waking me up in the middle of the night! Sure, it's not as cool as setting up Asterisk to tell callers you're in a different timezone, but for most people it makes a lot more sense.

not while traveling

Anonymous's picture

I want my family to be able to contact me *anytime* while I'm not at home or work. I'll tolerate the occasional wrong number, or mis-calculated time zone difference, rather than cut myself off from my loved ones.

or 2 numbers

Lawrence D&#039;Oliveiro's picture

Another answer to that is to have 2 numbers, an unlisted one for family and other close people, and a listed one for everybody else. Have them both go through your Asterisk server, and through to your real phone. Disable accepting calls from the listed one when it's not convenient.

You are absolutely right !

Wiersze's picture

I agree with you that two numbers it's very good solutions, btw are there any cell phones with cover two sim cards at time?

Greg

Time-Zone Processing

Mike Walker's picture

Whether or not you agree with the method used to solve the original problem this solution does a great job of exploring what can be achieved by exploiting the AGI. Hopefully other readers will create equally alternative uses engaging the AGI. After all the purpose here is surely to expand the general awareness of what Asterisk can be made to do. Let's not knock someone down just because we don't agree with their reasoning for a given implementation.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix