AnthonyMusgrove / Emby-ScripterX

Run custom external application or script (batch/bash/powershell/php/python/node.js/custom) on various events within the Emby Server.
https://emby-scripterx.com
76 stars 6 forks source link

Provide package js access to full token object #35

Closed Slugger closed 4 years ago

Slugger commented 4 years ago

Is your feature request related to a problem? Please describe. In my package event handler, I'll be doing a lot of posting to web hooks in my app. It would be ideal if I could just ask the context for an object of all of the tokens for the event instead of manually building an object for posting. Something like:

function onScheduledTask(ctx) {
   ScripterX.Log.Info('Starting scheduled task');
   var eventDetails = ctx.TokenObject; // this returns an object containing all available tokens for the current event
   ScripterX.Web.Post('http://192.168.1.2/event', JSON.stringify(eventDetails));
   ScripterX.Log.Info('Finished scheduled task');
}

This way, if/when event tokens change, I don't have to deal with it.

Describe alternatives you've considered The alternative is I build the object myself, which is difficult because I don't appear to have access to a list of keys available and prone to errors/becoming obsolete if/when an event's tokens change.

AnthonyMusgrove commented 4 years ago

This is an awesome idea, I'll look at implementing this now.

AnthonyMusgrove commented 4 years ago

All completed;

ScripterX.Log.Info("Failed to authenticate, allTokens = " + context.allTokens());

Yields:

2020-06-16 13:37:17.137 Info Emby ScripterX: 8b91c4d0-66f2-47c8-9471-ae9c741d6b6f: Failed to authenticate, allTokens = [ [ { "token": "%server.name%", "value": "MediusDev2", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.port.local.http%", "value": "8096", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.port.local.https%", "value": "8920", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.port.public.http%", "value": "8096", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.port.public.https%", "value": "8920", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.version%", "value": "4.4.3.0", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.platform.os%", "value": "Win32NT", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.platform.osver%", "value": "6.2.9200.0 ", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%server.platform.uptime%", "value": "5d 11h 7m 28s ", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%scripterx.version%", "value": "3.0.4.1", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%scripterx.version.major%", "value": "3", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%scripterx.version.minor%", "value": "0", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%scripterx.version.revision%", "value": "1", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%scripterx.version.build%", "value": "4", "IsGlobal": true, "datatype": "string" } ], [ { "token": "%username%", "value": "Anthony", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%user.id%", "value": "0", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%device.id%", "value": "1cd496ba-97bf-46b6-aeb7-b7f64b0a87fe", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%device.name%", "value": "Firefox", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%password%", "value": "asdgfsgfasfgasf", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%device.app%", "value": "Emby Web", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%device.app.version%", "value": "4.4.3.0", "IsGlobal": false, "datatype": "string" } ], [ { "token": "%device.remote.ipaddress%", "value": "::1", "IsGlobal": false, "datatype": "string" } ] ]

Slugger commented 4 years ago

Am I seeing this right? An array of arrays of objects? Hate to be picky, but if you could flatten that to just an array of objects that would be easier to work with. Even better, an object keyed on the token keys is even better still. :)

{
  "%server.platform.uptime%": "5d 11h 7m 28s ",
  "%username%": "Anthony",
  // and so on...
}

This is actually all anyone probably cares about when they just want a dump of all keys & values from the context. I can then stringify() this and post to to my webhook downstream.

AnthonyMusgrove commented 4 years ago

Absolutely mate ill fix that up :)

AnthonyMusgrove commented 4 years ago

There we go!

allTokens = { "%server.name%": "MediusDev2", "%server.port.local.http%": "8096", "%server.port.local.https%": "8920", "%server.port.public.http%": "8096", "%server.port.public.https%": "8920", "%server.version%": "4.4.3.0", "%server.platform.os%": "Win32NT", "%server.platform.osver%": "6.2.9200.0 ", "%server.platform.uptime%": "5d 13h 3m 29s ", "%scripterx.version%": "3.0.4.1", "%scripterx.version.major%": "3", "%scripterx.version.minor%": "0", "%scripterx.version.revision%": "1", "%scripterx.version.build%": "4", "%username%": "Anthony", "%user.id%": "0", "%device.id%": "1cd496ba-97bf-46b6-aeb7-b7f64b0a87fe", "%device.name%": "Firefox", "%password%": "asdgfsgfasfgasf", "%device.app%": "Emby Web", "%device.app.version%": "4.4.3.0", "%device.remote.ipaddress%": "::1" }

from:

ScripterX.Log.Info("Failed to authenticate, allTokens = " + context.allTokens());

AnthonyMusgrove commented 4 years ago

Released now on the Emby Catalog 3.0.4.2 :)

Slugger commented 4 years ago

Confirmed working.