Using bgapi from LUA

Status
Not open for further replies.

ewdpb

Member
Oct 3, 2019
151
19
18
Hi all,

I am trying to wrap my head around the use of bgapi. What I am tryin gto is to call a lua script that executes a curl command. While curl does its thing I want to do something else (e.g. playing back some music on hold to the caller). I am trying to do this in pure LUA (no ESL - the only reason is that I did not know ESL existed at all when I started this project and now it is too late to go back).

So, I've been testing with the following script which simply executes another external script which, in turn, is using mod curl. I woud expect some session variables to be set but it does not seem to be the case. I use a for loop just to check every few seconds the existence of the session variable.

My main script:

Code:
session:answer();
api = freeswitch.API();

if(session:ready())then
  reply = api:executeString('bgapi \'my_curl.lua\'');
end

for i=1, 30 do

if(session:getVariable('curl_response_code') == nil)then
    freeswitch.consoleLog('Notice', 'Nothing to see yet')
    session:execute('sleep', 500) 
  else
   freeswitch.consoleLog('Notice','The data is' .. session:getVariable('curl_response_data'))
end

i = i + 1;

end



session:hangup();

My_curl.lua:

Code:
local url = 'http://myapi.com'
session:execute('curl', url);


In the logs I can see something happening:

+OK Job-UUID: fbb932aa-d58b-4e9f-b658-c542eff0640d

But nothing else happens. My main script simply goes through the for loop until the end and session variable curl_response_code never gets set.

Clearly I am probably misunderstanding how this whole thing works. Any hint or guide would be very helpful.

Thanks!
 
Last edited:

KonradSC

Active Member
Mar 10, 2017
166
99
28
If I understand correctly, bgapi kicks off a completely different process that knows nothing of the original process. Try passing the primary calls's uuid to the bgapi as a parameter, then in the child process pass the curl results back to the primary call using the uuid.
 

ewdpb

Member
Oct 3, 2019
151
19
18
Hi @KonradSC, Thanks very much for the reply. Listen, I know it is too much to ask but I am a telecom engineer wanna be script developer. Would you mind throwing a line around how I get those things you suggest done. In particular:

1. How do I pass the primary call's uuid to bgapi as a parameter?
2. How do I pass the curl results back to the primary call using the uuid.

I know, everybody hates those who do not put some effort on their side and want a pre-cook recipe but I spent a whole day yesterday trying to do something similar and I could not find my own tail. If you can simply point me to an example it would really help.

Thanks very much again!
 

KonradSC

Active Member
Mar 10, 2017
166
99
28
This isn't going to be completely detailed but it should get you going.

Call the original script with a parameter.

mainscript.lua $uuid

Then grab the parameter and set it as a variable in mainscript.lua

uuid = argv[1];

Then execute My_curl.lua with a parameter.

cmd_string = "bgapi my_curl.lua " .. uuid api:executeString('bgapi cmd_string'');

Grab the uuid in my_curl.lua
uuid = argv[1];

Pass the result back to the orignal call using uuid_setvar.

api:executeString("uuid_setvar " .. uuid .. " result=1234");

Then look for that new variable in your mainscript.
 

ewdpb

Member
Oct 3, 2019
151
19
18
Awesome man, thanks very much. I'll give it a try and report back on how it goes.
 
Status
Not open for further replies.