Run a UiPath job from Google Dialogflow using Python!

Soukaina Zouyne
SOGEDES tech savvy
Published in
7 min readMay 11, 2023

Integrating your UiPath workflows to your Dialogflow chatbots is not a complex process, but figuring out the steps that are required for the first time can seem like a challenging task to accomplish.

If this is the first time you are creating a chatbot that triggers a UiPath robot — or if it has been a while since you did last time and need a reminder — this blog post is for you!

In this article I’ll give you step-by-step instructions on how to trigger a UiPath robot from a Dialogflow chatbot using Python webhook that will send a request to the UiPath Orchestrator API with the input parameters taken from the chat.

How to trigger UiPath robot from Google Dialogflow?

As you may know the UiPath chatbot connector (chatbot.uipath.com) to Google Dialogflow ES was deprecated on July 1st, 2021 and removed permanently on Oct 24th, 2021.

For that reason, we need to find another way to trigger our robots from scratch😊.

The simplest way to do so, is to build a webhook that will receive a POST Request from Dialogflow, on the other hand the webhook should send requests to Orchestrator API to get the robot triggered.

Before getting started let’s build a small UiPath project which needs a few inputs and generates few outputs.

For instance, we can use the fake generator web site which asks for the gender, the name set and the country to generate a fake name and an address. The link to the Fake Generator web site is: https://www.fakenamegenerator.com

Fake name generator web site

In UiPath Studio, we should build a small flow to browse the fake name generator web site, select the given gender, name set and country, click Generate and get the name and the address, then we can publish our project to Orchestrator:

UiPath workflow

Now it’s time to create the agent in Google Dialogflow which should ask the user for the three input parameters. For that we can modify the text response of the default welcome intent as bellow:

Welcome Intent

After that we have to add a new intent ‘Get Started’ to ask the user to provide the gender, the name set and the country parameters, the training phrases of this intent could be something like generate fake name, get fake name, get name and address…etc.

Get-Started entent

The next intent would be ‘GetResults’ which should store our three input parameters.

Get-Results parameters

In this intent the Fulfillment should be activated to be able to trigger the UiPath robot:

Enabling Fulffillment

To display the output results extracted we will need to create a new intent called ‘DisplayResults’, so that we can give time to our UiPath robot to finish the job, so the training phrases would be like ok, sure, yes…as the previous intent asked the user to type ok to show the results…As we should enable the webhook call for this intent as well, the response would be generated from our webhook.

The last intent

Now that our automation project is ready as well as the Dialogflow agent, let’s build our Python Webbook.

First thing we need to do is to get the Client Id, User key and Tenant Name from Orchestrator, under Manage Orchestrator Services.

Getting API Access data

The first request we have to send to the Orchestrator API is regarding the authentication, so we have to send our Client Id and the User Key to get the access token.

import flask
import requests
import json
import time
from flask import make_response
from flask import send_from_directory, request

data = { "grant_type": "refresh_token",
"client_id": "Your_CLIENT_ID",
"refresh_token": "Your_User_Key" }

headers = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "Your_TenantName" }

r0 = requests.post("https://account.uipath.com/oauth/token", data, headers)
response0 = json.loads(r0.content)

auth = "Bearer "+response0["access_token"]

According to the Orchestrator API official documentation: https://postman.uipath.rocks/) before triggering a UiPath job we need to have the OrganizationUnitId and the Release key for that we have to send the two requests bellow:

headers1 = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "TenantName",
"Authorization" : auth}

r1 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Folders?$Filter=DisplayName eq 'OrchestratorFolderName'", headers =headers1)
response1 = json.loads(r1.text)

orgID = str(response1["value"][0]["Id"])

headers2 = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "TenantName",
"X-UIPATH-OrganizationUnitId" : orgID,
"Authorization" : auth}

r2 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Releases?$filter=Name eq 'ProcessName'", headers =headers2)
response2 = json.loads(r2.text)

releaseKey = response2["value"][0]["Key"]

So now that we have everything needed, we want to take the input parameters, trigger the robot which needs about 12 seconds, get the output parameters, and display them to the user in the chat, however by default the webhook timeout is 5 seconds. For that reason, we had to add the additional intent ‘DisplayResults’, so that we can ask the user to wait for a moment and press Ok to view the results.

If you want to increase the fulfillment timeout to up to 30 seconds then you can use Dialogflow CX.

Since we added the ‘DisplayResults’ intent which uses also the webhook call, we should check in our code the intent name, so if it’s the ‘GetResults’ intent then we can trigger the robot, and if it’s the ‘DisplayResulst’ intent, means all what we need is to extract the output parameters:

app = flask.Flask(__name__)

@app.route('/uiRobot', methods=['POST'])

