From f0f29b3b0ab81f90a181cefc98d2f1d6ec80ab9b Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 23 Nov 2021 13:36:15 +0800 Subject: [PATCH] variable length number --- extensions.conf | 84 ++++++++++++++++++++++++++----------------------- pjsip.conf | 1 + whois.py | 21 +++++++------ 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/extensions.conf b/extensions.conf index 5f73e1b..bb88549 100644 --- a/extensions.conf +++ b/extensions.conf @@ -4,25 +4,28 @@ writeprotect=no clearglobalvars=no [extdn42whois] -exten => _X.,5,Set(CALLNUM=${EXTEN}) - same => n,Goto(s,1) +exten => _X!,200,Set(CALLNUM=${EXTEN}) + same => n,Goto(s,1) exten => s,1,Playback(silence/1) same => n,Set(USERINPUT=) same => n,Read(USERINPUT,jerry-whois,10,,1,6) - same => n,GotoIf($["${READSTATUS}" = "TIMEOUT"]?s,1:) - same => n,GotoIf($["${USERINPUT}" = ""]?extmymenu,${CALLNUM},5:whois,1) + same => n,GotoIf($["${READSTATUS}" = "TIMEOUT" & "${USERINPUT}" = ""]?s,1:) + same => n,GotoIf($["${USERINPUT}" = ""]?extmymenu,${CALLNUM},200:whois,1) -exten => whois,1,Set(SESSIONUNID=${RAND(0,100000)}) - same => n,TrySystem(/var/lib/asterisk/scripts/jerry/whois.py "${USERINPUT}" "${SESSIONUNID}") - same => n,Playback(/var/tmp/ast-dynamic/${SESSIONUNID}) +exten => whois,1,NoOp() + same => n,TrySystem(/var/lib/asterisk/scripts/jerry/whois.py "${USERINPUT}" "${UNIQUEID}") + same => n,Playback(/var/tmp/ast-dynamic/${UNIQUEID}) same => n,GotoIf($["${PLAYBACKSTATUS}" = "SUCCESS"]?whois,whoisend:) same => n,Playback(im-sorry&something-terribly-wrong) same => n(whoisend),Goto(s,1) [extmymenu] -exten => _X.,5,Set(CALLNUM=${EXTEN}) - same => n,Goto(s,1) +exten => _X!,200,Set(CALLNUM=${EXTEN}) + same => n,Goto(s,1) + +exten => _X!,210,Set(CALLNUM=${EXTEN}) + same => n,Goto(3,1) exten => s,1,Wait(1) same => n(loop),Background(jerry-intro) @@ -40,7 +43,7 @@ exten => _X,1,NoOp() same => n,Goto(s,loop) exten => 1,100,NoOp() - same => n,Goto(extdn42whois,${CALLNUM},5) + same => n,Goto(extdn42whois,${CALLNUM},200) same => n,Return() exten => 2,100,NoOp() @@ -51,7 +54,7 @@ exten => 3,100,NoOp() same => n,Playback(your&number&is) same => n,SayAlpha(${CALLERID(num)}) same => n,Playback(silence/1&calling) - same => n,SayAlpha(${CALLNUM}) + same => n,SayAlpha(42403618${CALLNUM}) same => n,Return() exten => 4,100,NoOp() @@ -73,41 +76,42 @@ exten => i,1,NoOp() exten => chanunavail,1,Playback(im-sorry&number-not-answering&please-try-call-later) same => n,Hangup() -exten => 424036180001,5,Dial(PJSIP/REDACTED,300,m) -exten => 424036180002,5,Dial(PJSIP/REDACTED,300,m) -exten => 424036180003,5,Dial(PJSIP/REDACTED,300,m) -exten => 424036180004,5,Dial(PJSIP/REDACTED,300,m) -exten => 424036180005,5,Dial(PJSIP/REDACTED,300,m) -exten => _X.,6,GotoIf($["${DIALSTATUS}" = "CHANUNAVAIL"]?chanunavail,1:) -exten => 424036180000,5,Goto(extmymenu,${EXTEN},5) -exten => 424036183618,5,Playback(silence/1&your&number&is) - same => n,SayAlpha(${CALLERID(num)}) - same => n,Playback(silence/1) -exten => 424036184242,5,Goto(extdn42whois,${EXTEN},5) -exten => _42403618XXXX,5,Playback(im-sorry&check-number-dial-again) +exten => invalidinput,1,Playback(im-sorry&cannot-complete-as-dialed) + same => n,Hangup() + +exten => 0001,5,Dial(PJSIP/REDACTED,300,m) +exten => 0002,5,Dial(PJSIP/REDACTED,300,m) +exten => 0003,5,Dial(PJSIP/REDACTED,300,m) +exten => 0004,5,Dial(PJSIP/REDACTED,300,m) +exten => 0005,5,Dial(PJSIP/REDACTED,300,m) +exten => 0000,5,Goto(extmymenu,${EXTEN},200) +exten => 0 ,5,Goto(extmymenu,${EXTEN},200) +exten => 3618,5,Goto(extmymenu,${EXTEN},210) +exten => 4242,5,Goto(extdn42whois,${EXTEN},200) +exten => _X! ,5,Playback(im-sorry&check-number-dial-again) +exten => _X! ,6,GotoIf($["${DIALSTATUS}" = "CHANUNAVAIL"]?chanunavail,1:) [extpeers] -exten => _42403618XXXX,5,Answer() - same => n,Goto(extmyself,${EXTEN},5) +exten => _42403618.,5,Answer() + same => n,GotoIf($["${EXTEN:8}" : ".*[^0-9]+.*"]?extmyself,invalidinput,1:) + same => n,Goto(extmyself,${EXTEN:8},5) -exten => _42401332XXXX,5,NoOp() - same => n,Dial(PJSIP/${EXTEN}@nia) +exten => _42401332.,5,NoOp() + same => n,Dial(PJSIP/${EXTEN}@nia) -exten => _42403315XXXX,5,NoOp() - same => n,Dial(PJSIP/${EXTEN}@zane) +exten => _42403315.,5,NoOp() + same => n,Dial(PJSIP/${EXTEN}@zane) -exten => _42403088XXXX,5,NoOp() - same => n,Dial(PJSIP/${EXTEN}@sunnet) +exten => _42403088.,5,NoOp() + same => n,Dial(PJSIP/${EXTEN}@sunnet) -exten => _42401353XXXX,5,NoOp() - same => n,Dial(PJSIP/${EXTEN}@hertz) +exten => _42401353.,5,NoOp() + same => n,Dial(PJSIP/${EXTEN}@hertz) -exten => _42400119XXXX,5,NoOp() - same => n,Dial(PJSIP/${EXTEN}@jrb0001) +exten => _42400119.,5,NoOp() + same => n,Dial(PJSIP/${EXTEN}@jrb0001) [jerry] -exten => _XXXX,1,Goto(42403618${EXTEN},1) -exten => _XXXXXXXX,1,Goto(4240${EXTEN},1) -exten => _X.,1,NoOp() - -exten => _X.,2,Goto(extpeers,${EXTEN},5) +exten => _4240XXXX.,1,Goto(extpeers,${EXTEN},5) +exten => _0XXXX. ,1,Goto(extpeers,424${EXTEN},5) +exten => _X! ,1,Goto(extpeers,42403618${EXTEN},5) diff --git a/pjsip.conf b/pjsip.conf index 520502d..7c5693f 100644 --- a/pjsip.conf +++ b/pjsip.conf @@ -136,6 +136,7 @@ match=172.20.29.73 [jrb0001](endpoint-template) aors = jrb0001 identify_by=ip +allow=!all,opus48,opus,ulaw,speex,alaw [jrb0001](aor-template) contact=sip:[fd42:5d71:219:1003:216:3eff:fe9d:882f]:5060 [jrb0001] diff --git a/whois.py b/whois.py index 336125a..192e9ad 100644 --- a/whois.py +++ b/whois.py @@ -15,8 +15,11 @@ ROOTDIR = Path("/var/tmp") OUTDIR = ROOTDIR / "ast-dynamic" OUTDIR.mkdir(exist_ok=True) -def cleanup(): +def cleanup(*files): try: + for f in files: + if f.exists(): + f.unlink() now = time() for f in OUTDIR.iterdir(): if f.is_file() and now - f.stat().st_mtime > 60.0: @@ -24,7 +27,7 @@ def cleanup(): except Exception: raise -def espeak(to_speak, unid): +def espeak(to_speak, outfile): p1 = subprocess.run( ["espeak-ng", "-v", "en-us", "--stdin", "-p", "80", "--stdout"], input=str(to_speak).encode("utf-8"), @@ -32,10 +35,8 @@ def espeak(to_speak, unid): check=True, timeout=10 ) - unfile = f"{unid}.wav" p2 = subprocess.run(['ffmpeg', '-hide_banner', '-i', "-", - '-vn', "-ac", "1", "-ar", "8000", "-y", - f"{OUTDIR / unfile}"], + "-vn", "-ac", "1", "-ar", "8000", f"{outfile}"], input=p1.stdout, capture_output=True, check=True, @@ -123,15 +124,17 @@ def whois(arg): def main(): # argv[1]: as number # argv[2]: unique identifier - cleanup() + unid = argv[2] + outfile = OUTDIR / f"{unid}.wav" + errfile = OUTDIR / f"{unid}{ERRSOUND.suffix}" + cleanup(outfile, errfile) try: ret = whois(argv[1]) except Exception as e: ret = f"error, {e}" try: - espeak(ret, argv[2]) + espeak(ret, outfile) except Exception: - outpath = OUTDIR / f"{argv[2]}{ERRSOUND.suffix}" - shutil.copy(ERRSOUND, outpath) + shutil.copy(ERRSOUND, errfile) if __name__ == "__main__": main()