Asterisk + Twitter = Call Monitor

Why? Why not. I was wondering how else I could make use of Twitter for other things I thought “How could I use twitter with my Asterisk PABX?” Doing some Google searching showed I wasn’t the first to think of this (as if that would have happened). There are a few people doing various things with asterisk and twitter.

I didn’t really like much of what I found, or found it wasn’t really what I wanted to do with it .. so I decided to use some of that info and came up with something of my own.

The short story is this. When someone calls my Canadian number, my US Toll-Free number any number that ultimately rings the group of phones in the house, I will now get a direct message in Twitter from a special account I setup for this purpose, which also sends a SMS to my phone (device notifications in twitter).

Read more by clicking below… Be warned! Thar be scripts and technical stuff in them thar parts


Ok, so lets see what’s under the bonnet of this beast now.

First, you’ll need an AGI script. Look, here’s one I prepared earlier – twitter.agi

#!/bin/bash
# Background the curl process incase twitter doesn't respond. It will hang the dialplan.
curl -u username:PASSWORD -d text="$1" \
-d user="recipient" \
http://twitter.com/direct_messages/new.xml &

Put the script in /var/lib/asterisk/agi-bin. Don’t forget to chmod +x the script as well.

Pretty simple eh? The only things you’ll have to change in the script are

  • username = your twitter login name
  • PASSWORD = your twitter password
  • recipient = Who will get your direct message

Ok, and to make it work in Asterisk I put this in the extensions.conf file for the exten that I wanted to monitor:

[home_phone]
exten => s,1,NoOp("This is my home phone context")
exten => s,n,Set(_DIDNUM="PUT YOUR DID NUM HERE")
exten => s,n,LookupCIDName
exten => s,n,AGI(twitter.agi|PBX: ${CALLERID(all)} just called on ${EXTEN})

I’ve only shown the first bits of this context, after this is your normal context such as DIAL or whatever else you might now. I actually build in a few seconds of delay to allow the text message a chance to get to my phone before the phone rings. A sort of early warning system 🙂

The only thing here you need to change really is the value of _DIDNUM (notice the underscore in front of the var name, this isn’t strictly needed, but if you wish to use that variable else where in the context or exten it’s a good idea. Obviously you can change the message after the “pipe” in the AGI line to customize the message you are sent.

The one thing I have left to work out is what asterisk variable to use to actually get the DID number used to call into the PBX with. If I can get that that would make this so much easier and more flexible. If you know what that is please drop me a comment.

Well, that’s really about it. It’s not rocket science by any means, heck, it’s nothing at all but it works.

Share this post :


9 Responses to Asterisk + Twitter = Call Monitor

  1. @rob – try callerid(DNID) – that lets you know the number that was called

  2. Great work !
    One very quick solution for determining where the call come from is to use different context for each line,set a variable and then jump to a common context to do the agi stuff

  3. David,

    Thanks, that bit I’ve got. What I’m actually looking for is a way to say “You have received a call on xxx-xxxx”. I have multiple numbers in different countries that terminate on asterisk. I’d like to have the message sent to twitter say something like:

    “A call from [Callerid(num)] on [the number dialed by callerid(num)]”

    I currently just use EXTEN, but the internal exten used doesn’t help since I have 3 or 4 numbers that end at that exten. It’s knowing which inbound number was actually used that I need to figure out.

  4. The callerid(num) will return the caller’s number.

  5. @Rob: I would think if the AGI hasn’t exited that the next step in the dialplan wouldn’t be executed. Shoving it in the background is probably not bad, but you might want to submit it to batch so that it’s handed off to something else entirely.

  6. Nice stuff! I did the same type of thing using [URL removed by moderator] with missed calls, so I could just go to a protected web site and see if I messed calls from people who didn’t leave a message. But in the end, I could do that using the local Apache server.

  7. Well, that was fast. Digium, the folks that produce Asterisk have already found this posting and have sent out a tweet about it on Twitter..

  8. Hmm good point.. to be honest I have no idea what would happen.. I can test that tonight around midnight when the entire US is tweeting and it nearly always breaks twitter.

    My first thought is, no. There shouldn’t be any delays. But, that’s being said without a full understanding of how AGI’s really work. To make sure it doesn’t happen though, I’ve added a “&” to the end of the curl line so it’s shoved in the background and the script can exit quickly and return control to asterisk and the dial plan. I don’t really care if the sending is successful or not, it’s just a “nice to have” feature and not as such, if the message fails, it fails.

  9. Interesting use of Twitter. I was looking at the Twitter website last night wondering what the big difference is between it and Facebook’s statuses.

    What happens if Twitter is slow in responding or doesn’t respond to twitter.agi? Is there a possibility that it could take tens of seconds to complete the AGI before it reaches the Dial() step?