前提・実現したいこと
Node.jsで実装しているプログラムからAPIでDialogflowを呼び出そうとしております。
プログラムを実行すると、PERMISSION_DENIEDエラーが表示されます。
発生している問題・エラーメッセージ
Sending Query: Hello Error: 7 PERMISSION_DENIED: IAM permission 'dialogflow.sessions.detectIntent' on 'projects/xxxx/agent' denied.
該当のソースコード
Javascript
1//Dialogflow 2 3// projectId: ID of the GCP project where Dialogflow agent is deployed 4const projectId = 'xxxx'; 5// sessionId: String representing a random number or hashed user identifier 6const sessionId = '123456789'; 7// queries: A set of sequential queries to be send to Dialogflow agent for Intent Detection 8const queries = [ 9 'Hello' 10] 11// languageCode: Indicates the language Dialogflow agent should use to detect intents 12const languageCode = 'en'; 13 14// Imports the Dialogflow library 15const dialogflow = require('@google-cloud/dialogflow'); 16 17// Instantiates a session client 18const sessionClient = new dialogflow.SessionsClient(); 19 20async function detectIntent( 21 projectId, 22 sessionId, 23 query, 24 contexts, 25 languageCode 26) { 27 // The path to identify the agent that owns the created intent. 28 const sessionPath = sessionClient.projectAgentSessionPath( 29 projectId, 30 sessionId 31 ); 32 33 // The text query request. 34 const request = { 35 session: sessionPath, 36 queryInput: { 37 text: { 38 text: query, 39 languageCode: languageCode, 40 }, 41 }, 42 }; 43 44 if (contexts && contexts.length > 0) { 45 request.queryParams = { 46 contexts: contexts, 47 }; 48 } 49 50 const responses = await sessionClient.detectIntent(request); 51 return responses[0]; 52} 53 54async function executeQueries(projectId, sessionId, queries, languageCode) { 55 // Keeping the context across queries let's us simulate an ongoing conversation with the bot 56 let context; 57 let intentResponse; 58 for (const query of queries) { 59 try { 60 console.log(`Sending Query: ${query}`); 61 intentResponse = await detectIntent( 62 projectId, 63 sessionId, 64 query, 65 context, 66 languageCode 67 ); 68 console.log('Detected intent'); 69 console.log( 70 `Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}` 71 ); 72 // Use the context from this response for next queries 73 context = intentResponse.queryResult.outputContexts; 74 } catch (error) { 75 console.log(error); 76 } 77 } 78} 79executeQueries(projectId, sessionId, queries, languageCode);
試したこと
環境変数GOOGLE_APPLICATION_CREDENTIALSにはダウンロードしたjsonファイルのパスを設定しておりますが、うまくいっていないようです。
あなたの回答
tips
プレビュー