Asterisk® is our Friend

I suppose this is slightly off the usual topics for this blog, but what the heck.

Asterisk logo Asterisk® software is an open-source system for computer telephone stuff. Yes, I did just say “telephone stuff” instead of “PBX, VoIP, and Telephony”. Cope. Anyway, it’s an entirely legally-free (aside from the cost of a computer and any desired additional equipment) replacement for the kinds of many-thousands-of-dollars proprietary software systems that your cable TV and telephone companies use to prevent you from talking to human beings on the phone (so they can fire most of them, and outsource the rest to India or the Philipines or Florida or China or whatever other “developing” area has cheap low-grade labor). In other words, they seem to use their telephony system mainly for telephony prevention. The fact that “The Man™” uses the power of a PBX for evil in this way shouldn’t trick you into thinking that having your own is a bad thing, though. For example, my Minister of Domestic Affairs was recently in Australia for work. Since calls back to the US on the cellphone cost $1.50/minute, I set up a voice-over-IP client on her computer before she left. She could then use her computer’s internet connection to connect to the Asterisk box at no extra cost. The Asterisk box could then forward her Voice-Over-IP call out our residential phone line to my cellphone – a local call for the Asterisk box. No $1.50/minute for “The Man™”! Take that, The Man!

(Oh, “PBX”? That’s Private Branch eXchange. It’s a fancy way of saying it’s your own personal robotic Ernestine the Operator for your house or office.)

I discovered Asterisk a few years ago and have been puttering with it off and on. I figured if I wanted to learn how to use it, it’d be a good, simple start to replace my answering machine with it. It was a little trickier than I thought. I got my hands on a working “X100P“-type card, which is really just a specific variety of cheap voice-modem that was used originally for early development of Asterisk prior to the fancier hardware being developed. This connects my Asterisk box to the phone line. Like my old answering machine, it shares the line with an ordinary telephone that it doesn’t control.

Googling turned up all kinds of information on getting Asterisk to answer the phone and do all kinds of amazing tricks, but not a lot about controlling the answering in the first place. I wanted it to act like my old answering machine. It wasn’t answering the phone and taking messages that was hard to figure out, it was getting it to not answer the phone if someone in the house beat Asterisk to it.

I couldn’t find any references to this anywhere online at the time (and still can’t, actually, though they may be out there). Asterisk doesn’t seem to have any way – at least not with the X100P – to explicitly detect when another device picks up the shared line, but I came up with a workaround.

Now, when the lines starts ringing, I have Asterisk wait 11 seconds (which works out to about 3 rings) before doing anything. Then, I have it explicitly check for one more second to see if the line is still ringing. If not, the assumption is that someone picked up the phone and Asterisk leaves it alone. If it DOES detect one more ring, it picks and and carries on with whatever incoming-call magic I feel like programming into it – like detecting and saving incoming faxes. A copy of the relevant portion of my dialplan for any other Asterisk users out there who care may be found at the end of the post.

Once the house-hunting frenzy I’m in the middle of dies down, I’d like to start adding some nerdier features. For example, we’re moving to Southeast Texas, where there are occasional tornado warnings. Apparently, the National Weather Service’s warnings online contain embedded geographic information defining the boundaries of the warning area. I could have Asterisk watch the warnings page, and call my cellphone to tell me if I have to worry about tornadoes or not. (Kind of silly, I know…). It’d also be nice to finally test the fax reception that hypothetically is set up to work on my Asterisk box, too. (Dang crippled Motorola cellphones won’t let me fax despite supposedly supporting Class 1 fax mode, among other missing features…But that’s another post for another time.)

And now, the dialplan (or fragment thereof) (Update 20080523, fixed missing “]” after “[incoming”):

[incoming]
;give time to allow for someone to pick up 'regular' phone before asterisk does
exten => s,1,Wait(11)
;pause to check for one last ring, just in case someone picks up at the last second
exten => s,n,WaitForRing(1)
;So, you get 11 seconds - about 3 rings - to answer the phone.
;after that, Asterisk waits one more second for another ring.
;obviously if someone has picked up the phone before then,
;that last ring will never come and Asterisk will leave the call alone.
;otherwise, answer the phone:
exten => s,n,Answer()
;supposedly this will correctly jump to the fax extension if it's an incoming fax

;give announcement that ain't nobody here..
;(after waiting 3 seconds in case of fax tone detection)
exten => s,n,Wait(3)
exten => s,n,Background(nobody-but-chickens)
;...then go to 'leave a message' like a normal (if extremely powerful) answering machine
exten => s,n,Voicemail(9000)
exten => s,99,Hangup()
;end of line for now

Any questions?…

Published by

Epicanis

The Author is (currently) an autodidactic student of Industrial and Environmental microbiology, who is sick of people assuming all microbiology should be medical in nature, and who would really like to be allowed to go to graduate school one of these days now that he's finished his BS in Microbiology (with a bonus AS in Chemistry). He also enjoys exploring the Big Room (the one with the really high blue ceiling and big light that tracks from one side to the other every day) and looking at its contents from unusual mental angles.

32 thoughts on “Asterisk® is our Friend”

  1. *technolust*

    Having an asterisk system is awesome. I’ve been wanting one for quite some time, but without a landline, there isn’t much point.

    Now, you could integrate a text-to-speech engine, and get some really interesting things going on, although, if this is running on linux, that might be difficult. I don’t know what text-to-speech engines are available on that platform for free.

  2. “although, if this is running on linux, that might be difficult.”
    Why does everybody think doing things with Linux is difficult?…

    Actually, when I mentioned the possibility of having the computer call me to tell me about tornado warnings, I did mean that it would literally call and TELL me about the warning.

    Text-to-speech on Linux is easy. For simple uses in English, there’s espeak. For simple as well as more complex uses (and multiple languages), there’s Festival, which is what the Asterisk documentation talks about using.

    Calling your Asterisk box and having it read you your email or the weather forecast is a commonly-mentioned “neat trick”. Personally, I think it’d be neat to devise some fermentation monitoring equipment and hook it to the Asterisk box. Then I could call in and get current temperature, rate of gas evolution, pH, specific gravity, etc. for whatever my current fermentation project happens to be (or have it call ME if one of the parameters goes too far out of where it should be…).

  3. Oh, almost forgot:
    “without a landline, there isn’t much point.”
    Asterisk is great for Voice-over-IP, too. There are actually VoIP providers that you can connect Asterisk to, who will forward your calls to the Plain Old Telephone System for you as appropriate, though of course they charge for the service. (There MAY actually be some services that do some VoIP-POTS bridging free of charge, but I don’t currently know of any).

    Come to think of it, I would swear I’d seen reference to hooking up Asterisk to a cellular phone system, too. If so, you could just add a second line to your current plan and use it for the Asterisk box…

  4. Thank you for explaining your use of the WaitForRing app. It is ridiculous that more people don’t know about this or talk about it online anywhere. I guess I could have eventually figured it out by reading through countless pages of documentation, but the purpose of this feature does not seem to be clearly outlined anywhere that I have looked, and it would have taken many more hours out of my life were it not for this page’s existence.. Your article has saved me lots of time and trouble, and made it possible for me to use asterisk as an answering service while continuing to use regular phones in parallel.

  5. Thanks, glad to be of some use – especially since I was having the same problem you were as far as finding documentation for this kind of application.

    The next thing I want to do is work out how to set up a blocklist analogous to the ones used by email spam filters, to automatically recognize phone-spammers and either immediately hang up on them or shunt them to a twisty, hellish maze of voicemail.

  6. I was thinking of doing something like that as well, but all I can figure out for that is routing based on caller-id. There’s probably a much more high-tech way that I’m not aware of though. Good luck with that, and I’d be interested to know what you come up with. I read up a bit on asterisk a few years ago, and even bought a clone x100p a while back, but I hadn’t become nearly practiced enough in linux to compile and install a kernel module (zaptel) until very recently. Now that I’m a bit smarter with the linux, I’m having fun exploring all the possibilities of asterisk. You wouldn’t happen to know which audio file is the one in the voicemail app that plays right before you start recording a message, would you? (the automated voice that talks right before the beep…) I really want to change that, and I can’t find which file it is or even any reference to the audio files used in the voicemail app. Anyway, you don’t have to help anymore since you’re not a support mailing list, so I’ll stop picking your brain now.

  7. Off the top of my head I don’t, but I can find out when I get home.

    I don’t mind the questions – it’s not like I’m buried under comments on my obscure little blog here, and in any case I end up learning a lot when I try to answer them.

    You’re on Debian? I’d be surprised if there wasn’t a repository SOMEWHERE with pre-built zaptel(or “dahdi”, depending on how recent your Asterisk version is) package for Debian.

    The add-on I REALLY want to figure out how to work at the moment is chan_mobile. Bluetooth audio seems to be a major pain to get to work, though.

  8. I’m using asterisk 1.4, so I’m pretty sure I’m stuck with zaptel for now. In the default debian repository the zaptel package only installs ztcfg and zttool for configuring zaptel once it’s installed. There is also a package for the zaptel-source which, when installed, needs to be compiled with the module assistant. Although it mentions the correct command to do that right in the package description, when I was a newb, I just saw the letters m-a a-i zaptel at the bottom of the description and didn’t know that I was supposed to type that. The wording wasn’t very clear and it didn’t look like a command to me. However, as with most things on linux, after a small amount of searching online it was quite obvious how to make it work. I chose 1.4 because I didn’t want to begin my astriscapades with an unstable version. Now that I know a bit about it, I might upgrade to 1.6 if I find any advantage to using that version for my particular setup.
    I hadn’t heard of chan_mobile until you mentioned it, but holy crap that sounds awesome. I would need a bluetooth transmitter for my computer first which I should get for so many reasons anyway, and I don’t know if my cellphone fw would allow me to use this properly but that would make auto texting and sip to gsm so much easier. Right now, the only texting my asterisk can do is through an email to sms gateway. I can’t even imagine what else that driver could do, but it’s exciting to think about. Great, now I want to go out and spend money. Gee, thanks.

  9. Oh, found the sound that it plays for the voicemail, by the way. It’s “vm-intro” (on my system, “/var/lib/asterisk/sounds/en/vm-intro.gsm”

    Actually, as far as I can tell Dahdi IS Zaptel – it’s just that they had to change the name due to some trademark complaint. I’m not absolutely certain but I think the current version of the 1.4 series also recognizes dahdi (I know 1.6 does, obviously…) as well as “zaptel”.

    Chan_mobile only works over bluetooth because that is apparently the only standard way to get both modem commands AND audio to and from the phone. I actually got it to work, except for audio (that is, I had Asterisk seeing my cellphone ring and answering it, but no sound came in or went out. I haven’t gotten bluetooth audio in any form to work on any computer yet, though admittedly I’ve only tried once or twice.) SMS (“texting”) is supposed to work both in and out, too, though since I don’t want to fork out $0.20 for every test I haven’t tried it myself yet.

  10. Thanks for the info! On my system the path for the sounds is /usr/share/asterisk/sounds. I’m sure it’s in there and I’m going to change it in a minute. I think I’m going to shell out for a bluetooth usb dongle or card and give 1.6 and chan_mobile a try. I have unlimited texting so I don’t have to worry about the charges. Bluetooth is completely new to me as I’ve never had a computer that supported it, but maybe you need a card that has fully operational drivers on linux for the audio to work, or one that is reported to work properly with the bluez stack. Again, I don’t really know anything about this, just offering some ideas that you’ve probably already looked into. Maybe it’s just that 1.6 and chan_mobile have bugs, and there might be documented workarounds. Good luck with that and I’ll let you know if I figure it out. It will be at least a couple of weeks before I get the bluetooth card though. Thanks again.

  11. That was totally the right file by the way. Thanks a lot, I couldn’t find the right one for the life of me. I tried changing just about every other vm-*.gsm file other than that one, since I don’t know how to listen to gsm files except through asterisk. For blacklisting phone spam, what I’m going to do for now is create caller-id pattern matching rules for all the area codes I expect to receive legitimate calls from, to make them go through as normal. Then I’m going to make a rule for everything else to go to a menu that picks up instantly and says “Hi, your call is important to us, please stay on the line to maintain your call priority.” Then it’s going to put them on hold for 2 minutes after which it is going to say “sorry, I lied. your call is not important to us at all. goodbye.” click. Hopefully it won’t catch any important calls. If you have a better idea about the screening aspect, I’m all ears.

  12. I use the “play” command from sox to listen to gsm files – seems to work fine. (When I mount via sshfs I have to explicitly tell it they’re gsm files e.g. “play -t gsm vm-intro.gsm”)

    Area-code based screening might be a little coarse-grained (but it depends on what your phone gets used for, obviously) but I imagine it’s also a lot easier to implement.

    In my case, I’m thinking about having Asterisk do a “audio ‘Captcha'” thing for non-whitelisted/blacklisted numbers e.g. “If you are a human being, press the [random single digit key] key”, and then just hang up if the key isn’t pressed. Numbers that are known to belong to scammers and phonespammers Asterisk would do an Answer/play-“number-disconnected”-three-tones/hangup-immediately routine. Maybe after sending a “1” dtmf tone for all of the “press 1 to speak to us about this important call” phonespam bots, so as to waste their human flunkies’ time picking up to talk to the line that has already hung up on them…

    I like the idea of known human-using phonespammers being sent to an auto-annoy recording though. I’d also like to find (or start!) an online database of “callerid numbers used by known phonespammers”…

  13. The captcha idea is interesting. I wouldn’t use it personally for my home number as many people who call my home hate having to interact with things on the phone, but a great idea for certain places! Even if it’s only set to require one fixed number to be pressed, the spam bots would never be able to get past. The sending 1 tone for blacklisted numbers would be a funny addition. A global blacklist would be neat, although people’s numbers change and that could lead to problems. It would probably have to be set up with the help of the phone companies so they could remove numbers from it when the subscriber of a blacklisted number changes. I set up mine the way I mentioned, so right now I’m blocking private numbers, all area codes that I don’t explicitly set (I set to allow all area codes where I know people who might call me from), and then I’m just going to wait for some spammers to call and I guess blacklist their numbers one by one. It’s not the best way to do it I know, but it’s a start. Now I just have to warn certain individuals to not call me with their numbers blocked or my phone will be rude to them.

  14. I’m thinking I should put up a “Stupid Asterisk Tricks” post – there are some good ideas coming out of this comment thread…

  15. Indeed. Maybe you should also do a “Stupid misconceptions about Linux” post for mr. windows vista up there.

  16. Okay, maybe that was a bit rude, but honestly, if it exists on computers then there’s usually a more powerful and free version of it for linux. Sorry windows, but it’s just the effin way she goes.

  17. I used to be pretty worked up about it too, but these days I just think of a preference for proprietary platforms (Be they Microsoft or Apple) as being analogous to having an unsanitary fetish. Open-minded guy that I am, I can accept that, so long as I’m not the one that has to clean up after it.

    The only time I really bothers me is when it encroaches on my own freedoms, in the form of “we only allow Microsoft® products here” edicts, which (to extend the metaphor) is like walking into a building wearing perfectly functional and comfortable clothing, and having someone explain that you are violating the dress code and won’t be allowed in without your diaper, inflatable anatomically correct marsupial, and tub of lard, just like everyone else there.

    Thankfully, that’s an increasingly rare situations these days. Linux usually just does what it needs to do for me without any outside “support”, so even in places that “only support Windows™” I can generally connect and interoperate pretty seamlessly, for which I am grateful. Heck, I’ve even had Linux boxes authenticating from Microsoft® ActiveDirectory™ before. I refuse to go into further detail, though, as I believe that’s still considered an illegal Unnatural Act in some states and the statute of limitations probably isn’t up yet. I’m happy to say I’ve managed to put those days behind me.

    Plus, the diaper rash was killing me.

  18. Hehe. That makes me imagine a world where the majority of people are furries or something. Man, would that be a strange world. Sometimes I wonder if Microsoft pays people to only support their software, but then I remember how much most people just like flashy GUIs and to follow what “everyone else” is doing. But it’s not always a matter of preference so much as it is a matter of ignorance. In just about every other part of life, people try to find bargains, good deals, the best prices for things, etc. before they buy stuff. It just seems to me that people are more than happy to throw their money away purchasing software, because they don’t know that there’s any legal alternative. And the argument that linux is more complicated is sort of valid, in the sense that it is more customizable, but personally, I find a text file containing all the options and the explanation of the options right next to them to be far easier to understand than a big window full of buttons and tabs and check boxes with virtually no explanation of the options. Thats my preference. And I don’t have a lot of money, so free is certainly better than expensive.
    I did the opposite with the ActiveDirectory back when I was still using windows machines but was starting to learn linux. I wanted a domain controller for my windows computers, so I set up a samba domain controller with openldap. I don’t use that anymore though because I have ditched windows, except for a few small tasks that won’t work in wine, like Adobe CS4 and this little program that backs up my ps2 games and lets me play them off an external hard drive.
    On a final note, if people like flashy GUIs and also don’t like paying for software, there’s always ubuntu.
    Okay, I’m done ranting now.

  19. I just made a php call file generator… I think that would be a good and simple “stupid asterisk trick”. The html/php form generates a call file, and then a cron job chowns it to asterisk and then moves it to the outgoing spool dir. The cron job runs once every minute. On my setup, it’s only useful for sending automated pre-recorded calls, but with a SIP trunk or a second pstn line it could potentially call your cell phone (to utilize free incoming calls) and then allow you to make a call through your other pstn line or the SIP line. With a good voip plan, you could make free long distance calls through your cell phone. I think that functionality is quite neat and quite stupid.

  20. Do you have a write-up on it? Something like that could also be combined with something like Yahoo’s “FireEagle” or my homebrewed “where was I?” asterisk application project to, for example, periodically check for tornado warnings or other severe weather situations near my current location, and have it use Festival to generate a call to let me know about it. (Now I’m chuckling, imagining my phone ringing and the emotionless voice of Festival saying “There is a severe blizzard heading for your area. Everybody panic. Run. Run for your lives.”)

  21. Okay, the blog is processing the html, sorry about that. Please delete the post and I will try again. I had a funny feeling that this was going to happen.

  22. Hehe, your bad behavior filter blocked my repost. I guess I have to wait until you remove the previous post before I can repost. I used escape characters this time so it should display correctly.

  23. Yeah, blogs that disallow HTML in comments annoy me, so as long as I don’t end up with any severe abuse problems – and I doubt I will – I allow them here.

    I deleted your previous post – I’ll also see if I can manually clear the “Bad Behavior” cache.

  24. Thanks, I’ll try a repost in, let’s say… 20 minutes. I hope that give you enough time to give bad behavior a lesson in good behavior.

  25. What’s really strange is that the Bad Behavior log isn’t even showing your last couple of posts – neither this successful one or the last few ones that it apparently blocked. Very odd. I’ve temporarily just disabled Bad Behavior, so you ought to be able to post now…

  26. This really is quite strange. Bad Behavior is off, but when I try to post the tutorial, the page reloads like normal, except the post doesn’t appear on the page at all. Maybe it’s being discarded because the post is too long? I just tried posting it twice and it doesn’t like me. There must be something in the logs that could help you fix the problem… I’m going to try once again, but break it up into a couple of separate comments instead of one really long comment. Lets see if this works. *fingers crossed*

  27. It’s not because the post is too long. I just tried posting only the first 3rd of the tutorial, and even that disappeared. I think the escape character for < breaks the comment feature. Let me know if you can fix it, or if I should just put it up on my site and send you a link. The only reason I’ve been reluctant to do that is my site will be experiencing some down time very soon while I move it to a new server, and I’m giving the main page a big overhaul fairly soon. But some problems just aren’t practical to fix, so if it’s too much work, I’ll happily put it up and provide a link for you.

  28. It SHOULDN’T be a problem. Unfortunately, there don’t appear to be much in the way of logs for wordpress. What log I do have is a plugin, and it only shows that you sent POST queries (and does not indicate an error). Kind of useless…Grrr….

    It’s not a big deal if you want to post on your own site and link to it. If you’d rather, you can also email to me (epicanis+wp at bigroom.org) and I’ll see if I can figure out what the heck wordpress is choking on…

  29. Here’s the tutorial: How to create a PHP/HTML Asterisk call file generator
    Feel free to take its contents and post it on your blog if you want. Just please give credit to me (Defcronyke Webmaster).
    Let me know if any part is unclear or hard to follow and I’ll fix it up or help you out. I doubt you’ll have any problem doing it though. Enjoy.

  30. Did anyone made an audio captcha support for Asterisk?

    The idea was described by Epicanis in this thread – did you succeed?

  31. I don’t know if anyone’s implemented it yet, but the idea was just to have a relatively simple text-to-speech script for Asterisk that generates something like “If you are human, press [some randomly-chosen number between 0 and 9] now.”, and then passes the call through if the correct number is pressed, or either hangs up or sends the call to an auto-annoy script if the wrong one is pressed or no key is pressed within a second or two.

    It’s been a while since I played with Asterisk at this point – I took down the dedicated tower server that was running it so I can no longer use the inexpensive PCI modem that I was using for it. I bought a Sangoma U100 USB FXO, but I’ve been having ongoing trouble with the fact that it’s only “supported” on ancient fossilized “enterprise” distributions (e.g. RHEL or CentOS) on old kernels, and dahdi support may not be as good as the support for the FreeSwitch driver in any case (personal impression, I don’t know if that’s actually true. The one time I got this device working on Asterisk it would answer but then crash upon trying to send audio to the phone through it.)

    At the moment I’m thinking of trying out FreeSwitch, which apparently already has support for the awesome opus audio codec available (Asterisk still doesn’t as far as I know, though I just saw some third-party patch was released just in the last few days) and whose interface for the U100 MAY be better than the Dahdi one. Maybe.

    If I ever manage to get time to try out my own “audio captcha” implementation, though, I’ll remember to post it on the blog here!

Leave a Reply