<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://labs.echoditto.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>EchoDitto Labs - Asterisk for Web Developers - Comments</title>
 <link>http://labs.echoditto.com/asterisk-for-web-developers</link>
 <description>Comments for &quot;Asterisk for Web Developers&quot;</description>
 <language>en</language>
<item>
 <title>I&#039;ll try to install the</title>
 <link>http://labs.echoditto.com/asterisk-for-web-developers#comment-3882</link>
 <description>&lt;p&gt;I&#039;ll try to install the asterisk too and for that I will use this steps.&lt;/p&gt;
</description>
 <pubDate>Wed, 20 Feb 2008 08:39:47 -0800</pubDate>
 <dc:creator>best voip service</dc:creator>
 <guid isPermaLink="false">comment 3882 at http://labs.echoditto.com</guid>
</item>
<item>
 <title>Asterisk for Web Developers</title>
 <link>http://labs.echoditto.com/asterisk-for-web-developers</link>
 <description>&lt;p&gt;You&#039;ve probably heard of Asterisk before.  It&#039;s one of those buzzed-about open source projects that keeps popping up on &lt;a href=&quot;http://radar.oreilly.net&quot;&gt;O&#039;Reilly&lt;/a&gt; and &lt;a href=&quot;http://slashdot.org&quot;&gt;Slashdot&lt;/a&gt;.  Take our word for it: it&#039;s fun stuff.  At EchoDitto we run our office phone system on it, have built client applications around it, and even made a version of Tetris for our lobby monitors that runs on it (more on that later).&lt;/p&gt;

&lt;p&gt;But getting into Asterisk can seem more intimidating than it really is.  There&#039;s plenty of documentation around, but a lot of it seems to assume that the reader has just come home from his or her job managing AT&amp;amp;T&#039;s fiberoptic backbone and is now hoping to screw around with some open-source fun.  In other words, it&#039;s written for an audience of VoIP engineers.&lt;/p&gt;

&lt;p&gt;Well, none of us are VoIP engineers.  If you&#039;re like us, you&#039;re a general technologist who builds stuff on the web, knows that in five years your toaster  will have Ruby bindings, and realizes that There Is No Spoon.  You&#039;ve used Skype and heard of Vonage, but you don&#039;t know much about VoIP besides that.  And now you&#039;d like to use Asterisk to recreate the final scene in &lt;em&gt;The Lawnmower Man&lt;/em&gt; (the director&#039;s cut) &amp;mdash; or maybe just have your website talk to people over the phone.&lt;/p&gt;

&lt;p&gt;We can do that!  In fact, it&#039;s probably easier than you think.  In this post I&#039;ll outline how to get up and running with Asterisk, how to connect it to the Plain Ol&#039; Telephone System (POTS) and how to connect it to your programs.  From there it&#039;s up to you.&lt;/p&gt;

&lt;h2&gt;What Is Asterisk?&lt;/h2&gt;

&lt;p&gt;Let&#039;s start slow.  Asterisk is an open source project that works as a Private Branch Exchange, or PBX.  A PBX is to the phone system as a LAN is to the internet.  You probably already have one in your office for routing calls to different extensions and handling voicemail.  It might also provide conferencing capabilities, or provide an IVR system &amp;mdash; &quot;press 1 for your balance, press 2 if you lost your card&quot; &amp;mdash; that sort of thing.  Asterisk can perform all of these functions (and many others).&lt;/p&gt;

&lt;p&gt;If you&#039;re interested in setting up a PBX for your home or office, you may want to check out the &lt;a href=&quot;http://www.trixbox.org/&quot;&gt;trixbox project&lt;/a&gt;, which adds a bunch of useful tools and scripts to an Asterisk installation, including a web frontend for configuring the system.  It&#039;s what we use for our office PBX here at EchoDitto, and is available as a liveCD so that you can try it out before committing to it.&lt;/p&gt;

&lt;p&gt;But the extra stuff that comes with trixbox can make developing Asterisk applications confusing, and (if memory serves) its installer kind of wants the target machine to itself.  If you&#039;re reading this you&#039;re probably just getting started; you want to keep things simple and don&#039;t want to commit to anything too drastic.  So let&#039;s just install a vanilla version of Asterisk.&lt;/p&gt;

&lt;h2&gt;Installing Asterisk&lt;/h2&gt;

&lt;p&gt;You can download Asterisk from &lt;a href=&quot;http://www.asterisk.org/&quot;&gt;here&lt;/a&gt;.  It&#039;s been a little while, but my recollection is that it&#039;s a very easy installation.  Read the docs, but I believe that ./configure &amp;&amp; make &amp;&amp; sudo make install will pretty well handle things.  There weren&#039;t any missing dependencies on the Red Hat boxes I&#039;ve installed it on.&lt;/p&gt;

&lt;p&gt;Make sure Asterisk is set to start automatically in /etc/inittab (make sure your runlevels are properly configured):&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;# Asterisk
as:345:respawn:/usr/sbin/asterisk -f&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;then &lt;em&gt;#/sbin/telinit q&lt;/em&gt; to get it started.  You should now be able to connect to the Asterisk console from the command line like so:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;#/usr/sbin/asterisk -r&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;If that works, then everything&#039;s probably A-OK.&lt;/p&gt;

&lt;h2&gt;What Can Asterisk Talk To?&lt;/h2&gt;

&lt;p&gt;So you&#039;ve got an Asterisk server.  How do you actually connect it to things?  Well, first let&#039;s figure out what sorts of things you can connect to the server:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Phones like VoIP handsets, software-based VoIP phones, or VoIP adapters (which let you connect an analog handset to a VoIP network).  These generally speak the SIP protocol &lt;/li&gt;

&lt;li&gt;Other VoIP servers &amp;mdash; either peering arrangements that let you connect PBXs together (and allow you to make free calls between them) or connections to terminators that can send traffic through to POTS lines &amp;mdash; i.e. they can call regular phones.&lt;/li&gt;

&lt;li&gt;Special hardware providing direct connections to POTS lines or other network configuration.  This is not only beyond my expertise but also relatively expensive.  Unless you have a very good reason for doing it, don&#039;t bother.  There are other folks out there who perform this function and will sell you service.&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Setting Up A Softphone&lt;/h2&gt;

&lt;p&gt;Let&#039;s test things out a little further &amp;mdash; and take this opportunity to enter the exciting world of Asterisk config files (woo!).  But first, a note of warning: VoIP does not play nicely with NAT.  Is there a router in between you and the Asterisk server?  If so, you&#039;ve got more work ahead of you.  Forwarding the port for the SIP protocol (5060) will let command controls move freely between your softphone and Asterisk, but the actual audio is carried via separate RTSP streams.  And, to be honest, it can be a nasty business to open the necessary ports properly.  For simplicity&#039;s sake, my best advice is to either use an Asterisk server on the same LAN as you, or to make your home machine your router&#039;s DMZ host &amp;mdash; only temporarily, of course, for security reasons.  The rest of this tutorial assumes that there isn&#039;t any port-opening business to be attended to.  If there is, consult &lt;a href=&quot;http://portforward.com&quot;&gt;portforward.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Okay!  The first thing to do is to create a new SIP user.  It&#039;s pretty easy to do: open up /etc/asterisk/sip.conf in a text editor and add this to the bottom:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;[test]
type=friend
allow=all
context=demo
username=test
authname=test
secret=test
host=dynamic
canreinvite=no&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;This isn&#039;t very secure, but for now it&#039;ll do.  This creates a new user called &quot;test&quot; with password &quot;test&quot; and a default context of &quot;demo&quot;, which happens to be a context included with asterisk by default (more on this shortly).  We&#039;re going to need to tell Asterisk to reexamine the sip.conf file, though.  To do this, go to the Asterisk console (#/usr/sbin/asterisk -rvvv) and type &quot;sip reload&quot;.  Keep an eye out for any parsing errors &amp;mdash; if there are some, double-check that you edited sip.conf properly.&lt;/p&gt;

&lt;p&gt;Now let&#039;s configure a softphone.  I&#039;m a fan of &lt;a href=&quot;http://www.sjlabs.com/sjp.html&quot;&gt;SJPhone&lt;/a&gt;, since it crashes less on my Intel Mac than X-Lite seems to.  But use whatever you&#039;d like &amp;mdash; the settings will be similar:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &quot;Preferences&quot;, then the &quot;Profiles&quot; tab.&lt;/li&gt;
&lt;li&gt;Create a new Profile. Give it a descriptive name and select &quot;Calls through SIP proxy&quot; as the Profile Type.&lt;/li&gt;
&lt;li&gt;Edit the new profile (if the editing dialog didn&#039;t pop up immediately upon hitting &quot;OK&quot;.&lt;/li&gt;
&lt;li&gt;Go to the &quot;SIP Proxy&quot; tab.  Enter the IP or domain name of your Asterisk server in the &quot;Proxy Domain&quot;.  You should be able to leave the port number as &quot;0&quot; &amp;mdash; it&#039;ll default to 5060, which is the default number for SIP.&lt;/li&gt;
&lt;li&gt;Save the profile.  Back on the profile list screen, highlight the new entry and click the &quot;Use&quot; button.  You should be prompted for a username and password.  Password is synonymous with &quot;secret&quot; in the configuration above.  So if you use that configuration snippet unaltered, enter &quot;test&quot; for both values.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Alright!  Hopefully SJPhone will say something like &quot;SIP: registered&quot; in its status window.  Enter &quot;s&quot; into the textbox at the top and hit &quot;Dial&quot;.&lt;/p&gt;

&lt;p&gt;If all is well you should hear a burst of fake-dialing, then a welcome message from Asterisk.  We&#039;ve just dialed the &quot;s&quot; extension in the &quot;demo&quot; context.  The extension was defined by entering it in the text box; your user&#039;s default context was set in the stuff we added to sip.conf. Feel free to navigate the demo context to see what&#039;s available.&lt;/p&gt;

&lt;h2&gt;The Dialplan: Contexts, Extensions and Priorities&lt;/h2&gt;

&lt;p&gt;You&#039;re probably familiar with the idea of an extension.  At work, mine&#039;s 117.  It&#039;s probably something similar for you.  But what&#039;s a context?&lt;/p&gt;

&lt;p&gt;Well, there are only so many numbers on a telephone keypad.  That&#039;s the obvious way to navigate the flow of the dialplan.  But in some places you might want, say, the &quot;2&quot; button to mean something different than in another place.  For instance, at the start of the call it might mean you want to hear menus in Spanish.  Later on in the call it might mean you want to check your balance.&lt;/p&gt;

&lt;p&gt;This is where contexts come in.  Each line in the dial plan must have a unique combination of context, extension and priority.  Think of it like a street address: the context is the town, the extension is the street and the priority is the house number. Here&#039;s the &quot;s&quot; extension we just played, straight from /etc/asterisk/extensions.conf:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;[demo]
;
; We start with what to do when a call first comes in.
;
exten =&gt; s,1,Wait,1                     ; Wait a second, just for fun
exten =&gt; s,n,Answer                     ; Answer the line
exten =&gt; s,n,Set(TIMEOUT(digit)=5)      ; Set Digit Timeout to 5 seconds
exten =&gt; s,n,Set(TIMEOUT(response)=10)  ; Set Response Timeout to 10 seconds
exten =&gt; s,n(restart),BackGround(demo-congrats) ; Play a congratulatory message
exten =&gt; s,n(instruct),BackGround(demo-instruct)        ; Play some instructions
exten =&gt; s,n,WaitExten          ; Wait for an extension to be dialed.&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;There&#039;s quite a bit of stuff to unpack here.  Let&#039;s jump right in:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;The [demo] defines the context.  Simple enough.  Dialplan commands listed after this will be added to the &quot;demo&quot; context, unless we run into another context declaration.&lt;/li&gt;
&lt;li&gt;Semicolons denote comments.  Easy enough.&lt;/li&gt;
&lt;li&gt;&quot;exten =&gt; &quot; is the syntax for defining an extension in the dialplan.  Again, simple enough.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Here we get to the meat of it.  s,1,Wait,1 &amp;mdash; what does that mean?&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Well, &quot;s&quot; is the extension &amp;mdash; just like we dialed.&lt;/li&gt;
&lt;li&gt;The first &quot;1&quot; is the priority.  Commands are executed in ascending order of priority.  You might notice that the next lines have &quot;n&quot; instead of a number.  This is just shorthand; it means &quot;assign this line the priority of the last line, plus one&quot;.&lt;/li&gt;
&lt;li&gt;&quot;Wait&quot; is the actual application command.  You can find a full list of these &lt;a href=&quot;http://www.voip-info.org/wiki-Asterisk+-+documentation+of+application+commands&quot;&gt;here&lt;/a&gt;.  &quot;Wait&quot; is a pretty straightforward one &amp;mdash; it makes the system pause for the specified amount of time.&lt;/li&gt;
&lt;li&gt;The final &quot;1&quot; is the parameter being passed to &quot;Wait&quot;.  If the command accepted multiple parameters, they would be specified in order, separated by commas.  In this case the parameter is indicating that &quot;Wait&quot; should pause the program for one second.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;I should also point out that &quot;s&quot; isn&#039;t usually defined as a dialable extension.  There&#039;s no &quot;s&quot; key on the standard telephone, after all.  Instead, &quot;s&quot; is frequently used in Asterisk dialplans as the &lt;strong&gt;s&lt;/strong&gt;tarting extension in a context.  There are other special extension names, too: &quot;h&quot; is executed when a call is &lt;strong&gt;h&lt;/strong&gt;ung up, &quot;t&quot; fires when the dialplan &lt;strong&gt;t&lt;/strong&gt;imes out while waiting for input, and so on.  You can find a complete list of these special extensions &lt;a href=&quot;http://www.voip-info.org/wiki/index.php?page=Asterisk+standard+extensions&quot;&gt;here&lt;/a&gt;.  You can also use patterns to match ranges of input.  You can find more detail on that &lt;a href=&quot;http://www.voip-info.org/wiki/index.php?page=Asterisk+Dialplan+Patterns&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hopefully this makes sense.  It&#039;s possible to include multiple files in your dialplan by placing references to them in /etc/asterisk/extensions.conf. But extensions.conf is where you&#039;ll probably want to do your initial exploration.  Just as with sip.conf, you need to go to the Asterisk console and type &quot;extensions reload&quot; to get Asterisk to notice your changes.&lt;/p&gt;

&lt;p&gt;Have a look around the &lt;a href=&quot;http://www.voip-info.org/wiki-Asterisk+-+documentation+of+application+commands&quot;&gt;list of application commands&lt;/a&gt; and see what&#039;s possible.  You might want to try defining some new extensions in a new context and playing around with them.  Be sure to change the user&#039;s default context in sip.conf to the new one!  Naturally, you can also use application commands to jump between contexts and extensions.  Hopefully this will all seem familiar to anyone who&#039;s programmed in BASIC.&lt;/p&gt;

&lt;h2&gt;Scripting the Dialplan&lt;/h2&gt;

&lt;p&gt;Perhaps the most interesting application command is the one called &lt;a href=&quot;http://www.voip-info.org/wiki-Asterisk+AGI&quot;&gt;AGI&lt;/a&gt;.  This stands for Asterisk Gateway Interface and, like CGI technology, it provides a way to handle Asterisk calls using your favorite scripting language.  There are AGI bindings for Perl, Python, Ruby and PHP, to name a few (check out the preceding link to find these projects).  Most, if not all of the Asterisk application commands are available in an AGI session, and it&#039;s possible to pass Asterisk variables such as the phone number of the caller to the script.&lt;/p&gt;

&lt;p&gt;The possibilities opened up by this are enormous.  You could make incoming calls send people IM messages.  You could connect your Asterisk server to an &lt;a href=&quot;http://en.wikipedia.org/wiki/X10_%28industry_standard%29&quot;&gt;X10 controller&lt;/a&gt; and adjust your lights and thermostat over the phone.  You could hit a database and update statistics on a website.  The only real limits are your imagination, available hardware, and the phone&#039;s admittedly clumsy interface.&lt;/p&gt;

&lt;p&gt;The downside to this is that each time you hit an AGI command a new interpreter for your scripting language is spawned, which consumes system resources.  If enough of them are spawned it can negatively affect the Asterisk server&#039;s performance.  This is why you should resist the temptation to simply write off the dialplan and throw every incoming call to a scripting language.&lt;/p&gt;

&lt;p&gt;But you still might run into AGI overhead problems if your application includes some AGI and is heavily used.  If you do, consider &lt;a href=&quot;http://www.voip-info.org/wiki/index.php?page=Asterisk+FastAGI&quot;&gt;FastAGI&lt;/a&gt;, which lets you run those sessions on a separate machine over the network.  Projects like &lt;a href=&quot;http://adhearsion.com/&quot;&gt;Adhearsion&lt;/a&gt; aim to make the process even more efficient.&lt;/p&gt;

&lt;p&gt;But we&#039;re getting ahead of ourselves.  We haven&#039;t even connected Asterisk to the plain ol&#039; telephone system yet!  Let&#039;s find a vendor and connect to the POTS.&lt;/p&gt;

&lt;h2&gt;Connecting the POTS&lt;/h2&gt;

&lt;p&gt;Actually, I&#039;m going to outsource most of this work.  That&#039;s because vendors&#039; configurations differ, and most of them will provide customized configuration directives to copy and paste into Asterisk&#039;s config files.&lt;/p&gt;

&lt;p&gt;As for specific vendors, I like Broadvoice and Vitelity, both of which are very Asterisk-friendly.  Broadvoice is aimed at home users.  It has extensive Asterisk documentation, but imposes a limit on the number of concurrent channels that can be used (in other words, the number of simultaneous inbound and outbound calls).  For that reason I prefer Vitelity, which is oriented toward large VoIP projects that will be making or receiving a lot of simultaneous calls &amp;mdash; it&#039;s nice to have that freedom.  Both vendors offer very low-cost startup options, and will provide you with customized configs to place in sip.conf and extensions.conf.&lt;/p&gt;

&lt;p&gt;I believe that Broadvoice will set you up with an inbound number, but with Vitelity you&#039;ll have to order one.  In VoIP-speak inbound numbers are called &quot;DIDs&quot;.  They&#039;ll run you an additional dollar or two per month, in addition to the traffic that goes to them (which generally costs between 1 and 3 cents per minute).  You can make outbound POTS calls without a DID, but not inbound.&lt;/p&gt;

&lt;p&gt;So I&#039;ll go ahead and assume that you signed up with Vitelity.  You updated your configuration files, did a &quot;sip reload&quot; and &quot;extensions reload&quot;, and Asterisk&#039;s console isn&#039;t complaining about any registration errors.  When you type &quot;sip show registry&quot; in the Asterisk console you should see something like this:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;Host                            Username       Refresh State               
inbound1.vitelity.net:5060      your_username           45 Registered&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;&quot;Registered&quot; being the most important part, of course.  Additionally, when you dial the DID number on your phone, you should start to see messages scroll past in the Asterisk console.  If you alter the context for the vitel-inbound SIP registration (in sip.conf) to point to &quot;demo&quot;, you should be able to dial the DID number and get Asterisk&#039;s welcoming spiel.&lt;/p&gt;

&lt;p&gt;The sound quality of the call is much too large a topic to cover here.  But if you experience problems, you&#039;ll probably need to ensure that your connection has sufficient bandwidth, low latency and low jitter.  Googling around for these terms should help you figure out how to diagnose your problem.  You may ultimately need to install a traffic-shaping firewall that prioritizes VoIP packets.  Finally, note that Asterisk can use a variety of codecs to encode audio.  There&#039;s generally a tradeoff between bandwidth, sound quality and processor use that needs to be considered.&lt;/p&gt;

&lt;p&gt;But for now you can probably ignore all of that.  So congratulations!  You&#039;re now connected to the public telephone network, with all the rights and privileges that entails.&lt;/p&gt;

&lt;h2&gt;Making Calls Programmatically&lt;/h2&gt;

&lt;p&gt;Okay.  You can receive inbound calls.  If you&#039;ve pored over the application command documentation, you&#039;ve probably even figured out how to make calls to an outside number from an extension (hint: &lt;em&gt;exten =&gt; s,n,Dial(SIP/15551234567@vitel-outbound,30,rg)&lt;/em&gt;).  But what about making your system initiate the call?&lt;/p&gt;

&lt;p&gt;It&#039;s pleasantly simple, actually.  The secret is .call files.  These simple text files are placed into a spool directory.  Asterisk picks them up, reads them, deletes them, and initiates the call that they specify.  Here are the complete contents of a sample .call file:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;Channel: SIP/15551234567@vitel-outbound
MaxRetries: 0
RetryTime: 300
WaitTime: 15
Context: demo
Extension: s
Priority: 1
Set: USERID=12345&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Let&#039;s go line-by-line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Channel&lt;/em&gt; defines what number will be called.  In this case it&#039;s saying to make a SIP call to extension 15551234567 under the &lt;em&gt;vitel-outbound&lt;/em&gt; context in sip.conf.  If you&#039;re using Vitelity, this is the default outbound context, which they should supply you with.&lt;/li&gt;

&lt;li&gt;&lt;em&gt;MaxRetries&lt;/em&gt; specifies how many times the call will be retried if it fails (for example, if it encounters a busy signal).&lt;/li&gt;

&lt;li&gt;&lt;em&gt;WaitTime&lt;/em&gt; specifies the time between retries.&lt;/li&gt;

&lt;li&gt;&lt;em&gt;Context&lt;/em&gt; sets the context where the user will find themselves when they pick up the phone.  The same goes for &lt;em&gt;Extension&lt;/em&gt; and &lt;em&gt;Priority&lt;/em&gt; &amp;mdash; these three values determine where in the dialplan the user will be placed when the call is connected.&lt;/li&gt;

&lt;li&gt;&lt;em&gt;Set&lt;/em&gt; allows you to set variables that will be passed to Asterisk.  You can have more than one Set command.  In this case, we&#039;re passing the value &quot;12345&quot; into the ${USERID} variable.  This variable can be used by the dialplan and passed to AGI scripts.  It&#039;ll help you keep track of exactly who a particular call corresponds to, or what it&#039;s supposed to do.  Don&#039;t underestimate its usefulness &amp;mdash; this is where the real application-integration action happens.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So you place this into a file with a .call extension &amp;mdash; temp.call, let&#039;s say.  You should do this outside the spool directory (/var/spool/asterisk/outgoing), then move the file into it.  Asterisk doesn&#039;t have systems in place to keep it from trying to read from a half-written file.&lt;/p&gt;

&lt;p&gt;Here&#039;s some sample PHP code to make a call using Asterisk:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;&amp;lt;?php
$handle = fopen(&quot;/tmp/temp.call&quot;,&quot;w&quot;);

$contents = &quot;Channel: SIP/&quot; . $_POST[&#039;phone_number_to_call&#039;] . &quot;@vitel-outbound
MaxRetries: 0
RetryTime: 300
WaitTime: 15
Context: demo
Extension: s
Priority: 1
Set: USERID=&quot; . $_SESSION[&#039;user_id&#039;] . &quot;
&quot;;

fwrite($handle,$contents);

fclose($handle);

exec(&quot;mv /tmp/temp.call /var/spool/asterisk/outgoing&quot;);
?&amp;gt;&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Naturally, you might have some filesystem permissions to deal with.  But this is the basic shape of it.  Define your SIP registrations.  Define your dialplan contexts and the extensions that flow through them.  Perform fancy integration with AGI.  Process incoming calls to your DID by setting its context, extension and priority to the right portion of the dialplan.  Make outgoing calls with .call files.&lt;/p&gt;

&lt;h2&gt;One Last Thing&lt;/h2&gt;

&lt;p&gt;One of the first things you&#039;ll likely find yourself wanting to do is play customized audio to the user.  This can be easily accomplished with the &lt;a href=&quot;http://www.voip-info.org/wiki/index.php?page=Asterisk+cmd+Playback&quot;&gt;Playback&lt;/a&gt; application command (there are &lt;a href=&quot;http://www.voip-info.org/wiki-Asterisk+-+documentation+of+application+commands#SoundsPlayback&quot;&gt;a few other relevant ones&lt;/a&gt;, too).  But getting the audio files into a readily-usable format can be harder.  &lt;a href=&quot;http://www.voip-info.org/tiki-index.php?page=Convert+WAV+audio+files+for+use+in+Asterisk&quot;&gt;This page&lt;/a&gt; provides a good starting point.  The short version: you&#039;re going to want to convert your WAV files into .gsm or (preferably) .sln files using the &lt;em&gt;sox&lt;/em&gt; utility.&lt;/p&gt;

&lt;p&gt;As for voice synthesis: there are a few text-to-speech libraries that can be plugged into Asterisk, such as &lt;a href=&quot;http://nerdvittles.com/?p=134&quot;&gt;Flite&lt;/a&gt;.  I&#039;ve had a hell of a time getting them configured, though (except under Trixbox, which comes with them built-in).  If you run into the same trouble I did, you can always use AGI to generate and convert the audio you need on-the-fly using command-line tools like text2wave and sox.  You&#039;ll need to be wary of efficiency problems, and will want to cache whenever possible.  It also may sound abysmal. But it&#039;ll work in a pinch.&lt;/p&gt;

&lt;p&gt;Finally, let me recommend &lt;a href=&quot;http://nerdvittles.com&quot;&gt;Nerdvittles.com&lt;/a&gt;, an Asterisk-heavy tech blog.  Phil turned me on to it, and it&#039;s really got a lot of excellent Asterisk-related content.  You may also want to check out the various Asterisk-related channels on irc.freenode.net.&lt;/p&gt;

&lt;h2&gt;We&#039;re Done!&lt;/h2&gt;

&lt;p&gt;Whew... I think that&#039;s it!  My sincere apologies for any errors, misunderstandings or gross oversimplifications above &amp;mdash; as I said, I&#039;m just an generalist web guy who&#039;s muddled his way through this stuff.  But with the techniques outlined above and a little time in the documentation, odds are good that you&#039;ll be able to hack together something cool.  The barrier to entry is very low: a Linux box, an internet connection and $10 are about all you&#039;ll need.  Happy hacking, and let me know if you put together anything particularly neat.&lt;/p&gt;</description>
 <comments>http://labs.echoditto.com/asterisk-for-web-developers#comments</comments>
 <category domain="http://labs.echoditto.com/taxonomy/term/59">asterisk</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/3">perl</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/60">php</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/62">tutorial</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/61">webdev</category>
 <pubDate>Tue, 12 Jun 2007 14:35:43 -0700</pubDate>
 <dc:creator>Tom</dc:creator>
 <guid isPermaLink="false">31 at http://labs.echoditto.com</guid>
</item>
</channel>
</rss>
