上一篇介绍了asterisk的基本特性和架构,为了搭建属于自己的call center,跟住要做的事就是配置asterisk的config。
基本上来讲,主要有三个配置文件一定要搞懂:iax.conf,sip.conf,extensions.conf。 iax.conf是配置iax2 client的,sip.conf是配置sip协议的,extensions.conf就是为了让sip和iax协议通信连接。由此可以看到,asterisk其实是一个网关,转换iax和sip协议。
sip.conf
sip.conf可以注册sip用户,用法如下:
;register => username:secret@host/callbackextension
;register => 1234:[email protected]
eg:
[general]
register => 87654321@2b_111001/111001 ; 2b_111001 is a context in bellow and 111001 is the call back extension
.....
.....
[2b_111001]
username=87654321hk
secret=xxxxxxxx
fromuser=87654321hk
context=from_sip ;这个context将在extension.conf中定义
到这里我们注册了一个87654321的sip用户,具体的用户名,你可以向你的sip provider索取。当有电话打87654321是,它将进入extensions.conf的from_sip context,并根据该context的设定来进行一步一步的处理
iax.conf
iax.conf可以注册asterisk的iax用户,用法如下:
; We can register with another IAX server to let him know where we are
; in case we have a dynamic IP address for example
;
; Register with tormenta using username marko and password secretpass
;
;register => marko:[email protected]
;
; Register joe at remote host with no password
;
;register => joe@remotehost:5656
;
; Register marko at tormenta.linux-support.net using RSA key "torkey"
;
;register => marko:[torkey]@tormenta.linux-support.net
;
; Sample Registration for iaxtel
;
; Visit http://www.iaxtel.com to register with iaxtel. Replace "user"
; and "pass" with your username and password for iaxtel. Incoming
; calls arrive at the "s" extension of "default" context.
;
;register => user:[email protected]
;
; Sample Registration for IAX + FWD
;
; To register using IAX with FWD, it must be enabled by visiting the URL
; http://www.fwdnet.net/index.php?section_id=112
;
; Note that you need an extension in you default context which matches
; your free world dialup number. Please replace "FWDNumber" with your
; FWD number and "passwd" with your password.
;
;register => FWDNumber:[email protected]
请大家注意以上注册的iax用户是不属于本地asterisk的用户的,而是remote asterisk的用户,这在asterisk与asterisk之间的通信将发挥用处。
如果要在本地asterisk注册iax用户,只需要配置一个section,eg:
[111001]
type=friend
context=from_iax
secret=xxxxx
host=dynamic
disallow=all
allow=gsm
以上配置注册了一个111001的iax用户,可以从允许任何ip连接注册该用户。当使用该iax用户打电话是,它将执行extensions.conf的from_iax的配置。
extensions.conf
跟住最重要的就是extensions.conf文件啦,它完成iax与sip协议的转换和链接。在这个配置文件里,最重要的概念就是context啦,以下内容摘自extensions.conf的说明:
; Any category other than "General" and "Globals" represent
; extension contexts, which are collections of extensions.
;
; Extension names may be numbers, letters, or combinations
; thereof. If an extension name is prefixed by a '_'
; character, it is interpreted as a pattern rather than a
; literal. In patterns, some characters have special meanings:
;
; X - any digit from 0-9
; Z - any digit from 1-9
; N - any digit from 2-9
; [1235-9] - any digit in the brackets (in this example, 1,2,3,5,6,7,8,9)
; . - wildcard, matches anything remaining (e.g. _9011. matches
; anything starting with 9011 excluding 9011 itself)
; ! - wildcard, causes the matching process to complete as soon as
; it can unambiguously determine that no other matches are possible
;
; For example, the extension _NXXXXXX would match normal 7 digit dialings,
; while _1NXXNXXXXXX would represent an area code plus phone number
; preceded by a one.
;
; Each step of an extension is ordered by priority, which must always start
; with 1 to be considered a valid extension. The priority "next" or "n" means
; the previous priority plus one, regardless of whether the previous priority
; was associated with the current extension or not. The priority "same" or "s"
; means the same as the previously specified priority, again regardless of
; whether the previous entry was for the same extension. Priorities may be
; immediately followed by a plus sign and another integer to add that amount
; (most useful with 's' or 'n'). Priorities may then also have an alias, or
; label, in parentheses after their name which can be used in goto situations.
;
; Contexts contain several lines, one for each step of each extension. One may
; include another context in the current one as well, optionally with a date
; and time. Included contexts are included in the order they are listed.
; Switches may also be included within a context. The order of matching within
; a context is always exact extensions, pattern match extensions, includes, and
; switches. Includes are always processed depth-first. So for example, if you
; would like a switch "A" to match before context "B", simply put switch "A" in
; an included context "C", where "C" is included in your original context
; before "B".
;
;[context]
;exten => someexten,{priority|label{+|-}offset}[(alias)],application(arg1,arg2,...)
;
; Timing list for includes is
;
; <time range>,<days of week>,<days of month>,<months>
;
; Note that ranges may be specified to wrap around the ends. Also, minutes are
; fine-grained only down to the closest even minute.
;
;include => daytime,9:00-17:00,mon-fri,*,*
;include => weekend,*,sat-sun,*,*
;include => weeknights,17:02-8:58,mon-fri,*,*
;
; ignorepat can be used to instruct drivers to not cancel dialtone upon receipt
; of a particular pattern. The most commonly used example is of course '9'
; like this:
;
;ignorepat => 9
;
; so that dialtone remains even after dialing a 9. Please note that ignorepat
; only works with channels which receive dialtone from the PBX, such as DAHDI,
; Phone, and VPB. Other channels, such as SIP and MGCP, which generate their
; own dialtone and converse with the PBX only after a number is complete, are
; generally unaffected by ignorepat (unless DISA or another method is used to
; generate a dialtone after answering the channel).
; For more information on applications, just type "core show applications" at your
; friendly Asterisk CLI prompt.
;
; "core show application <command>" will show details of how you
; use that particular application in this file, the dial plan.
; "core show functions" will list all dialplan functions
; "core show function <COMMAND>" will show you more information about
; one function. Remember that function names are UPPER CASE.
[from_sip]
exten => _XXXXXX,1,GotoIf($[${DEVICE_STATE(IAX2/${EXTEN})} != NOT_INUSE]?busytone)
exten => _XXXXXX,n,Set(DISTRICT=cra)
exten => _XXXXXX,n,MixMonitor(/dev/shm/${DISTRICT}/${STRFTIME(${EPOCH},,%y%m%d)}/${CALLERID(ani)}.00.${STRFTIME(${EPOCH},,%Y%m%d%H%M%S)}.${EXTEN}.${UNIQUEID}.wav49,bv(0)V(0)) ;call rec
exten => _XXXXXX,n,Answer()
exten => _XXXXXX,n,Dial(IAX2/${EXTEN},30,rM(sendUID,${UNIQUEID})) ; 将电话连接到iax用户
exten => _XXXXXX,n,Hangup()
exten => _XXXXXX,n(busytone),Playback(bn-busy)
exten => _XXXXXX,n,Hangup()
还记得在sip.conf中配置的from_sip context吗?当电话87654321进入asterisk后,查询sip.conf取得context(from_sip)后,将执行from_sip的代码。
在asterisk终端可以看到上面的输出如下:
== Using SIP RTP CoS mark 5
-- Executing [111001@from_sip:1] GotoIf("SIP/87654321hk-08b6eaa0", "0?busytone") in new stack
-- Executing [111001@from_sip:2] Set("SIP/87654321hk-08b6eaa0", "DISTRICT=cra") in new stack
-- Executing [111001@from_sip:3] MixMonitor("SIP/87654321hk-08b6eaa0", "/dev/shm/cra/091214/35693480.00.20091214102904.111001.1260757744.83.wav49,bv(0)V(0)") in new stack
-- Executing [111001@from_sip:4] Answer("SIP/87654321hk-08b6eaa0", "") in new stack
== Begin MixMonitor Recording SIP/87654321hk-08b6eaa0
-- Executing [111001@from_sip:5] Dial("SIP/87654321hk-08b6eaa0", "IAX2/111001,30,rM(sendUID,1260757744.83)") in new stack
-- Called 111001
-- Call accepted by 192.168.121.181 (format gsm)
-- Format for call is gsm
-- IAX2/111001-13347 is ringing
-- IAX2/111001-13347 answered SIP/87654321hk-08b6eaa0
-- Executing [s@macro-sendUID:1] SendText("IAX2/111001-13347", "__UID_1260757744.83") in new stack
-- Hungup 'IAX2/111001-13347'
== Spawn extension (2b_in_cra, 111001, 5) exited non-zero on 'SIP/87654321hk-08b6eaa0'
== End MixMonitor Recording SIP/87654321hk-08b6eaa0
当电话呼出是,将查询iax.conf里面context的设定,并执行extensions.conf里该context的内容:
[from_iax]
; outbound to outside
exten => _XXXXXXXX,1,SendText(__UID_${UNIQUEID})
exten => _XXXXXXXX,n,Set(fromCh=${IF($["${BLINDTRANSFER}"=""]?${CHANNEL}:${BLINDTRANSFER})}) ;check if blind transfer
exten => _XXXXXXXX,n,Set(DISTRICT=other)
exten => _XXXXXXXX,n,execIf($["${fromCh:0:6}"="IAX2/1"]?Set(DISTRICT=cra))
exten => _XXXXXXXX,n,execIf($["${fromCh:0:6}"="IAX2/2"]?Set(DISTRICT=crb))
exten => _XXXXXXXX,n,execIf($["${fromCh:0:6}"="IAX2/3"]?Set(DISTRICT=cre))
exten => _XXXXXXXX,n,execIf($["${fromCh:0:6}"="IAX2/4"]?Set(DISTRICT=crk))
exten => _XXXXXXXX,n,execIf($["${fromCh:0:6}"="IAX2/5"]?Set(DISTRICT=crm))
exten => _XXXXXXXX,n,MixMonitor(/dev/shm/${DISTRICT}/${STRFTIME(${EPOCH},,%y%m%d)}/${EXTEN}.00.o_${STRFTIME(${EPOCH},,%Y%m%d%H%M%S)}.${CALLERID(ani)}.${UNIQUEID}.wav49,bv(0)V(0)) ;call rec
exten => _XXXXXXXX,n,Set(2bAcc=2b_${fromCh:5:6})
exten => _XXXXXXXX,n,Dial(SIP/${EXTEN}@${2bAcc},30,r)
exten => _XXXXXXXX,n,Hangup
; internal call (inter agent) iax to iax ,
exten => _XXXX,1,Dial(IAX2/${EXTEN})
exten => _XXXX,n,Hangup
; silence monitor (by ChanSpy) super dial no to monitor call
exten => _*84XXXX,1,Set($MON_CHAN=${EXTEN:3:4})
exten => _*84XXXX,n,ChanSpy(IAX2,bq)
exten => _*84XXXX,n,Hangup
; coaching (by ChanSpy)
exten => _*85XXXX,1,Set($MON_CHAN=${EXTEN:3:4})
exten => _*85XXXX,n,ChanSpy(IAX2,wbq)
exten => _*85XXXX,n,Hangup
asterisk在执行以上代码是终端的输出如下:
-- Accepting AUTHENTICATED call from 192.168.121.181:
> requested format = alaw,
> requested prefs = (),
> actual format = gsm,
> host prefs = (gsm),
> priority = mine
-- Executing [35693480@from_iax:1] SendText("IAX2/111001-9344", "__UID_1260757473.81") in new stack
-- Executing [35693480@from_iax:2] Set("IAX2/111001-9344", "fromCh=IAX2/111001-9344") in new stack
-- Executing [35693480@from_iax:3] Set("IAX2/111001-9344", "DISTRICT=other") in new stack
-- Executing [35693480@from_iax:4] ExecIf("IAX2/111001-9344", "1?Set(DISTRICT=cra)") in new stack
-- Executing [35693480@from_iax5] ExecIf("IAX2/111001-9344", "0?Set(DISTRICT=crb)") in new stack
-- Executing [35693480@from_iax:6] ExecIf("IAX2/111001-9344", "0?Set(DISTRICT=cre)") in new stack
-- Executing [35693480@from_iax:7] ExecIf("IAX2/111001-9344", "0?Set(DISTRICT=crk)") in new stack
-- Executing [35693480@from_iax:8] ExecIf("IAX2/111001-9344", "0?Set(DISTRICT=crm)") in new stack
-- Executing [35693480@from_iax:9] MixMonitor("IAX2/111001-9344", "/dev/shm/cra/091214/35693480.00.o_20091214102433.111001.1260757473.81.wav49,bv(0)V(0)") in new stack
-- Executing [35693480@from_iax:10] Set("IAX2/111001-9344", "2bAcc=2b_111001") in new stack
-- Executing [35693480@from_iax:11] Dial("IAX2/111001-9344", "SIP/35693480@2b_111001,30,r") in new stack
== Using SIP RTP CoS mark 5
-- Called 35693480@2b_111001
== Begin MixMonitor Recording IAX2/111001-9344
-- SIP/from_sip-08b6eaa0 is making progress passing it to IAX2/111001-9344
-- SIP/from_sip-08b6eaa0 answered IAX2/111001-9344
通过以上的介绍,我们可以看到asterisk是通过extensions.conf的context来dial(sip/xxxx)或dial(iax/xxxx)来连接sip和iax的。对于extensions.conf中的很多内置命令,可以参考http://www.voip-info.org/wiki/view/Asterisk ;或者《Asterisk.The.Future.of.Telephony》、《Building Telephony Systems with Asterisk》、《Asterisk Gateway Interface 1.4 and 1.6 Programming》
如有需要以上的书籍,可以再google上搜索,如果实在下载不到,可以留言给我,有时间的话将Email你们!