Skip to Content
《Asterisk:权威指南》第 5 版
book

《Asterisk:权威指南》第 5 版

by Jim Van Meggelen, Russell Bryant, Leif Madsen
May 2025
Intermediate to advanced
414 pages
5h 2m
Chinese
O'Reilly Media, Inc.
Content preview from 《Asterisk:权威指南》第 5 版

第 10 章 深入对话计划 深入了解对话计划

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

如需了解技术未能改善生活质量的所有方式,请按三。

爱丽丝-卡恩

好了。你已经掌握了拨号计划的基本内容,但你知道后面还有更多内容。如果你还没弄清第 6 章的内容,请回头再读一遍。我们即将进入更高级的主题。

表达式和变量操作

在我们开始深入研究拨号计划时,现在是时候向您介绍一些工具了,它们将大大增强您在拨号计划中的能力。这些结构可以使您的拨号计划根据您定义的不同标准做出决策,从而为您的拨号计划增添令人难以置信的智能。戴上你的思考帽,让我们开始吧。

备注

在本章中,我们将使用多年来在拨号计划创建过程中形成的最佳 实践。您会注意到,所有第一优先级都以NoOp()应用程序开头(简单地说就是无操作;不会发生任何功能性操作)。另一个是所有后续行都以same => n 开头,这是一种快捷方式,表示 "使用与之前定义的相同扩展名"。此外,缩进为两个空格。

基本表达式

表达式 是变量、运算符和数值的组合,将它们串起来可以产生一个结果。表达式可以测试值、更改字符串或执行数学计算。比方说,我们有一个名为COUNT 的变量。用简单的英语来说,使用该变量的两个表达式可能是 [COUNT 加 1] 或[COUNT 除以 2]。每个表达式都有一个特定的结果或值,这取决于给定变量的值。

在 Asterisk 中,表达式总是以美元符号和开头的方括号开始,以结尾的方括号结束,如图所示:

$[expression]

因此,我们可以这样写我们的两个例子:

$[${COUNT} + 1]
$[${COUNT} / 2]

当 Asterisk 在拨号计划中遇到表达式时,它会用结果值替换整个表达式。需要注意的是,这发生在变量替换之后。让我们看一下下面的代码来演示一下:1

exten => 321,1,NoOp()
    same => n,Answer()
    same => n,Set(COUNT=3)
    same => n,Set(NEWCOUNT=$[${COUNT} + 1])
    same => n,SayNumber(${NEWCOUNT})

在第二个优先级中,我们将3 的值赋值给名为COUNT 的变量。

在第三个优先事项中,只涉及一个应用程序Set(),但实际发生了三件事:

  1. Asterisk 在表达式中用数字3 代替${COUNT} 。表达式实际上变成了这样

        same => n,Set(NEWCOUNT=$[3 + 1])
  2. Asterisk 对表达式进行求值,将1 加入3 ,并用计算值4 取而代之:

        same => n,Set(NEWCOUNT=4)
  3. Set() 应用程序会将4 的值赋给NEWCOUNT 变量。

第三个优先级只需调用SayNumber() 程序,该程序会说出变量${NEWCOUNT} 的当前值(在第二个优先级中设置为4 )。

在自己的拨号计划中试试吧。

操作员

当 您创建 Asterisk dialplan 时,您实际上是在用专门的脚本语言编写代码。这意味着,Asterisk拨号计划与任何编程语言 一样,可以识别称为运算符的符号 ,这些符号可以让你操作变量。让我们来看看 Asterisk 中可用的运算符类型:

布尔运算符

这些运算符 评估语句的 "真假"。在计算机术语中,这主要是指语句是 "有 "还是 "无"(非零或零、真或假、开或关等)。布尔运算符有

expr1 | expr2

这个运算符(称为 "或 "运算符,或 "管道 "运算符)如果为真(既不是空字符串,也不是零),则返回 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Brick-and-Mortar Retailers Get Right

What Successful Brick-and-Mortar Retailers Get Right

Rob Angell
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
Three Essentials for Agentic AI Security

Three Essentials for Agentic AI Security

Paolo Dal Cin, Daniel Kendzior, Yusof Seedat, Renato Marinho

Publisher Resources

ISBN: 9798341659568