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 版

第 19 章 Asterisk REST 接口

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

自以为无所不知的人让我们这些知道的人感到非常烦恼。

艾萨克-阿西莫夫

Asterisk REST 接口(ARI )的创建是为了解决在 Asterisk 外部开发外部功能或增强功能时固有的局限性。虽然 AGI 允许用户触发外部应用程序,AMI 允许用户从外部监督和控制正在进行的呼叫,但任何将两者集成到一个完整的外部应用程序的尝试都会很快变得复杂和笨拙。ARI 允许开发人员使用 Asterisk 作为底层引擎,构建独立、完整的应用程序。

截至本文撰写时,ARI 需要一个非常基本的拨号计划才能触发Stasis() 拨号计划应用程序,然后将通道交给 ARI。当您读到这篇文章时,这一要求很可能已经改变,因为 Asterisk 开发人员社区一直在积极努力,使 ARI 能够在中间不使用任何拨号计划的情况下启动。

使用 ARI 等外部接口来控制 Asterisk 不一定会让你的生活变得更轻松。实施这类应用程序并排除故障所需的技能不仅需要你所选择的语言,还需要Linux系统管理、Asterisk管理、网络故障排除和基本电话概念等方面的综合技能。对于熟练的开发人员来说,ARI 能为您的应用程序提供所需的功能,但对于正在学习的人员来说,我们建议您在进入外部开发环境之前考虑掌握拨号计划。拨号计划很特别,但它也是完全集成、高性能和相对易学的。

说了这么多,让我们开始使用 ARI 吧。

ARI 快速入门

本节为您提供了一个简单的 ARI 工作示例 。在本章的后面部分,我们将介绍更详细的内容。1

警告

在本快速入门章节中,我们将使用一个非常简单的 HTTP 访问层。将这种配置投入生产必须非常谨慎。例如,如果你要在一台单独的机器上运行你的应用程序,并通过套接字与 Asterisk 连接,你就需要一个更安全的连接。我们在本节中所做的,就好比帆船俱乐部使用小艇进行教学;作为入门很有用,但驾驶这种小艇出海则既愚蠢又危险。

Asterisk 基本配置

你应该已经运行了 Asterisk 网络服务器, ,因此只需确认你的/etc/asterisk/http.conf文件与下面的文件相似即可:

[general]
enabled = yes
bindaddr = 127.0.0.1

接下来,需要一个简单的/etc/asterisk/ari.conf文件:

[general]
enabled = yes
pretty = yes
[asterisk]
type = user
read_only = no
password = whateveryoudodontusethispassword

好了,现在让我们加载ari 模块:

$ sudo asterisk -rx 'module load res_ari.so'
Loaded res_ari.so => (Asterisk RESTful Interface)

然后,在/etc/asterisk/extensions.conf文件中,我们需要一个扩展名来触发Stasis() dialplan 应用程序:2

exten => 242,1,Noop()
  same => n,Stasis(zarniwoop)
  same => n,Hangup()

用以下命令重新加载拨号计划

$ sudo asterisk -rx 'dialplan reload'
Dialplan reloaded.

此时,不妨重新加载 Asterisk:

$ sudo ...
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