You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ari-client(Node.js)实现Asterisk分机100转分机200?

实现方案与可行性说明

可行性结论

通过ari-client(Node.js)实现该转接需求完全可行,核心是要捕获当前活跃的呼叫通道,再对其执行转接操作,而非直接发起新呼叫。

关键配置与代码调整

1. 调整Asterisk拨号计划(extension.conf)

确保初始分机100的呼叫能进入ARI应用,修改或添加对应路由:

[from-external]
; 处理来自Twilio的入站呼叫(对应拨打100的路由)
exten = 100,1,Answer()
same = n,Stasis(your-ari-app-name)  ; 应用名必须与ariConfig.appName一致
same = n,Hangup()

[from-internal]
; 保留你原有的200分机配置
exten = 200,1,Answer()
same = n,Wait(1)
same = n,Playback(hello-world)
same = n,Hangup()

2. Node.js ari-client 代码实现

两种主流转接方式任选其一:

方式一:直接重定向现有呼叫到分机200

这种方式简洁高效,直接将当前呼叫路由到200分机的拨号计划:

const ari = require('ari-client');

const ariConfig = {
  url: 'http://你的Asterisk服务器IP:8088',
  username: 'ARI用户名',
  password: 'ARI密码',
  appName: 'your-ari-app-name' // 与extension.conf中的Stasis应用名一致
};

ari.connect(ariConfig.url, ariConfig.username, ariConfig.password)
  .then(client => {
    // 监听呼叫进入ARI应用的事件
    client.on('StasisStart', async (event, incomingChannel) => {
      await incomingChannel.answer();
      
      // 执行转接:将当前呼叫重定向到from-internal上下文的200分机
      await incomingChannel.redirect({
        context: 'from-internal',
        extension: '200',
        priority: 1
      });
    });

    // 启动ARI应用
    client.start(ariConfig.appName);
  })
  .catch(err => console.error('ARI连接失败:', err));

方式二:发起新呼叫到200并桥接通道

适合需要在转接前后添加额外逻辑(如等待接通提示)的场景:

const ari = require('ari-client');

const ariConfig = {
  url: 'http://你的Asterisk服务器IP:8088',
  username: 'ARI用户名',
  password: 'ARI密码',
  appName: 'your-ari-app-name'
};

ari.connect(ariConfig.url, ariConfig.username, ariConfig.password)
  .then(client => {
    client.on('StasisStart', async (event, incomingChannel) => {
      await incomingChannel.answer();

      // 发起对200分机的呼叫(endpoint替换为你200分机的实际PJSIP端点名称)
      const targetChannel = await client.channels.originate({
        endpoint: 'PJSIP/200',
        app: ariConfig.appName,
        context: 'from-internal',
        extension: '200'
      });

      // 创建混音桥并将两个通道加入桥接
      const bridge = await client.bridges.create({ type: 'mixing' });
      await bridge.addChannel({ channel: incomingChannel.id });
      await bridge.addChannel({ channel: targetChannel.id });
    });

    client.start(ariConfig.appName);
  })
  .catch(err => console.error('ARI连接失败:', err));

3. 额外注意事项

  • 确保Asterisk的ari.conf中已配置正确的用户权限,允许该应用访问通道、桥接等资源。
  • 若使用方式二,需保证200分机的PJSIP端点已正确配置,Asterisk能正常呼叫到该分机。
  • 你原代码中endpoint: "PJSIP/twilio0"是错误配置,需替换为目标分机的端点或使用方式一的重定向逻辑。

内容的提问来源于stack exchange,提问作者Anas Lakhany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 18:46:11