Skip to content

MESRA-Motor-Sehat-dan-Rapi/predictchatbot-dialogflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dialogflow Integration with Express.js

Description

This project demonstrates how to integrate a Dialogflow agent with an Express.js server. The application allows users to send queries to a Dialogflow agent and receive responses via a RESTful API.

Features

  • Query Dialogflow agent using REST API.
  • Handle user queries and return bot responses.
  • Use environment variables for configuration.

Prerequisites

  1. Node.js (version 14 or higher recommended)
  2. Dialogflow Agent created on Dialogflow Console.
  3. Google Cloud Project with the following APIs enabled:
    • Dialogflow API
    • IAM & Admin API
  4. Service Account Key JSON for authenticating with Google Cloud.
  5. gcloud CLI (optional, for deployment to Google Cloud).

Installation

  1. Clone this repository:

    git clone <repository-url>
    cd <repository-folder>
  2. Install dependencies:

    npm install
  3. Set up environment variables: Create a .env file in the root directory and add the following variables:

    PORT=7000
    PROJECT_ID=your-dialogflow-project-id
    CREDENTIALS_PATH=./credential.json
  4. Add your Service Account Key JSON:

    • Download the JSON key from the Google Cloud Console.
    • Place the file in the project directory and name it credential.json (or update CREDENTIALS_PATH in .env).

Usage

Running Locally

  1. Start the server:

    npm start
  2. Send a GET request to the server:

    • URL: http://localhost:7000/?message=Your+Message
  3. Example Response:

    {
        "message": "Success",
        "result": {
            "user": "Your Message",
            "bot": "Response from Dialogflow"
        }
    }

Deploying to Google Cloud Run

  1. Authenticate with Google Cloud:

    gcloud auth login
  2. Deploy the application:

    gcloud run deploy dialogflow-integration \
        --source . \
        --platform managed \
        --region <your-region> \
        --set-env-vars GOOGLE_APPLICATION_CREDENTIALS=/path/to/credential.json \
        --set-env-vars PROJECT_ID=your-dialogflow-project-id
  3. Access the deployed service and query it via the URL provided by Cloud Run.

Project Structure

.
├── credential.json        # Service Account Key JSON (not included in repo)
├── .env                   # Environment variables
├── package.json           # Dependencies and scripts
├── app.js                 # Main server code
└── README.md              # Project documentation

Code Overview

Main Components

  1. Dialogflow Integration

    • The runSample function interacts with Dialogflow API to send user queries and fetch bot responses.
    async function runSample(userMessage) {
        const sessionId = uuid.v4();
        const sessionClient = new dialogflow.SessionsClient();
        const sessionPath = sessionClient.sessionPath(projectId, sessionId);
    
        const request = {
            session: sessionPath,
            queryInput: {
                text: {
                    text: userMessage,
                    languageCode: 'en-US',
                },
            },
        };
    
        const responses = await sessionClient.detectIntent(request);
        return {
            user: responses[0].queryResult.queryText,
            bot: responses[0].queryResult.fulfillmentText,
        };
    }
  2. Express.js Server

    • A simple Express server exposes an API endpoint to handle user queries.
    app.get("/", async (req, res) => {
        try {
            const userMessage = req.query.message;
            const result = await runSample(userMessage);
            return res.status(200).json({ message: "Success", result });
        } catch (error) {
            console.error(error);
            return res.status(500).json({ message: "Server error", error });
        }
    });

Troubleshooting

  1. Malformed Request Error (400)

    • Ensure that userMessage is not empty or null.
    • Validate GOOGLE_APPLICATION_CREDENTIALS path and PROJECT_ID.
  2. Environment Variable Issues

    • Confirm that .env file is properly configured and loaded.
  3. Dialogflow API Error

Cloud Computing's Team

Name Bangkit ID University
Arifatus Fitriani C296B4KX0643 Universitas Pembangunan Nasional Veteran Jawa Timur
Mochammad Irfan Efendi C202B4KY2532 Universitas Dr Soetomo

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors