简体   繁体   中英

How to create the intent via code in dialogflow?

I am beginner in dialogflow and trying to create intent and context through code, but i am unable to achieve it.This is the code i am using, i referred the below link also but no help,

https://cloud.google.com/dialogflow/es/docs/how/manage-intents#create_intent

but getting below error: 无法在当前环境中检测到项目 ID。


    function main(
            projectId = 'trainer-gulw',
            displayName = 'intent_001',
            trainingPhrasesParts = [
              'Hello, What is weather today?',
              'How is the weather today?',
            ],
            messageTexts = ['Rainy', 'Sunny']
          ) {    
            const dialogflow = require('@google-cloud/dialogflow');    
            // Instantiates the Intent Client
            const intentsClient = new dialogflow.IntentsClient();      
            async function createIntent() {
              // Construct request      
              // The path to identify the agent that owns the created intent.
              const agentPath = intentsClient.projectAgentPath(projectId);      
              const trainingPhrases = [];      
              trainingPhrasesParts.forEach(trainingPhrasesPart => {
                const part = {
                  text: trainingPhrasesPart,
                };      
                // Here we create a new training phrase for each provided part.
                const trainingPhrase = {
                  type: 'EXAMPLE',
                  parts: [part],
                };      
                trainingPhrases.push(trainingPhrase);
              });      
              const messageText = {
                text: messageTexts,
              };      
              const message = {
                text: messageText,
              };      
              const intent = {
                displayName: displayName,
                trainingPhrases: trainingPhrases,
                messages: [message],
              };
              const createIntentRequest = {
              parent: agentPath,
              intent: intent,
              };     
              // Create the intent
              const [response] = await intentsClient.createIntent(createIntentRequest);
              console.log(`Intent ${response.name} created`);
        }
  
    createIntent();
  
    // [END dialogflow_create_intent]
  }

TIA

From the screen shot, it looks like you're running this on your local machine. The error message suggests that the environment variables that point to your credentials isn't setup.

See the Before you Begin section of the library documentation for how to setup that environment. One of the steps includes setting up authentication by downloading credentials and setting an environment variable to point to them. This will also set the project ID you're working with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM