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.
- Query Dialogflow agent using REST API.
- Handle user queries and return bot responses.
- Use environment variables for configuration.
- Node.js (version 14 or higher recommended)
- Dialogflow Agent created on Dialogflow Console.
- Google Cloud Project with the following APIs enabled:
- Dialogflow API
- IAM & Admin API
- Service Account Key JSON for authenticating with Google Cloud.
- gcloud CLI (optional, for deployment to Google Cloud).
-
Clone this repository:
git clone <repository-url> cd <repository-folder>
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the root directory and add the following variables:PORT=7000 PROJECT_ID=your-dialogflow-project-id CREDENTIALS_PATH=./credential.json
-
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 updateCREDENTIALS_PATHin.env).
-
Start the server:
npm start
-
Send a GET request to the server:
- URL:
http://localhost:7000/?message=Your+Message
- URL:
-
Example Response:
{ "message": "Success", "result": { "user": "Your Message", "bot": "Response from Dialogflow" } }
-
Authenticate with Google Cloud:
gcloud auth login
-
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 -
Access the deployed service and query it via the URL provided by Cloud Run.
.
├── 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
-
Dialogflow Integration
- The
runSamplefunction 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, }; }
- The
-
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 }); } });
-
Malformed Request Error (400)
- Ensure that
userMessageis not empty or null. - Validate
GOOGLE_APPLICATION_CREDENTIALSpath andPROJECT_ID.
- Ensure that
-
Environment Variable Issues
- Confirm that
.envfile is properly configured and loaded.
- Confirm that
-
Dialogflow API Error
- Check Google Cloud Logs for more details: Logs Explorer.
| Name | Bangkit ID | University |
|---|---|---|
| Arifatus Fitriani | C296B4KX0643 | Universitas Pembangunan Nasional Veteran Jawa Timur |
| Mochammad Irfan Efendi | C202B4KY2532 | Universitas Dr Soetomo |