无需Google Cloud Project,在本地PC搭建Dialogflow Webhook的方法咨询
Got it, let's walk through this step by step—since you're new to Node.js module syntax and want to avoid Google Cloud, we'll use Express to build a local server and expose it to the internet for Dialogflow to reach.
First: Understanding exports. Syntax
That exports.hellohttp you saw in the Google Functions guide is part of Node.js's CommonJS module system. Google Cloud Functions expects you to export a function so their platform can trigger it when an HTTP request comes in. Since we're hosting locally, we don't need this exact syntax—instead, we'll use Express to define our own HTTP endpoint.
Step 1: Set Up Your Local Project
- Create a new folder for your webhook (e.g.,
dialogflow-local-webhook) and open it in your terminal. - Initialize a Node.js project:
npm init -y - Install the required packages:
express: To build our HTTP serverdialogflow-fulfillment: Official SDK to handle Dialogflow requests
npm install express dialogflow-fulfillment
Step 2: Write Your Webhook Code (index.js)
Create an index.js file in your folder, and paste this code (I'll explain what each part does):
const express = require('express'); const { WebhookClient } = require('dialogflow-fulfillment'); // Initialize Express server const app = express(); // Parse JSON request bodies (Dialogflow sends data as JSON) app.use(express.json()); // Define your webhook endpoint (this replaces the Google Functions `exports.hellohttp`) app.post('/webhook', (req, res) => { // Create a Dialogflow agent instance to handle the request/response const agent = new WebhookClient({ request: req, response: res }); // Example intent handler: responds to the "Default Welcome Intent" function welcomeIntentHandler(agent) { agent.add('Hey there! This is coming from my local webhook 🎉'); } // Map intent names to their handler functions const intentMap = new Map(); intentMap.set('Default Welcome Intent', welcomeIntentHandler); // Add more intents here as you build them, e.g.: // intentMap.set('Your Custom Intent', yourCustomHandler); // Let the agent process the request agent.handleRequest(intentMap); }); // Start the server on port 3000 (you can change this if needed) const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Local webhook server running on http://localhost:${PORT}`); });
Step 3: Run Your Local Server
In your terminal, run:
node index.js
You should see a message saying Local webhook server running on http://localhost:3000—that means your server is up!
Step 4: Expose Your Local Server to the Internet
Dialogflow can't send requests to your local localhost URL, so we need to create a public tunnel. The easiest tool for this is ngrok:
- Download and install ngrok (it's free for basic use).
- In a new terminal window, run:
ngrok http 3000 - Ngrok will generate a public URL like
https://abc123-45-67-89-10.ngrok.io—copy this URL.
Step 5: Connect to Dialogflow
- Go to your Dialogflow agent's Console.
- Navigate to Fulfillment > Webhook.
- Toggle the webhook on.
- Paste your ngrok URL plus
/webhookinto the URL field (e.g.,https://abc123-45-67-89-10.ngrok.io/webhook). - Save your changes.
Step 6: Test It Out
Go to the Test tab in Dialogflow, send a message that triggers your "Default Welcome Intent"—you should get the response from your local server!
Notes:
- Every time you restart ngrok, your public URL will change—you'll need to update the Dialogflow webhook URL accordingly.
- If you modify your
index.jscode, restart your Node server (node index.jsagain) to apply changes.
内容的提问来源于stack exchange,提问作者user1021085




