The Hitchhiker's Guide to Asterisk | ||
---|---|---|
<<< Previous | Creating Dialplans | Next >>> |
Now we're ready to create our first extensions.conf file. Because this is our first step, we'll start with a very simple example. We'll assume for this example that all Asterisk needs to do is to answer the channel, play a sound file that says "Goodbye", and then hang up. We'll use this simple example to point out the fundamentals of creating a dialplan.
Before we get too far into our example, we need to cover a special extension called 's', which stands for "start". By default, calls will start in a context's 's' extension. (You can think of 's' as the extension that gets automatically executed.) In our example, we'll start by creating a dialplan with this 's' extension.
If we're going to answer the call, play a sound file, and then hang up, we'd better learn how to do just that. The Answer() application is used to answer a channel which is ringing. This does the initial setup for the call so that we can perform other functions. A few applications don't necessarily require that we Answer() the channel first, but it is a very good habit to properly Answer() the channel before doing anything else.
The Playback() function is used for playing a previously recorded sound file over a channel. When using the Playback() function, input from the user is simply ignored. If you need to accept input during playback, use the Background() function. Asterisk comes with many professionally recorded sound files which are often found in /var/lib/asterisk/sounds/. Playback() is used by specifying the filename (without a file extension) as the argument. For example, Playback(filename) would play the sound file called filename.gsm, located in the default sounds directory.
The Hangup() function does exactly as it's name implies; it hangs up an active channel. You would use this at the end of a context once you want to drop a caller who no longer needs to be connected to the system.
Now we're ready to start our first example dialplan. Please pay attention to the way that each priority calls an application. Note that in this example, we only have one extension. In later examples we'll add other extensions, and show how to move from one extension to another.
![]() | The following examples are not meant to be completely working and usable. We are simply using these examples to explain how dialplans work. For these examples to work, you should have already configured some Zap channels (using a Devkit from Digium, for example), and configured those channels so that incoming calls go to the [incoming] context. |
After showing our first example, we'll explain each step.
When a call is sent into this [incoming] context, it will first go to 's' extension. As we learned earlier, calls usually begin in the 's' extension. We have three priorities in this context, numbered 1, 2 and 3. Each priority calls a particular application. Let's take a closer look at these three priorities.
Our first priority calls the Answer() application. Asterisk then takes control of the line and sets up the call. After answering the line, Asterisk goes on to the next priority. In our second priority, we call the Playback() application. This will play a sound file as specified by the filename. In our example we will play the file vm-goodbye. The caller will hear a voice say "goodbye". Notice that there is no filename extension. Asterisk will automatically determine the extension of the sound file. In our third and final priority line, we call the Hangup() function and thus end the call.
Now that we've gone through a simple example, let's build upon it by learning about the Background() and Goto() applications. These two applications will allow us to create dialplans with much more functionality.
The key to interactive Asterisk systems is the Background() application. It gives you the ability to play a recorded sound file, but when the caller presses a key it interrupts the playback and goes to the extension that corresponds with what the caller dialed.
Another very useful application is called GoTo(). As its name implies, it jumps from the current context, extension, and priority to the specified context, extension, and priority. The GoTo() application makes it easy to programatically move between different parts of the dialplan. The syntax for the GoTo() application calls for us to pass the destination context, extension, and priority as arguments to the application, like this:
In this example, let's assume we've been asked by a local movie theater to create an interactive system where callers can dial in and listen to pre-recorded movie listings. To make this example simple, we'll say that the movie theater only has two screens.
We'll also assume that we have three pre-recorded sound files. The first, called current-moves.gsm, says "Welcome to our movie theatre. To hear what's playing on screen one, press one. To hear what's playing on screen two, press two." The other two sound files, named movie1.gsm and movie2.gsm respectively, tell the caller the information about the movie playing on that particular screen.
![]() | We'll cover recording sound files later in this chapter. |
[incoming] exten => s,1,Answer() exten => s,2,Background(current-movies) exten => s,3,Hangup() exten => 1,1,Playback(movie1) exten => 1,2,Goto(incoming,s,1) exten => 2,1,Playback(movie2) exten => 2,2,Goto(incoming,s,1) |
Lets go through this example step by step. When the call enters the system, Asterisk executes the 's' extension automatically, starting with priority one. You may notice that the 's' extesion looks almost identical to our first example. The difference is the use of the Background() application instead of Playback(). With Background() we are able to accept digits from the caller while the sound file is being played. While the current-movies.gsm file is being played to the caller, lets say the user presses 1. Asterisk will then look for an extension in our current context that matches it. When Asterisk finds the '1' extension, it will execute all the priorities for that extension.
Now that the user has pressed '1', Asterisk can perform both priorities for extension 1. The first priority for extension 1 will use the Playback() application to play the movie details for screen one. After the file finishes playing, it will execute the second priority, which is a call to the Goto() application.
Remember that Goto() allows us to send the caller somewhere else in our dialplan. In our example exten => 1,2,Goto(incoming,s,1) we will send the user back to the first priority of the 's' extension in our current context.
If the user doesn't press a key before the Background() application finishes playing the file, the third priority of our 's' extension will be performed, hanging up the user. This is probably not the best way to handle incoming calls, but gives us a good example of how we can manipulate calls.
If the caller presses 2, then the sound file for movie screen number two is played, and then the caller is sent back to the 's' extension.
As you can see, it's quite simple to create interactive dialplans (often referred to as auto-attendants or voice menus) in Asterisk. In less than ten lines, we've been able to create a dialplan that could really be used. Now let's add additional functionality.
We are going to add to our movie theater example by showing the use of the Dial() application. If someone presses 0 during playback it will ring the ticket office. We are going to assume that the channel for the ticket office has already been setup. For examples of how to setup channels see Chapter 3.
[incoming] exten => s,1,Answer() ; Answer the line exten => s,2,Background(current-movies) ; Play back the 'current movies' sound file exten => s,3,Hangup() ; Now hangup the line if the caller doesn't press a key exten => 1,1,Playback(movie1) ; Now hangup the line if the caller doesn't press a key exten => 1,2,Goto(incoming,s,1) ; Now go back to the beginning exten => 2,1,Playback(movie2) ; Now hangup the line if the caller doesn't press a key exten => 2,2,Goto(incoming,s,1) ; Now go back to the beginning exten => 0,1,Dial(Zap/1) |
Our example has stayed the same except for the addition of another extension to our [incoming] context. We have added a 0 extension which will then execute the Dial application. The Dial() application allows us to call a specified interface using the format of [technology]/[resource]. In this instance we are calling the first channel on our Zaptel interface, which probably has attached to it a telephone and an operator to answer the phone.
By this point in the chapter you should understand the use of several applications such as Answer(), Playback() , Background(), Hangup(), GoTo() and the basics of Dial(). If you are still a little unclear of how these work, please go back and read this section again. The basic understanding of these applications is essential to fully grasp the concepts which will be explored further.
<<< Previous | Home | Next >>> |
Creating Dialplans | Up | Adding Additional Functionality |