Trigger process using custom chat BOT build on python using API and get the response back with BOT result

Hello Colleagues,

I have a requirement where we need to trigger robocorp process sitting in control room using custom python based chat bot.

I have followed API documentations and I am able to trigger the BOT.

However, that API endpoint doesn;t waits for the BOT to either fail or pass. it just triggers process and immediately return response with state=IP.

below is my sample code -

workspace_id="workspaceid"
process_id="processid"
APIKey="apikey"

def triggerBOT():
    base_url = "https://api.eu1.robocorp.com/process-v1/workspaces/"+workspace_id+"/processes/"+process_id+"/runs"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization":"RC-WSKEY "+APIKey
    }

    payloads ={
        "ticket_number":"123",
        "message": "Greetings from API Helper single payload"
    }
    response  = requests.post(base_url, headers=headers, json=payloads, timeout=180)
    # Check for HTTP codes other than 200
    if response.status_code != 200: 
        print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
        exit()

    return json.loads(response.text)["id"]

while the requirement is that it should wait for the process to complete/fail and then return response with the status and if it’s failed then what was the cause of failure.

please let me know if any work around is the to solve this.

Thank you in advance.

Hi Mritunjay!

Process API is indeed asynchronous, so it’ll return you the run id so that you can query the status later.

If you are working on chatbot platform like RASA, there is an example how to use their external events to return data back from the process to the chatbot. I’m happy to jump on a call with you to walk through it!

br,
tommi.

Hey Tommi,

Thanks and great to see you again in here :slight_smile:

I get that and have written below code to read the status.

def triggerBOTandGetStatus():
    runID = triggerBOT()
    base_url = "https://api.eu1.robocorp.com/process-v1/workspaces/"+workspace_id+"/processes/"+process_id+"/runs/"+runID+""
    #base_url = "https://api.eu1.robocorp.com/process-v1/workspaces/"+workspace_id+"/processes/"+process_id+"/runs/42f477f5-a47f-49fc-9a5a-71421b1485d7"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization":"RC-WSKEY "+APIKey
    }
    state = ""
    response  = requests.get(base_url, headers=headers, timeout=180)
    # Check for HTTP codes other than 200
    if response.status_code != 200: 
        print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
        exit()
    else:
        while state!="COMPL":
            response  = requests.get(base_url, headers=headers, timeout=180)
            state = json.loads(response.text)["state"]
            print(response.text)
            time.sleep(5)

however my concern is number of request we will send- above code will keep sending request in every 5 seconds till the time either process completes/fails.

is above code looks fine for you?

Thank you!

Hey!

How I did it with my example is that the robot itself triggers a call back to RASA, so you would not need a “listener” at all.

The example of that is in the robot here. However, remember that in this case your robot will need to execute in an environment where it has access to your RASA server.

And btw I just assumed that you are using RASA. Happy to have a look at options if you are using something else!

Hey Tommi,

I get that and following your blog on rasa integration and I was able to successfully perform that exercise.

however this requirement is different, we wanted to build our custom python scripts by which we wanted to trigger robocorp processes with payload as workitems and read the response back once the process is failed/completed for that particular work item.

I hope I am able to articulate the requirements.

Thank you!

Hey,

Sorry I completely missed your last message!

You have two ways to get responses (process outputs) back:

  1. Set up a webhook in control room to trigger on process events (complete, fail, …). This way you get the response back immediately when a run is complete.
  2. Poll the process API top see when the status changes to complete, and then get the outputs.

We can chat about the most optimal way in the call today. :slight_smile: