Asterisk/FreePBX Integration with AI for Call Processing Asterisk is an open-source PBX with a vast ecosystem. Integration with AI allows you to add speech recognition and synthesis to your existing telephony infrastructure without replacing the entire platform. A popular choice for on-premise deployments. ### Asterisk + AI Integration Methods AGI (Asterisk Gateway Interface) – a script runs on an incoming call:
# agi_bot.py - AGI скрипт
import sys
from asterisk.agi import AGI
agi = AGI()
agi.answer()
agi.set_variable("CHANNEL(audioreadformat)", "slin16")
# Запись аудио от пользователя
agi.record_file(
"/tmp/user_audio",
"wav",
"#", # stop key
3000, # timeout ms
0, # offset
True, # beep
3 # silence threshold
)
# STT + LLM + TTS в отдельном сервисе
import requests
with open("/tmp/user_audio.wav", "rb") as f:
stt_response = requests.post("http://ai-service/stt", files={"audio": f})
transcript = stt_response.json()["text"]
llm_response = requests.post("http://ai-service/chat",
json={"text": transcript})
response_text = llm_response.json()["response"]
tts_response = requests.post("http://ai-service/tts",
json={"text": response_text})
with open("/tmp/response.wav", "wb") as f:
f.write(tts_response.content)
agi.stream_file("/tmp/response")
```**ARI (Asterisk REST Interface)** is a more powerful approach for real-time:```python
import asyncio
import aiohttp
from ari_client import ARIClient
async def handle_stasis(channel_id: str, ari: ARIClient):
"""Обработчик входящего звонка через ARI"""
await ari.answer(channel_id)
# Создаём снэпшот аудио канала
await ari.channel.record(
channelId=channel_id,
name=f"call_{channel_id}",
format="wav",
terminateOn="silence",
maxSilenceSeconds=2
)
```### Asterisk dialplan```ini
; extensions.conf
[ai-bot-context]
exten => _X.,1,NoOp(Входящий звонок)
same => n,Answer()
same => n,AGI(agi://127.0.0.1/ai_bot)
same => n,Hangup()
```### FreePBX IVR via AGI FreePBX provides a web interface for configuration, and AGI adds AI logic:```python
# Заменяем стандартные IVR-пункты на AI-распознавание
# вместо "нажмите 1" → "скажите что хотите"
```Timeframe: AGI integration with Asterisk — 2–3 weeks. Full ARI-based implementation — 1–1.5 months.