BackgroundAction Help

Using the new Background Actions in 5.4, I was hoping to call an external API for any tiddler created/ modified/ deleted where the field realm has the value ‘Personal’.

I created a tiddler called ‘$:/hrs/background-actions/api-sync’, with the tag, ‘$:/tags/BackgroundAction’ and field, track-filter as ‘[[realm[Personal]]’ and with body:

\procedure trigger-api-sync()

<%if [<currentTiddler>field:realm[Personal]] %>
	<$let 
		payload-title={{{ [<currentTiddler>] }}}
		payload-text={{{ [<currentTiddler>get[text]] }}}
		payload-tags={{{ [<currentTiddler>get[tags]] }}}
		payload-realm={{{ [<currentTiddler>get[realm]] }}}
		payload-id={{{ [<currentTiddler>get[Id]] }}}
	>
		<$action-sendmessage 
			$message="tm-http-request"
			url="https://api.yourdomain.com/endpoint"
			method="POST"
			header-content-type="application/json"
			bind-status="$:/state/api-sync/status"
			body=`{
				"title": "$(payload-title)$",
				"text": $(payload-text)$,
				"tags": "$(payload-tags)$",
				"realm": "$(payload-realm)$"
				${ if [<payload-id>minlength[1]], ?[,"id": "$(payload-id)$"] }$
			}`
		/>
	</$let>
	
<% endif %>
\end trigger-api-sync

<<trigger-api-sync>>

In the logs I see this message, “Processing background action $:/hrs/background-actions/api-sync” upon saving a tiddler, say ‘Test’ with the realm ‘Personal’, but the api never gets called. For debugging, I also tried replacing the procedure with a simple action-log widget, but not seeing anything for that either.

What am I doing wrong?

Thanks

There’s a syntax error in your filter, try this: [realm[Personal]] (note the single opening square bracket).

Fred

Thanks Fred. Sorry, that extra ‘[’ was a typo in my post - I have written the filter correctly in my wiki.

This very basic example works on tiddliwiki.com

tags: $:/tags/BackgroundAction
title: Test
track-filter: [realm[Personal]]

<$action-log event="realm=Personal"/>

So my best guess is that in your code, the currentTiddler variable value is $:/hrs/background-actions/api-sync and not the triggering tiddler title you expect.

TW’s history tracking mechanism might help here, try this (not tested):

\procedure trigger-api-sync()

<%if [<currentTiddler>field:realm[Personal]] %>
	<$let 
		payload-title={{{ [<currentTiddler>] }}}
		payload-text={{{ [<currentTiddler>get[text]] }}}
		payload-tags={{{ [<currentTiddler>get[tags]] }}}
		payload-realm={{{ [<currentTiddler>get[realm]] }}}
		payload-id={{{ [<currentTiddler>get[Id]] }}}
	>
		<$action-sendmessage 
			$message="tm-http-request"
			url="https://api.yourdomain.com/endpoint"
			method="POST"
			header-content-type="application/json"
			bind-status="$:/state/api-sync/status"
			body=`{
				"title": "$(payload-title)$",
				"text": $(payload-text)$,
				"tags": "$(payload-tags)$",
				"realm": "$(payload-realm)$"
				${ if [<payload-id>minlength[1]], ?[,"id": "$(payload-id)$"] }$
			}`
		/>
	</$let>
	
<% endif %>
\end trigger-api-sync

<$tiddler tiddler={{$:/HistoryList!!current-tiddler}}>
<<trigger-api-sync>>
</$tiddler>

It might work for interactive tiddler creation/modification, but probably won’t trigger on programmatic changes (ie. <$action-setfield> for example). YMMV

Fred