Lua script for call routing

Dast

Member
Nov 11, 2019
67
11
8
Hi all,

I am writing a lua script that I'll be using with dialplan tools. The script is simple and makes a curl request, if a response is provided then I set some session variables, to be used later.

Observing the console it seems my session variables are set fine from within the lua script, however the condition in group 2 that checks one of those variables never evals as true, even though it should.
Any ideas where I'm going wrong?

1749977878384.png
 
Straight copy paste from copilot (I don't know how people operated before this, can't even remember the last time I visited Stack Overvflow, let alone a printed book!):

Ah, that’s a classic FreeSWITCH Lua gotcha. If your script sets a variable using session:setVariable("foo", "bar") but it’s not accessible later in the dialplan or in subsequent scripts, here are a few things to check:

  1. Scope of the session: Make sure you're modifying the same session object that the dialplan is using. If you're calling another Lua script via session:execute("lua", "script.lua"), that spawns a new interpreter context, and the session variables may not persist across that boundary.
  2. Timing of variable access: If you're trying to access the variable in the dialplan after the Lua script runs, ensure that the Lua script completes before the dialplan continues. This is true for <action application="lua" .../>, which is synchronous, but not for luarun.
  3. Variable visibility: Some variables set in Lua may not be visible to the dialplan unless explicitly exported. You can do this in Lua with:
    1. lua
      session:execute("export", "foo=bar")
      or
    2. lua
      session:setVariable("foo", "bar")
      session:execute("export", "foo")
      This ensures the variable is available in the channel’s exported variable space.
  4. Dialplan caching: If you're using static XML dialplans, FreeSWITCH might cache the dialplan before the Lua script runs. In that case, any variables set during the Lua execution won’t affect the already-parsed dialplan logic.
 
Last edited: