# Asterisk Project : Lua Dialplan Examples

Added by mnicholson , edited by mnicholson on Jul 21, 2011

Some example `extensions.lua` files can be found below. They demonstrate various ways to organize extensions.

## Less Clutter

Instead of defining every extension inline, you can use this method to create a neater `extensions.lua` file. Since the extensions table and each context are both normal lua tables, you can treat them as such and build them piece by piece.

extensions.lua

    -- this function serves as an extension function directly
    function call_user(c, user)
       app.dial("SIP/" .. user, 60)
    end

    -- this function returns an extension function
    function call_sales_queue(queue)
      return function(c, e)
          app.queue(queue)
       end
    end

    e = {}

    e.default = {}
    e.default.include = {"users", "sales"}

    e.users = {}
    e.users["100"] = call_user
    e.users["101"] = call_user

    e.sales = {}
    e.sales["5000"] = call_sales_queue("sales1")
    e.sales["6000"] = call_sales_queue("sales2")

    extensions = e

## Less Clutter v2

In this example, we use a fancy function to register extensions.

extensions.lua

    function register(context, extension, func)
       if not extensions then
          extensions = {}
       end

       if not extensions[context] then
          extensions[context] = {}
       end

       extensions[context][extension] = func
    end

    function include(context, included_context)
       if not extensions then
          extensions = {}
       end

       if not extensions[context] then
          extensions[context] = {}
       end

       if not extensions[context].include then
          extensions[context].include = {}
       end

       table.insert(extensions[context].include, included_context)
    end

    -- this function serves as an extension function directly
    function call_user(c, user)
       app.dial("SIP/" .. user, 60)
    end

    -- this function returns an extension function
    function call_sales_queue(queue)
      return function(c, e)
          app.queue(queue)
       end
    end

    include("default", "users")
    include("default", "sales")

    register("users", "100", call_user)
    register("users", "101", call_user)

    register("sales", "5000", call_sales_queue("sales1"))
    register("sales", "6000", call_sales_queue("sales2"))
    register("sales", "7000", function()
       app.queue("sales3")
    end)

## Comments:

This page ( Examples ) could cause issues with the namespace. I would suggest "Lua Examples" or "Examples of Lua usage". The same could be said for Dialplan Hints, Advanced Topics, Tips and Tricks, and Interacting with Asterisk (apps, variables, and functions). Posted by lathama at Jul 02, 2011 10:26
We can rename the page if that becomes an issue. Posted by mnicholson at Jul 05, 2011 07:35
OK, I have dealt with namespace issues on larger projects. It becomes a problem fast. Posted by lathama at Jul 05, 2011 08:08