Caller id with asterisk and Ajax

2006; Belltown Media; Volume: 2006; Issue: 151 Linguagem: Inglês

ISSN

1938-3827

Autores

Mike Diehl,

Tópico(s)

Software Engineering and Design Patterns

Resumo

Combine Asterisk and Ajax to display incoming and outgoing call information. I've been using an Asterisk server to handle all of our telephone service for about a year now. During this time, I've discovered many really neat things that can be done with Asterisk, VoIP and various other technologies. One of the more gimmicky things I've done is sent the caller-ID information from incoming calls to a Web page on my browser, in real time. To do this, I had to use Asterisk, Perl, CGI, HTML, CSS, SQL, XML and Asynchronous JavaScript, or Ajax. There are a lot of different pieces to bring together, but sometimes that's what makes a project interesting. Here's how it works in a nutshell. When someone calls us at the house, the Asterisk server waits for the caller-ID information to be sent. The server then puts this information, and a few other pieces of information, into a file in a subdirectory under /tmp. This is all done in the Asterisk dial plan. Then, I have a Web page open in my browser that runs a JavaScript program every second. This JavaScript program uses an XMLHttpRequest object to query the server for new caller-ID information. The CGI script on the server returns an XML file containing the caller information. The JavaScript program parses the returned XML and displays the content. I've created a Cascading Style Sheet (CSS) that makes the caller information look like a sticky note placed on the Web page. When the incoming call is complete, the Asterisk server creates a Call Detail Record, or CDR, which resides in an SQL database. Each time the JavaScript contacts the server, the CGI script looks for the CDR. If it exists, the program knows that the call is over and deletes the caller information file in /tmp. This has the effect of causing the sticky notes to disappear when the call is complete. As an added bonus, the program supports up to four concurrent calls and can be used to indicate outbound calls as well. It's kind of nice to be able to see who's on the phone, regardless of whether the person is the caller or callee, without having to interrupt the person on the phone to ask. When my boys get older, this may become an even more important feature. For this system to work, you must configure your Asterisk server to put CDRs in an SQL database. By default, Asterisk puts CDRs in a comma-delimited file. The problem is that the flat file CDRs don't contain the call's unique ID, which this system uses to detect when a call has completed. The CDRs that get put into the SQL database contain this field. This shouldn't be a steep requirement though. As I recall, configuring Asterisk to store CDRs in a Postgres database was fairly straightforward and well documented in the cdr_pgsql.conf file. You also could use a MySQL or ODBC database, if you like. The first, and easiest, part of this project is to modify the Asterisk dial plan to create the flat file when an incoming or outgoing call is made. Once you determine where to make the change, it's a simple one-line addition, as shown here (all one line): exten => s, n, system(echo IN#${CALLERID(name)} ↪#${CALLERID(number)}#${UNIQUEID} > ↪/tmp/panels/cid/${UNIQUEID}) This line creates a file in /tmp/panels/cid that contains four fields, delimited by the # character. Of course, you need to create /tmp/panels/cid and give it appropriate permissions so that the Asterisk server can create files in it Caller ID with Asterisk and Ajax http://0-delivery.acm.org.innopac.lib.ryerson.ca/10.1145/1190000/11... 2 of 8 8/27/2007 7:15 PM and the CGI script can read and delete those files. The first field is either IN or OUT and indicates that the call is INcoming, or OUTgoing. The next two fields call the CALLERID() function to retrieve the caller's name and phone number. The last field is the call's unique identifier. You need to place this line in your dial plan, such that the server has already received the caller-ID information but before the call is handed off to the dial command. If you want to receive information about outgoing calls, you could add a line like this to your dial plan: exten => s, n, system(echo OUT##${EXTEN}#${UNIQUEID} ↪> /tmp/panels/cid/${UNIQUEID}) In the case of the outgoing call, we don't have any caller-ID information to display, so the second field is left blank. We do know the number that was dialed, which is retrieved via the ${EXTEN} variable in the third field. In both the incoming and outgoing cases, you need to make sure to update the extension field and the priority fields (s and n in this example). For the purpose of demonstration, I've stripped the Web page down to its most basic requirements, as shown in Listing 1. Listing 1. Example Web Page9159l1.qrk CID Test @import cid.css; start_cid(); Your Content Would Go Here. This seemingly simple HTML code does a lot of things. First, it loads the cid.js JavaScript code. Then, it imports a stylesheet called cid.css. This stylesheet will give you a lot of flexibility to customize the appearance of the sticky notes. Then, the HTML code creates four div sections, called phone1 through phone4. These sections will be made visible later on and will be filled in with caller information. Finally, the HTML code starts the periodic polling by calling the start_cid() function. We'll discuss that function later. Even though my CSS skills aren't world-class, I've included a sample cid.css file to get you started (Listing 2). Listing 2. Sample cid.css File9159l2.qrk div#phone1{ background: #FFFFCC; display: none; position: absolute; border-top: thin solid black; border-left: thin solid black; border-right: 6px solid black; border-bottom: 6px solid black; Caller ID with Asterisk and Ajax http://0-delivery.acm.org.innopac.lib.ryerson.ca/10.1145/1190000/11... 3 of 8 8/27/2007 7:15 PM top: 85%; left: 2%; width: 20%; height: 5em; } div#phone2{ background: #FFFFCC; display: none; position: absolute; border-top: thin solid black; border-left: thin solid black; border-right: 6px solid black; border-bottom: 6px solid black; top: 85%; left: 27%; width: 20%; height: 5em; } div#phone3{ background: #FFFFCC; display: none; position: absolute; border-top: thin solid black; border-left: thin solid black; border-right: 6px solid black; border-bottom: 6px solid black; top: 85%; left: 52%; width: 20%; height: 5em; } div#phone4{ background: #FFFFCC; display: none; position: absolute; border-top: thin solid black; border-left: thin solid black; border-right: 6px solid black; border-bottom: 6px solid black; top: 85%; left: 77%; width: 20%; height: 5em; } This CSS file could have been made more concise by putting all of the common formatting in a common class; I'll leave that as an exercise for the reader. This stylesheet creates four evenly spaced sticky notes at the bottom of the screen. The sticky notes are yellow with a neat 3-D drop-shadow effect (Figure 1). Caller ID with Asterisk and Ajax http://0-delivery.acm.org.innopac.lib.ryerson.ca/10.1145/1190000/11... 4 of 8 8/27/2007 7:15 PM Figure 1. The incoming call information is displayed in a Web page in sticky note format. Now, it's time to take a look at the CGI script (Listing 3). Listing 3. CGI Script9159l3.qrk

Referência(s)