# Record call using EAGI and Python

**URL:** <https://community.asterisk.org/t/record-call-using-eagi-and-python/88106>\
**Category:** Asterisk APIs\
**Created:** [March 28, 2021, 7:27pm UTC](https://community.asterisk.org/t/record-call-using-eagi-and-python/88106 "2021-03-28T19:27:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Exodus](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Exodus](https://community.asterisk.org/u/Exodus)\
**Post date:** [March 28, 2021, 7:27pm UTC](https://community.asterisk.org/t/record-call-using-eagi-and-python/88106/1 "2021-03-28T19:27:02Z")

</div>

Hello everyone,

I’m currently working on a school project where I need to pass the audio stream from an asterisk call to an external script for analysis and kinda automate the call based on what is said during the call.

I made some researches and found that the audio stream can be accessed thanks to Asterisk EAGI but I can’t find any example or clear explanations on how to do it. I have already set up my Asterisk environment and I’m able to execute my python script as an EAGI script and do some basic operations.  
This is what my EAGI script looks like right now :

_#!/usr/bin/python_  
_import sys_  
\*from asterisk.agi import \*\*

_agi = AGI()_  
_agi.verbose(“Python AGI started successfully”)_  
_callerId = agi.env[‘agi\_callerid’]_  
_agi.verbose(“Call from %s” % callerId)_  
_agi.say\_number(3)_  
_agi.hangup()_

I read that I need to use File Descriptor 3 to get the incoming audio stream and I would like to write the audio stream to an actual audio file on my computer. I would like to save the calls to train a model. The objective is to later have an EAGI script that records the call for like 10sec → analyze the audio → take a decision → record → etc.

And before I forget, I’m using the Pyst2 library.

So any help on how can I do this will be reallyd appreciated 🙂  
Thanks to anyone who will read this post !

---

<div class="post-metadata">

**Author:** ![penguinpbx](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/penguinpbx/32/8856_2.png) [@penguinpbx](https://community.asterisk.org/u/penguinpbx)\
**Post date:** [April 23, 2021, 3:59pm UTC](https://community.asterisk.org/t/record-call-using-eagi-and-python/88106/2 "2021-04-23T15:59:22Z")

</div>

Did you try the built-in Asterisk dial plan [Record()](https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_Record) application ?

That app would let you record to a file. Then in the next line of dial plan in your _extensions.conf_ file, invoke your [AGI()](https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_AGI) to process the recorded file. And in the line of dial plan after that, branch around to another context/extension/priority, based on the results of the AGI, using [GosubIf()](https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_GosubIf), [GotoIf()](https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_GotoIf), etc.

This lets you decouple the solution – separating between recording, processing, and branching – which may help you with testing and training of your model (sounds like that will be where most of your work is anyhow.)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/asterisk/original/2X/4/475d0c889911cfb84ac4881ad6df8b523e3f9105.png) [@system](https://community.asterisk.org/u/system)\
**Post date:** [May 23, 2021, 3:59pm UTC](https://community.asterisk.org/t/record-call-using-eagi-and-python/88106/3 "2021-05-23T15:59:40Z")

</div>

Closed on May 23, 2021, 3:59pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
