[Freeswitch-users] Run a LUA script when voicemail received

Steven Schoch schoch+freeswitch.org at xwin32.com
Sat May 28 02:51:08 MSD 2016

I got it working. Ignore the rest of this message unless you want to know
how I solved it.
My problem was that I was using "reloadxml" after I added the "hook" line,
but after reading the mod_lua.cpp file, I found that while "reloadxml"
loaded the whole XML include tree into the Freeswitch config structure, The
mod_lua code used that config structure only on startup. The solution was
to shut down freeswitch. (And thanks to the new  systemd on Debian 8, it
started back up by itself.)


It didn't work.I tried putting this line in autoload_configs/lua.conf.xml:

    <hook event="CUSTOM" subclass="vm::maintenance"

I then created a "voicemail-event.lua" file with the following:
action = event:getHeader("VM-Action")
freeswitch.consoleLog("info", "VM Action: " .. action)

However, the log file has nothing about this script:

91307edb-28d3-4d1d-bd80-95654a327c8b 2016-05-27 15:07:47.533629 [DEBUG]
switch_core_state_machine.c:710 (sofia/external/+1408xxxxxxx at fl.gg) State
DESTROY going to sleep
2016-05-27 15:07:47.533629 [DEBUG] mod_voicemail.c:1946 Update MWI:
Messages Waiting yes
2016-05-27 15:07:47.533629 [DEBUG] mod_voicemail.c:1947 Update MWI: Update
Reason NEW
2016-05-27 15:07:47.533629 [DEBUG] mod_voicemail.c:1948 Update MWI: Message
Account 408xxxxxxx at starnet.com
2016-05-27 15:07:47.533629 [DEBUG] mod_voicemail.c:1949 Update MWI: Voice
Message 1/0
2016-05-27 15:07:47.953635 [DEBUG] switch_utils.c:1180 Emailed file
[/tmp/mail.14643868674630] to [voicemail at xxxx.com]
2016-05-27 15:07:47.953635 [DEBUG] mod_voicemail.c:3038 Sending message to
voicemail at xxxx.com
2016-05-27 15:07:48.393632 [DEBUG] switch_utils.c:1182 Emailed data to
2016-05-27 15:07:48.393632 [DEBUG] mod_voicemail.c:3088 Sending notify
message to vm-page

The deliver_vm() function in
src/mod/applications/mod_voicemail/mod_voicemail.c sends the event before
the MWI update. It also sends the event at other times, including after
voicemail authentication, but I don't see that it the log either.

Investigating a little further, in src/mod/languages/mod_lua/mod_lua.cpp,
there is a function that seems to process the autoload_configs/lua.conf.xml
file. It has a loop that appears to process the "hook" elements that
includes this line:

SWITCH_LOG_INFO, "hook params: '%s' | '%s' | '%s'\n", event, subclass,


On Mon, May 23, 2016 at 10:20 AM, Steven Schoch <
schoch+freeswitch.org at xwin32.com> wrote:

> Thanks! Another thing to point me in the right direction. Investigating
> the source, I found in the routine deliver_vm(), it makes these calls:
>                 switch_event_create_subclass(&message_event,
>                 switch_event_add_header_string(message_event,
> SWITCH_STACK_BOTTOM, "VM-Action", "leave-message");
> ...
>                 switch_event_fire(&message_event);
> This led me to the example autoload_configs/lua.conf.xml file, which has
> this line:
>     <!--<hook event="CUSTOM" subclass="conference::maintenance"
> script="catch-event.lua"/>-->
> I should be able to write a script that gets called whenever something
> happens to a voicemail box with this line:
>     <hook event="CUSTOM" subclass="vm::maintenance"
> script="voicemail-event.lua"/>
> Then, in my lua script, I should be able to call
> event:getHeader("VM-Action"), and exit unless it's "leave-message". There
> are other headers in the event that should have information such as the
> length of the recording.
> Just a little confused about the "event" variable. Is that a global
> variable that's already set when my script is called?
> --
> Steve
> On Sun, May 22, 2016 at 9:34 AM, Abaci B <abaci64 at gmail.com> wrote:
>> https://wiki.freeswitch.org/wiki/Mod_lua#Event_Hooks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20160527/2f07598e/attachment.html 

Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users mailing list