Module to resubmit a message to a channel

When writing Translator scripts, a useful operation to have on hand is the ability to resubmit a message to an Iguana channel.

In absence of an official API for this capability, I performed a little reverse engineering to use the same web service hook that Iguana’s own user interface uses. Once I figured that out, I wrote a resubmit Lua module around the call. In future versions of Iguana, we may change the API call (and update this module), or we may come up with an official API to do this.

Note: Please let us know when you use undocumented calls like this one. We want to track your needs and communicate with you when changes are made. Communication is always good!

To use the resubmit module effectively, you’ll need to know the unique reference number for the message (or refmsgid). You can get this information by querying the logs. To learn more, see Obtaining a Direct Link to a Logged Message.

The following script shows how to use the resubmit module:

local resubmit = require 'resubmit'

function main(Data)
   -- Caveat emptor - the refid needs to be for a valid message.  I have an admin
   -- user with user = 'kermit' and password = 'kermit'.
   
   if SomeCondition then
      resubmit.resubmit{user='kermit', password='kermit', url='http://localhost:6543/', 
         refid='20130310-14420', message='MSH exploded', channel='My Channel'}
   end
end

To access the web service hook, this module borrows some code from the monitor module to perform an Iguana login session. The login information for the session is specified when the resubmit{} function is called. Since this operation is performed programmatically, I would suggest creating a new user ID specifically for use with the resubmit module. The user only needs the Export Logs permission to complete the resubmit operation. You can also use an existing user with the module if desired; however, people logged into Iguana as this user may find that their session expires at seemingly random times (when really, it’s due to the resubmit module using the same credentials).

Source Code

The source code for the resubmit module can be found below. To start using it, simply paste the code into a new module named ‘resubmit’ and then add the line resubmit = require 'resubmit' near the top of your main module.

local message = {}

local function Split(s, d)        
   local t = {}        
   local i = 0        
   local f        
   local match = '(.-)' .. d .. '()'        
   if string.find(s, d) == nil then        
      return {s}        
   end        
   for sub, j in string.gfind(s, match) do        
      i = i + 1        
      t[i] = sub        
      f = j        
   end        
   if i~= 0 then        
      t[i+1]=string.sub(s,f)        
   end        
   return t        
end

function message.resubmit(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end

   -- We login first
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         auth={username=User, password=Password},live=true})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end

function message.resubmit_with_cookie(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end

   -- We login first
   local Success,Result, _, Headers = pcall(net.http.get,{url=Url..'login.html', 
      parameters={username=User, password=Password },live=true})
   if not Success then
      error(Result, 2)
   end
   -- That gives us our session cookie which we use for our
   -- login credentials.  The login interface was not quite
   -- meant for an API - but the problem is solvable.
   local SessionId = Headers["Set-Cookie"]
   if not SessionId then
      error('Username and/or password wrong',2)
   end
   SessionId = Split(SessionId,' ')[1]
   trace(SessionId)
   
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         headers={Cookie=SessionId}, live=Live})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end

local resubmitStatusHelp = {
       Title="resubmit.resubmit",
       Usage=[[resubmit.resubmit{url=<value>, user=<value>, password=<value>,
   refid=<value>, message=<value>, channel=<value> [, live=<value>]}]],
       Desc=[[This function utilizes an existing Iguana web service API call to resubmit a message to a specific channel.
   Code using this function should expect that this web service call may periodically fail since it's going over the network.]],
       Returns={
          {Desc="The response data from the HTTP request (string)."},
       },
       ParameterTable=true,
       Parameters={
           {url={Desc='The root URL for the target Iguana server. i.e. http://localhost:6543/'}},
           {user={Desc='User name to login with.'}},
           {password={Desc='Password to login with.'}},
           {refid={Desc='Unique log reference ID of the message you are resubmitting.'}},
           {message={Desc='Value of the message to be resubmitted.'}},
           {channel={Desc='Unique name of the channel you are resubmitting to.'}},
           {live={Desc='Resubmit the message while in the editor.', Opt=true}},
       },
       Examples={
           [[local Result = resubmit.resubmit{url='http://localhost:6543/', user='admin', password='password'}
       refid='20130310-14420', message='Some message', channel='Some channel'}]]
       },
       SeeAlso={
           {
               Title="Resubmit module",
               Link="http://wiki.interfaceware.com/1362.html"
           }
       }
   }

help.set{input_function=message.resubmit, help_data=resubmitStatusHelp}
return message

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.