Skip to main content

Asterisk AGI

  • AGI - Asterisk Gateway Interface - CGI
  • Async AGI
    • Asterisk 1.6+
    • 异步 CGI 脚本 - 相对使用少 - 可以使用 AMI、ARI、WebSocket 实现
    • agi:async
  • FastAGI
    • tcp 交互
    • 远程脚本
    • 默认端口 4573
    • agi://host.domain[:port][/script/name]
    • 支持 HA - DNS Lookup - 高可用
      • 前缀 _agi._tcp.
      • hagi://agi.example.com/foo.agi -> _agi._tcp.agi.example.com
  • agi-bin
    • exec 执行 - 基于 io 交互
    • eagi 支持音频通道 - 不可以运行在挂断的通道
    • 例如 AGI(test.bash) -> /var/lib/asterisk/agi-bin/test.bash
  • 参考

extensions.conf

[default]
exten => 1000,1,AGI(agi://localhost:3000)
# 调用 PHP 处理
exten => 1,1,AGI(myApplication.php)

Notes

  • AGI 通过 stdin stdout 交互
  • AMI AGI 事件
    • AGIExecStart
    • AGIExecEnd
    • AsyncAGIStart
    • AsyncAGIExec
    • AsyncAGIEnd

环境变量传递配置

envdesc
AST_CONFIG_DIRastetcdir
AST_CONFIG_FILEasterisk.conf
AST_MODULE_DIRastmoddir
AST_SPOOL_DIRastspooldir
AST_MONITOR_DIR默认 AST_SPOOL_DIR/monitor
AST_VAR_DIRastvarlibdir
AST_DATA_DIRastdbdir
AST_LOG_DIRastlogdir
AST_AGI_DIRastagidir
AST_KEY_DIRastkeydir
AST_RUN_DIRastrundir
  • 传递 asterisk.conf 配置的目录参数

变量传递上下文信息

vardemodesc
agi_requesttest.bashfilename of script
agi_channelSIP/6001-00000004originating channel
agi_languageenlanguage code
agi_typeSIPoriginating channel type
agi_uniqueid1621099875.4unique ID for call
agi_version18.2.1version of Asterisk (since Asterisk 1.6)
agi_calleridunknowncaller ID number (or "unknown").
agi_calleridnameunknowncaller ID name (or "unknown").
agi_callingpres1presentation of callerid.
agi_callingani20PRI Channels ani2 variable.
agi_callington0The type of number used in PRI Channels.
agi_callingtns0An optional 4 digit number (Transit Network Selector).
agi_dnidunknownThe dialed number id (or "unknown").
agi_rdnisunknownThe referring DNIS number (or "unknown").
agi_contexttestOrigin context in extensions.conf.
agi_extension1The called number (dnis).
agi_priority1The priority it was executed as in the dial plan.
agi_enhanced0.0The flag value is 1.0 if started as an EAGI script, 0.0 otherwise.
agi_accountcodeAccount code of the origin channel.
agi_threadid140092630592288Thread ID of the AGI script.

变量格式

  • agi_arg_<N> 为 AGI 参数
    • 例如 AGI(/tmp/agi.sh,arg1,arg2)
      • agi_arg_1: arg1
      • agi_arg_2: arg2
# 接收变量
<variable_name>:<space><variable_value>
agi_context: default
agi_arg_1: abc

# FastAGI 脚本参数
# AGI(agi://192.168.0.2/CallerWantsCustomerService,${EXTEN},${UNIQUEID},${CALLERID(name)})
agi_network: yes
agi_network_script: CallerWantsCustomerService

# 发送命令
VERBOSE "message" 3
# 接收响应
<error_code><space>result=<result_data><space>[additional_data]
200 result=1
respdesc
error_code
result_data
additional_dataresult_data 之外的额外数据
error_codedesc
200Operation was completed successfully.
510Invalid or unknown command.
511The command cant be executed on a dead (closed, terminated, hung up) channel.
520End of proper usage, when the command returns its syntax.
  • AGISTATUS
    • SUCCESS
    • FAILURE
    • NOTFOUND
    • HANGUP

res_agi.c

  • res/res_agi.c
  • run_agi - 实际处理流程
    • agi_handle_command - 处理读取命令,返回 cmd_status
      • find_command 匹配命令
  • setup_env - 发送变量信息
  • agi_command - 注册的命令
    • cmda - 命令 - 例如 {"get","data"}
    • int (* const handler)(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
    • 每个命令内部自行解析参数