Replace characters in Asterisk Dialplan

柔情痞子 提交于 2020-01-01 19:10:09

问题


I want to change a couple off characters * # for A and P to have the monitor filename with characters more friendly. The only solution I could find was to do it my self within the dialplan but it generates a lot of verbosity output and is not as efficient(fast) as I would like to. I'll post it here just in case someone wants to use it. But I'm looking for an asterisk function that I can compile something that I can call withing the dialplan like ${REPLACE(${EXTEN},*,a)} and have the exten **123**456*** converted to AA123AA456AAA.

;
; MACRO REPLACE
;
[macro-replace]
;
;   ${ARG1} - String source
;   ${ARG2} - Chars to replace
;   ${ARG3} - Chars to replace with
;
exten => s,1,NoOp(Replacing ${ARG2} for ${ARG3} in ${ARG1})
exten => s,n,Set(str=${ARG1})
exten => s,n,Set(find=${ARG2})
exten => s,n,Set(replace=${ARG3})
exten => s,n,Set(i=0)
exten => s,n,Set(length=${LEN(${str})})
exten => s,n,While($[${i} < ${length}])
exten => s,n,GotoIf($["${str:${i}:1}" != "${find}"]?continue)
exten => s,n,Set(pre=)
exten => s,n,GotoIf($["${i}" = "0"]?post)
exten => s,n,Set(pre=${str:0:${i}})
exten => s,n(post),Set(post=)
exten => s,n,GotoIf($["${i}" = $[${length} - 1]]?write)
exten => s,n,Set(post=${str:$[${i} + 1]})
exten => s,n(write),Set(str=${pre}${replace}${post})
exten => s,n(continue),Set(i=$[${i} + 1])
exten => s,n,EndWhile
exten => s,n,Set(REPLACERESULT=${str})


回答1:


The REPLACE() function now does this easily:

exten => 100,1,Set(find=**123**456***)
same => n,NoOp(find=${find})
same => n,Set(replace=${REPLACE(find,*,A)})
same => n,NoOp(find=${find}, replace=${replace})
same => n,hangup()

Output:

*CLI> channel originate local/100@default extension null@default
    -- Executing [100@default:1] Set("Local/100@default-c758;2", "find=**123**456***") in new stack
    -- Executing [100@default:2] NoOp("Local/100@default-c758;2", "find=**123**456***") in new stack
    -- Executing [100@default:3] Set("Local/100@default-c758;2", "replace=AA123AA456AAA") in new stack
    -- Executing [100@default:4] NoOp("Local/100@default-c758;2", "find=**123**456***, replace=AA123AA456AAA") in new stack
    -- Executing [100@default:5] Hangup("Local/100@default-c758;2", "") in new stack
  == Spawn extension (default, 100, 5) exited non-zero on 'Local/100@default-c758;2' 



回答2:


That's really the best way to do it (without using regex). If you want to use regex (regular expressions), Asterisk 1.1+ has full support for it. This will allow you to do your entire macro in a single line. The documentation for using regex in dialplan is here: voip-info.

Hopefully this helps! There are plenty of examples on that voip-info page that should be able to help you along!




回答3:


Another alternative to what you've done is to use an AGI script. Just write your code in bash/python/etc and use it as AGI(replace,${arg1},${arg2},${arg3}). Might not be as fast as an internal function but it's more compact and potentially faster than your solution.



来源:https://stackoverflow.com/questions/1924982/replace-characters-in-asterisk-dialplan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!