How to listen to an MQTT topic in automation

Doesn’t your example already pass a variable to a script?

It’s passing the variable message to script.test_script and the script is referencing message’s value by employing it in a template like this {{ message }}.