How to run custom action in background?

Can you try this, just to see if it will give you NameError: name 'next_action' is not defined or NameError: name 'sender_id' is not defined?

data = {
    "sender_id": "default"
    "next_action": "action_notify_data_changed",
}

res = requests.post('http://localhost:5055/webhook/', data=json.dumps(data))

Also, since you’re doing it with Python, make sure the keys are enclosed within " ".

Can you also try it using Postman?

after adding " " NameError is gone, but now I got the other error.

My code is like this,

class ActionSessionStart(Action):
    def name(self) -> Text:
        return "action_session_start"

    async def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
       
        client = Client(url='opc.tcp://141.82.166.100:4840/freeopcua/server/')
        client.connect()

        node = client.get_node("ns=2;i=2")
        print('#########################initial value=', node.get_value(),'###############################')

        handler = SubscriptionHandler(dispatcher)
        # We create a Client Subscription.
        subscription = client.create_subscription(500, handler)
        nodes = [
            node,
        ]
        # We subscribe to data changes for two nodes (variables).
        subscription.subscribe_data_change(nodes)
        
        return []

class SubscriptionHandler():
    def __init__(self, dispatcher) -> None:
        self._dispatcher = dispatcher

    def datachange_notification(self, node: Node, val, data):
        print('@@@@@@@@@@@@@@@@@datachange_notification %r %s' %(node, val),'@@@@@@@@@@@@@@@@@')
        self._dispatcher.utter_message(text=('datachange_notification %r %s' %(node, val)))

        data = {
            "sender_id": "default",
            "next_action": "action_notify_data_changed",
        }
        res = requests.post('http://localhost:5055/webhook/', data=json.dumps(data))
        print(str(res.status_code) + " | " + res.text)

class AtionNotifyDataChanged(Action):
    def name(self) -> Text:
        return "action_notify_data_changed"
    
    async def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

            dispatcher.utter_message(text=("🛑Warning! test value is 0!"))

And I got an error like this:

Exception occurred while handling uri: 'http://localhost:5055/webhook/'
Traceback (most recent call last):
  File "c:\users\je991\anaconda3\lib\site-packages\sanic\app.py", line 939, in handle_request
    response = await response
  File "c:\users\je991\anaconda3\lib\site-packages\rasa_sdk\endpoint.py", line 104, in webhook
    result = await executor.run(action_call)
  File "c:\users\je991\anaconda3\lib\site-packages\rasa_sdk\executor.py", line 392, in run
    tracker_json = action_call["tracker"]
KeyError: 'tracker'
500 | <!DOCTYPE html><html lang=en><meta charset=UTF-8><title>⚠️ 500 — Internal Server Error</title>
<style>
        html { font-family: sans-serif }
        h2 { color: #888; }
        .tb-wrapper p { margin: 0 }
        .frame-border { margin: 1rem }
        .frame-line > * { padding: 0.3rem 0.6rem }
        .frame-line { margin-bottom: 0.3rem }
        .frame-code { font-size: 16px; padding-left: 4ch }
        .tb-wrapper { border: 1px solid #eee }
        .tb-header { background: #eee; padding: 0.3rem; font-weight: bold }
        .frame-descriptor { background: #e2eafb; font-size: 14px }
    </style>
<h1>⚠️ 500 — Internal Server Error</h1><p>The server encountered an internal error and cannot complete your request.

Yes, I also tried in Postman, but it also have an error like this: Cloud Agent Error: Can not send requests to localhost. Select a different agent.

Wait… you’re sending the POST request from a custom action? Why?

You’re doing this to trigger a custom action, but then what would trigger the trigger if it is a custom action itself?

I don’t know what your data that changes is, but whenever it happens, you should execute the POST request from outside of Rasa.

Also make sure default is a sender ID that exists.


Anyway, again, try sending the request from Postman to see what happens.

To post to custom action use following format

{

next_action: "action_notify_data_changed",
"tracker":{"sender_id":"user123"}

}