def uiRobot():
req = request.get_json(silent=True, force= True)
intentName = req["queryResult"]["intent"]["displayName"]
if intentName == "GetResults":
country = req["queryResult"]["parameters"]["Country"]
gender = req["queryResult"]["parameters"]["Gender"]
nameset = req["queryResult"]["parameters"]["Name-Set"]
startInfo = {}
startInfo['ReleaseKey'] = releaseKey
startInfo['Strategy'] = 'ModernJobsCount'
startInfo['JobsCount'] = '1'
startInfo['InputArguments'] = json.dumps({"in_Sexe":gender,"in_Pays":country,"in_EnsembleMots":nameset})
data2 ={}
data2['startInfo'] = startInfo
json_data = json.dumps(data2)
r2 = requests.post("https://cloud.uipath.com/OrganizationID/TenantName/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs", data = json_data, headers = headers2)
wjdata2= json.loads(r2.text)
time.sleep(4)
return ""
if intentName == "DisplayResults":
r3 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Jobs?$Filter=State eq 'Successful' AND ReleaseName eq 'ProcessName'&$orderby=EndTime DESC", headers = headers2)
wjdata2 = json.loads(r3.text)
result1 = wjdata2["value"][0]["OutputArguments"]
result2= json.loads(result1)
res1 = result2['out_ExtractedName']
res2 = result2['out_ExtractedAdresse']
r = json.dumps({
'fulfillmentText': str("Your fake name and address are: "+ res1+", ")+res2, })
return r

The entire code is down below, you can also find it in our GitHub: https://github.com/sogedes-dev/Trigger-UiPath-Job-From-Dialogflow

import flask
import requests
import json
import time
from flask import make_response
from flask import send_from_directory, request

data = { "grant_type": "refresh_token",
"client_id": "CLIENT ID",
"refresh_token": "User Key" }

headers = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "TenantName" }

r0 = requests.post("https://account.uipath.com/oauth/token", data, headers)
response0 = json.loads(r0.content)

auth = "Bearer "+response0["access_token"]

headers1 = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "TenantName",
"Authorization" : auth}

r1 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Folders?$Filter=DisplayName eq 'OrchestratorFolderName'", headers =headers1)
response1 = json.loads(r1.text)

orgID = str(response1["value"][0]["Id"])

headers2 = { "Content-Type" : "application/json",
"X-UIPATH-TenantName" : "TenantName",
"X-UIPATH-OrganizationUnitId" : orgID,
"Authorization" : auth}

r2 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Releases?$filter=Name eq 'ProcessName'", headers =headers2)
response2 = json.loads(r2.text)

releaseKey = response2["value"][0]["Key"]

app = flask.Flask(__name__)

@app.route('/uiRobot', methods=['POST'])

def uiRobot():
req = request.get_json(silent=True, force= True)
intentName = req["queryResult"]["intent"]["displayName"]
if intentName == "GetResults":
country = req["queryResult"]["parameters"]["Country"]
gender = req["queryResult"]["parameters"]["Gender"]
nameset = req["queryResult"]["parameters"]["Name-Set"]
startInfo = {}
startInfo['ReleaseKey'] = releaseKey
startInfo['Strategy'] = 'ModernJobsCount'
startInfo['JobsCount'] = '1'
startInfo['InputArguments'] = json.dumps({"in_Sexe":gender,"in_Pays":country,"in_EnsembleMots":nameset})
data2 ={}
data2['startInfo'] = startInfo
json_data = json.dumps(data2)
r2 = requests.post("https://cloud.uipath.com/OrganizationID/TenantName/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs", data = json_data, headers = headers2)
wjdata2= json.loads(r2.text)
time.sleep(4)
return ""
if intentName == "DisplayResults":
r3 = requests.get("https://cloud.uipath.com/OrganizationID/TenantName/odata/Jobs?$Filter=State eq 'Successful' AND ReleaseName eq 'ProcessName'&$orderby=EndTime DESC", headers = headers2)
wjdata2 = json.loads(r3.text)
result1 = wjdata2["value"][0]["OutputArguments"]
result2= json.loads(result1)
res1 = result2['out_ExtractedName']
res2 = result2['out_ExtractedAdresse']
r = json.dumps({
'fulfillmentText': str("Your fake name and address are: "+ res1+", ")+res2, })
return r

if __name__ == "__main__":
app.secret_key = 'ItIsASecret'
app.debug = True
app.run()

Now, let’s run our app:

So, go to the folder where you have your python script, and open cmd, then run this command: python YourFileName.py

Running the Python app

Now we need to specify the URL of our server in the fullfilment area in Dialogflow, however the URL should be public and not of a localhost, for that we should make use of ngrok— a tool that offers free running of localhost. Sign up for an account and download ngrok: https://ngrok.com/

Once done, run the ngrok application in the same folder where you created the python weebhook script, and type the command: ngrok http 5000

Copy the forwarding URL and note that we want the second one listed as Dialogflow only accepts https and not http:

Insert the forwarding URL into your Dialogflow ‘Webhook’ under the ‘Fulfillment’ section and click save.

Congratulations! You have successfully built a Dialogflow Chatbot that launchs a UiPath job with input and output parameters.

Conclusion

We have covered the necessary steps required in order to trigger UiPath workflows from a Dialogflow chatbot by using a Python Webhook.

--

--