Share #5 Tạo Menu thực đơn nhà hàng cho bot - Chatbot nhà hàng Node.js và Messenger

minhnhilythuc

New member
## Tạo menu nhà hàng cho bot - node.js và messenger chatbot

**Giới thiệu**

Trong hướng dẫn này, bạn sẽ tìm hiểu cách tạo một menu nhà hàng chatbot bằng Node.js và nền tảng Messenger.Chúng tôi sẽ xây dựng một chatbot đơn giản cho phép người dùng tìm kiếm các mục trên menu, xem lịch sử đặt hàng của họ và đặt hàng mới.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Môi trường phát triển Node.js
* SDK nền tảng Messenger cho Node.js
* Menu nhà hàng ở định dạng JSON

** Bước 1: Tạo một dự án mới **

Đầu tiên, chúng ta cần tạo một dự án Node.js mới.Bạn có thể làm điều này bằng cách chạy lệnh sau trong thiết bị đầu cuối của bạn:

`` `
NPM init
`` `

Điều này sẽ tạo một thư mục mới cho dự án của bạn và tệp `pack.json`.

** Bước 2: Cài đặt các phụ thuộc **

Tiếp theo, chúng tôi cần cài đặt các phụ thuộc cho dự án của chúng tôi.Chúng ta có thể làm điều này bằng cách chạy lệnh sau:

`` `
NPM Cài đặt-Save Messenger-Plat
`` `

Điều này sẽ cài đặt SDK nền tảng Messenger cho Node.js.

** Bước 3: Tạo tệp cấu hình bot **

Bước tiếp theo là tạo tệp cấu hình bot.Tệp này sẽ chứa thông tin mà nền tảng Messenger cần biết về bot của bạn.Bạn có thể tạo tệp này bằng cách chạy lệnh sau:

`` `
Chạm vào .Env
`` `

Mở tệp `.env` trong trình chỉnh sửa của bạn và thêm mã sau:

`` `
Cổng = 3000

Messenger_app_id = your_messenger_app_id
Messenger_app_secret = your_messenger_app_secret
`` `

** Bước 4: Tạo tệp dữ liệu menu **

Bước tiếp theo là tạo một tệp chứa dữ liệu cho menu nhà hàng của bạn.Bạn có thể tạo tệp này bằng cách chạy lệnh sau:

`` `
Touch menu.json
`` `

Mở tệp `menu.json` trong trình chỉnh sửa của bạn và thêm mã sau:

`` `
{
"mặt hàng": [
{
"Tên": "Pizza",
"Giá": "$ 10",
"Mô tả": "Một chiếc bánh pizza ngon với pepperoni, nấm và hành tây."
},
{
"Tên": "Burger",
"Giá": "$ 15",
"Mô tả": "Một chiếc bánh burger ngon ngọt với tất cả các bản sửa lỗi."
},
{
"Tên": "Salad",
"Giá": "$ 12",
"Mô tả": "Một món salad tươi với sự lựa chọn của bạn là thay đồ."
},
{
"Tên": "khoai tây chiên",
"Giá": "$ 5",
"Mô tả": "Một khẩu phần khoai tây chiên ngon."
}
]
}
`` `

** Bước 5: Tạo mã bot **

Bước tiếp theo là tạo mã cho bot của bạn.Bạn có thể làm điều này bằng cách tạo một tệp mới có tên là `index.js`.Mở tệp `index.js` trong trình chỉnh sửa của bạn và thêm mã sau:

`` `
const express = yêu cầu ('express');
const messenger = yêu cầu ('messenger-platform');

const app = express ();

app.get ('/', (req, res) => {
res.Send ('Hello World!');
});

app.listen (process.env.port, () => {
Console.log (`Bot đang nghe trên cổng $ {process.env.port}`);
});

messenger.login (process.env.messenger_app_id, process.env.messenger_app_secret, (err, api) => {
if (err) {
Console.log (err);
trở lại;
}

api.on ('message', (tin nhắn) => {
const text = message.text;

if (text === '/menu') {
api.sendtext (message.sender.id, getMenu ());
} if if (text === '/order') {
api.sendtext (message.sender.id, getOrderForm ());
} khác {
api.sendtext (message.sender.id, 'Tôi đã không hiểu điều đó.');
}
});
});

hàm getMenu () {
menu const = yêu cầu ('./ menu.json');
const items = menu.items.map (item => {
trở lại {
Tiêu đề: item.name,
Giá: item.price,
Sự miêu tả
=======================================
## Create a restaurant menu for BOT - Node.js and Messenger chatbot

**Introduction**

In this tutorial, you will learn how to create a restaurant menu chatbot using Node.js and the Messenger platform. We will build a simple chatbot that allows users to search for items on the menu, view their order history, and make new orders.

**Prerequisites**

To follow this tutorial, you will need the following:

* A Node.js development environment
* The Messenger Platform SDK for Node.js
* A restaurant menu in JSON format

**Step 1: Create a new project**

First, we need to create a new Node.js project. You can do this by running the following command in your terminal:

```
npm init
```

This will create a new folder for your project and a `package.json` file.

**Step 2: Install the dependencies**

Next, we need to install the dependencies for our project. We can do this by running the following command:

```
npm install --save messenger-platform
```

This will install the Messenger Platform SDK for Node.js.

**Step 3: Create the bot configuration file**

The next step is to create a bot configuration file. This file will contain the information that the Messenger Platform needs to know about your bot. You can create this file by running the following command:

```
touch .env
```

Open the `.env` file in your editor and add the following code:

```
PORT=3000

MESSENGER_APP_ID=YOUR_MESSENGER_APP_ID
MESSENGER_APP_SECRET=YOUR_MESSENGER_APP_SECRET
```

**Step 4: Create the menu data file**

The next step is to create a file that contains the data for your restaurant menu. You can create this file by running the following command:

```
touch menu.json
```

Open the `menu.json` file in your editor and add the following code:

```
{
"items": [
{
"name": "Pizza",
"price": "$10",
"description": "A delicious pizza with pepperoni, mushrooms, and onions."
},
{
"name": "Burger",
"price": "$15",
"description": "A juicy burger with all the fixings."
},
{
"name": "Salad",
"price": "$12",
"description": "A fresh salad with your choice of dressing."
},
{
"name": "Fries",
"price": "$5",
"description": "A delicious serving of fries."
}
]
}
```

**Step 5: Create the bot code**

The next step is to create the code for your bot. You can do this by creating a new file called `index.js`. Open the `index.js` file in your editor and add the following code:

```
const express = require('express');
const messenger = require('messenger-platform');

const app = express();

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen(process.env.PORT, () => {
console.log(`Bot is listening on port ${process.env.PORT}`);
});

messenger.login(process.env.MESSENGER_APP_ID, process.env.MESSENGER_APP_SECRET, (err, api) => {
if (err) {
console.log(err);
return;
}

api.on('message', (message) => {
const text = message.text;

if (text === '/menu') {
api.sendText(message.sender.id, getMenu());
} else if (text === '/order') {
api.sendText(message.sender.id, getOrderForm());
} else {
api.sendText(message.sender.id, 'I didn\'t understand that.');
}
});
});

function getMenu() {
const menu = require('./menu.json');
const items = menu.items.map(item => {
return {
title: item.name,
price: item.price,
description
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock