{"id":447,"date":"2012-09-01T01:00:00","date_gmt":"2012-08-31T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=447"},"modified":"2012-09-27T22:18:40","modified_gmt":"2012-09-27T21:18:40","slug":"asterisk-for-a-small-business-iv","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=447","title":{"rendered":"Asterisk For a Small Business (IV)"},"content":{"rendered":"<p>When last we spoke, we had created an internal phone system. We had four extensions that could all happily call each other. This time we\u00e2\u20ac\u2122re going to extend that facility to include the ability to dial arbitrary numbers using our outgoing trunks.<\/p>\n<p>Recall how we arranged our contexts in the dialplan?<\/p>\n<pre><code>[stdexten]\n; ... standard code for handling a dialled local extension ...\n\n[internal-extensions]\n; ... extension definitions and hints for all our local extensions ...\n\n[internal-residential]\ninclude =&gt; internal-extensions\n[internal-business]    \ninclude =&gt; internal-extensions<\/code><\/pre>\n<p>Also recall that we used the \u00e2\u20ac\u0153<code>context=<\/code>\u00e2\u20ac\u009d option in <code>sip.conf<\/code> to place <code>SIP\/andyp<\/code>, <code>SIP\/wife<\/code>, and <code>SIP\/handytone1<\/code> in the \u00e2\u20ac\u0153<code>[internal-residential]<\/code>\u00e2\u20ac\u009d context; and <code>SIP\/handytone2<\/code> in the \u00e2\u20ac\u0153<code>[internal-business]<\/code>\u00e2\u20ac\u009d context.<\/p>\n<p>At this point, \u00e2\u20ac\u0153internal-residential\u00e2\u20ac\u009d and \u00e2\u20ac\u0153internal-business\u00e2\u20ac\u009d behave identically since they both only include the extensions definition. However, our \u00e2\u20ac\u0153internal-extensions\u00e2\u20ac\u009d context only says what to do for a very limited subset of possible dial strings. What if an endpoint in the \u00e2\u20ac\u0153internal\u00e2\u20ac\u009d context dials something other than another extension? At the moment \u00e2\u20ac\u201c nothing. Let\u00e2\u20ac\u2122s change that to \u00e2\u20ac\u0153something\u00e2\u20ac\u009d.<\/p>\n<pre><code>[internal-residential]\ninclude =&gt; internal-extensions\nexten =&gt; _0X.,1,Dial(SIP\/voiptalkRESIDENTIAL\/${EXTEN})\n\n[internal-business]    \ninclude =&gt; internal-extensions\nexten =&gt; _0X.,1,Dial(SIP\/voiptalkOFFICE\/${EXTEN})<\/code><\/pre>\n<p>Asterisk scans the dialplan in the order it\u00e2\u20ac\u2122s defined, so when a new call enters, say \u00e2\u20ac\u0153internal-residential\u00e2\u20ac\u009d, it hits the included \u00e2\u20ac\u0153internal-extensions\u00e2\u20ac\u009d first. If none of them matches the dialled string, asterisk continues scanning \u00e2\u20ac\u0153internal-residential\u00e2\u20ac\u009d. Here I\u00e2\u20ac\u2122ve added a special extension, \u00e2\u20ac\u0153<code>_0X.<\/code>\u00e2\u20ac\u009d. The \u00e2\u20ac\u0153<code>_<\/code>\u00e2\u20ac\u009d tells Asterisk that it shouldn\u00e2\u20ac\u2122t match this extension literally, instead it should do a pattern match.<\/p>\n<ul>\n<li>\u00e2\u20ac\u0153<code>_<\/code>\u00e2\u20ac\u009d pattern match<\/li>\n<li>\u00e2\u20ac\u0153<code>0<\/code>\u00e2\u20ac\u009d match a literal zero. Therefore anything dialled without a zero prefix will not match.<\/li>\n<li>\u00e2\u20ac\u0153<code>X<\/code>\u00e2\u20ac\u009d match any digit 0-9.<\/li>\n<li>\u00e2\u20ac\u0153<code>.<\/code>\u00e2\u20ac\u009d match any number of any character<\/li>\n<\/ul>\n<p>You can therefore read this extension to mean \u00e2\u20ac\u0153anything prefixed with a zero\u00e2\u20ac\u009d. All UK numbers start with a zero (if you count the area code), so will be handled by this entry. The handler runs the following command:<\/p>\n<pre><code>Dial(SIP\/voiptalkRESIDENTIAL\/${EXTEN})<\/code><\/pre>\n<p>Here, \u00e2\u20ac\u0153<code>${EXTEN}<\/code>\u00e2\u20ac\u009d will be replaced not with the pattern but with the string that matched the pattern \u00e2\u20ac\u201c i.e.\u00c2\u00a0what the caller dialled. This form of \u00e2\u20ac\u0153Dial()\u00e2\u20ac\u009d references one of our endpoints that we defined (and named) in <code>sip.conf<\/code>. In reality, Asterisk looks up the options from <code>sip.conf<\/code> and translates this to:<\/p>\n<pre><code>Dial(SIP\/${EXTEN}:password::username@voiptalk.org)<\/code><\/pre>\n<p>But our symbolic form is far more meaningful to us.<\/p>\n<p>Note now that I\u00e2\u20ac\u2122ve added a different dial target to each of <code>[internal-residential]<\/code> and <code>[internal-extensions]<\/code>. This means when no local extension matches each dials out externally <em>on a different trunk<\/em>.<\/p>\n<p>For now we\u00e2\u20ac\u2122ve addresses the two outgoing requirements from our list:<\/p>\n<ul>\n<li><code>andyp<\/code>, <code>wife<\/code>, <code>handytone1<\/code> and <code>handytone2<\/code> should all be able to ring each other internally.<\/li>\n<li><code>andyp<\/code>, <code>wife<\/code>, and <code>handytone1<\/code> shall make outgoing calls on <code>voiptalkRESIDENTIAL<\/code> by default.<\/li>\n<\/ul>\n<p>We\u00e2\u20ac\u2122ll return to the internal part of the dial plan later, but now we turn to how to process incoming calls.<\/p>\n<p>Recall one of our trunk configurations from <code>sip.conf<\/code>.<\/p>\n<pre><code>[voiptalkOFFICE](voiptalk)\n    defaultuser=222222222\n    secret=your_222222222_secret_goes_here\n    fromuser=222222222\n    context=external\n    callbackextension=trunkOFFICE<\/code><\/pre>\n<p>Now we\u00e2\u20ac\u2122re more familiar with the dialplan, the <code>context<\/code> and <code>callbackextension<\/code> options should make more sense. We already know that the <code>context<\/code> option defines which dialplan context handles inbound calls from this endpoint. Just as we put all our internal endpoints to be handled within an \u00e2\u20ac\u0153[internal-extensions]\u00e2\u20ac\u009d context, we have put our two external endpoints in an \u00e2\u20ac\u0153[external]\u00e2\u20ac\u009d context.<\/p>\n<p>What about the dialled extension though? Remember that from the point of view of our trunk provider, we have only one extension, \u00e2\u20ac\u0153222222222\u00e2\u20ac\u009d in the case of this \u00e2\u20ac\u0153voiptalkOFFICE\u00e2\u20ac\u009d endpoint. In essence then \u00e2\u20ac\u201c there is no information for us to obtain from the dialled extension given by a trunk it will <em>always<\/em> be our single number. That\u00e2\u20ac\u2122s why Asterisk is able to use the <code>callbackextension<\/code> option as a trigger for registration with the provider, all we\u00e2\u20ac\u2122re doing is overriding one fixed piece of information with another (as a fringe benefit it would also address the situation where we had the same username at two providers \u00e2\u20ac\u201c how would we tell them apart in the dialplan?). <code>callbackextension<\/code> then simply defines what Asterisk will rewrite the dialstring to when it runs the dialplan context to handle this incoming call.<\/p>\n<p>Let\u00e2\u20ac\u2122s now add an appropriate \u00e2\u20ac\u0153<code>[external]<\/code>\u00e2\u20ac\u009d context for these incoming calls from an external trunk.<\/p>\n<pre><code>[external]\nexten =&gt; voiptalkRESIDENTIAL,1,Verbose(1,Incoming call on callbackextension=${EXTEN})\nexten =&gt; voiptalkRESIDENTIAL,n,Dial(SIP\/andyp&amp;SIP\/wife&amp;SIP\/handytone1)\nexten =&gt; voiptalkOFFICE,1,Verbose(1,Incoming call on callbackextension=${EXTEN} from ${CALLERID(all)})\nexten =&gt; voiptalkOFFICE,n,Dial(SIP\/andyp&amp;SIP\/handytone2)<\/code><\/pre>\n<p>Here we\u00e2\u20ac\u2122ve used Asterisk\u00e2\u20ac\u2122s ability to specify multiple endpoints in a single \u00e2\u20ac\u0153Dial()\u00e2\u20ac\u009d to ring exactly the extensions we wanted in our original spec.<\/p>\n<ul>\n<li><code>andyp<\/code>, <code>wife<\/code>, <code>handytone1<\/code> and <code>handytone2<\/code> should all be able to ring each other internally.<\/li>\n<li><code>andyp<\/code>, <code>wife<\/code>, and <code>handytone1<\/code> shall make outgoing calls on <code>voiptalkRESIDENTIAL<\/code> by default.<\/li>\n<li><code>handytone2<\/code> shall make outgoing calls on <code>voiptalkOFFICE<\/code> by default.<\/li>\n<li>Incoming calls to <code>voiptalkOFFICE<\/code> shall ring <code>andyp<\/code> and <code>handytone2<\/code><\/li>\n<li>Incoming calls to <code>voiptalkRESIDENTIAL<\/code> shall ring <code>andyp<\/code>, <code>wife<\/code>, and <code>handytone1<\/code>.<\/li>\n<\/ul>\n<p>Check, check, check, check, check.<\/p>\n<p>We\u00e2\u20ac\u2122ve now got a perfectly workable telephone system. Why stop now though? Next time we\u00e2\u20ac\u2122ll look at adding some additional features; in particular: voicemail. Then you can chuck away that answering machine.<\/p>\n<p><a href=\"?p=441\">asterisk1<\/a> <a href=\"?p=443\">asterisk2<\/a> <a href=\"?p=445\">asterisk3<\/a> <a href=\"?p=447\">asterisk4<\/a> <a href=\"?p=449\">asterisk5<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When last we spoke, we had created an internal phone system. We had four extensions that could all happily call each other. This time we\u00e2\u20ac\u2122re going to extend that facility to include the ability to dial arbitrary numbers using our outgoing trunks. Recall how we arranged our contexts in the dialplan? [stdexten] ; &#8230; standard\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=447\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[31,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/447"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=447"}],"version-history":[{"count":9,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}