Last active
January 22, 2020 13:00
-
-
Save fcjurado/9b4154e2342faf745d86d8a9bc3bda91 to your computer and use it in GitHub Desktop.
Really simple DialogFlow V2 JS connection with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var projectID = "xxxx-xxxx"; | |
// session-id can apparently be set to an arbitrary value, eg. "abcd1234-abcd-abcd-abcd-abcd12345678". | |
var sessionID = "xxxx-xxxx"; | |
var serviceAccountEmail = "xxxx-xxxxx@xxxxxxxx.iam.gserviceaccount.com"; | |
var serviceAccountPrivateKey = "-----BEGIN PRIVATE KEY-----\nXXXXXXX==\n-----END PRIVATE KEY-----\n"; | |
var ClaimSet = /** @class */ (function () { | |
function ClaimSet(scopes, issuerEmail) { | |
this.scopes = scopes; | |
this.issuerEmail = issuerEmail; | |
this.audienceURL = "https://www.googleapis.com/oauth2/v4/token"; | |
this.duration = 60 * 60 * 1000; | |
} | |
ClaimSet.prototype.ToSpecClaimSet = function () { | |
var issuedAtTime = this.issuedAtTime || Date.now(); | |
var expireAtTime = issuedAtTime + this.duration; | |
return { | |
scope: this.scopes.join(" "), | |
iss: this.issuerEmail, | |
sub: this.delegationEmail, | |
aud: this.audienceURL, | |
iat: Math.floor(issuedAtTime / 1000), | |
exp: Math.floor(expireAtTime / 1000) | |
}; | |
}; | |
return ClaimSet; | |
}()); | |
function GetAssertion(claimSet, privateKey) { | |
//TS: var header = { alg: "RS256", typ: "JWT" }; | |
// Header | |
var oHeader = {alg: 'RS256', typ: 'JWT'}; | |
// Payload | |
var oPayload = claimSet.ToSpecClaimSet(); | |
// Sign JWT, password=616161 | |
var sHeader = JSON.stringify(oHeader); | |
var sPayload = JSON.stringify(oPayload); | |
//TS: return KJUR.jws.JWS.sign(header.alg, header, claimSet.ToSpecClaimSet(), privateKey); | |
return KJUR.jws.JWS.sign(oHeader.alg, sHeader, sPayload, privateKey); | |
} | |
// Get token: | |
var cs = new ClaimSet(["https://www.googleapis.com/auth/cloud-platform"], serviceAccountEmail); | |
var assertion = GetAssertion(cs, serviceAccountPrivateKey); | |
console.log(assertion); | |
$.when( $.post({ | |
url: "https://www.googleapis.com/oauth2/v4/token", | |
headers: { | |
"content-type": "application/json" | |
}, | |
data: JSON.stringify({ | |
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", | |
assertion: assertion | |
}) | |
}) ).then(function( data, textStatus, jqXHR ) { | |
if (data == null) { | |
// Whenever you want... | |
return; | |
} | |
access_token = data.access_token; | |
$.when( $.post({ | |
url: "https://dialogflow.googleapis.com/v2/projects/" + projectID + "/agent/sessions/" + sessionID + ":detectIntent", | |
headers: { | |
"authorization": "Bearer " + access_token, | |
"content-type": "application/json", | |
}, | |
data: JSON.stringify({ | |
queryInput: { | |
text: { | |
"text": "hello", | |
"language_code": "en-US" | |
} | |
}, | |
queryParams: { | |
timeZone: "Europe/Madrid", | |
} | |
}) | |
}) ).then(function( data, textStatus, jqXHR ) { | |
console.log( data ); | |
console.log( data.queryResult.fulfillmentText ); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment