Buildingiline Bots on Messenger/WhatsApp

thuvongdo

New member
..

Messenger và WhatsApp là hai trong số các ứng dụng nhắn tin phổ biến nhất trên thế giới, với hơn 2 tỷ người dùng hoạt động.Điều này làm cho họ trở thành một nền tảng tuyệt vời để tiếp cận một lượng lớn khán giả với bot của bạn.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách xây dựng một bot Messenger/WhatsApp đơn giản với Python.

## Đ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:

* Một máy tính với Python 3 được cài đặt
* [SDK API cho Python] (https://github.com/botframework-python/botbuilder-python)
* Tài khoản nhà phát triển Facebook và tài khoản API kinh doanh WhatsApp

## Bắt đầu

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

`` `
$ python3 -m venv venv
`` `

Điều này sẽ tạo ra một môi trường ảo mới gọi là `venv`.Chúng ta có thể kích hoạt môi trường này bằng cách chạy lệnh sau:

`` `
$ Nguồn venv/bin/kích hoạt
`` `

Khi môi trường được kích hoạt, chúng ta có thể cài đặt SDK API Bot cho Python bằng cách chạy lệnh sau:

`` `
$ pip cài đặt botbuilder-python
`` `

## Tạo bot

Bây giờ chúng tôi đã cài đặt SDK API bot, chúng tôi có thể tạo một bot mới.Chúng ta có thể làm điều này bằng cách chạy lệnh sau:

`` `
$ botbuilder-python tạo-dự án mybot
`` `

Điều này sẽ tạo ra một dự án mới gọi là `mybot`.Dự án sẽ bao gồm một tệp `main.py`, đó là nơi chúng tôi sẽ viết mã cho bot của chúng tôi.

## mã hóa bot

Điều đầu tiên chúng ta cần làm là nhập SDK khung bot:

`` `Python
từ botbuilder.core nhập (
Người máy,
BotframeworkAdapter,
Botframeworkconfiguration,
Cuộc trò chuyện,
Bộ nhớ,
)
`` `

Tiếp theo, chúng ta cần tạo một bot mới:

`` `Python
lớp mybot (bot):

def __init __ (self, hội thoại_state: hội thoại, bộ điều hợp: botframeworkad CHƯƠNG):
Super (mybot, self) .__ init __ (hội thoại_state, bộ chuyển đổi)

# Thêm trình xử lý cho các tin nhắn mà bot của bạn sẽ nhận được.
self.add_handler (self.on_message)

async def on_message (self, tin nhắn: tin nhắn):
# Nhận văn bản của tin nhắn.
Text = message.text

# Nếu tin nhắn là một tin nhắn văn bản đơn giản, hãy trả lời bằng lời chào.
Nếu văn bản == "Xin chào":
Await self.send_message (message.conversation.id, "Xin chào!")

# Nếu không, tin nhắn là một tin nhắn phức tạp, vì vậy chúng tôi sẽ chỉ đăng nhập nó.
khác:
print (f "đã nhận được một thông báo phức tạp: {text}")
`` `

## chạy bot

Bây giờ chúng tôi đã mã hóa bot của chúng tôi, chúng tôi có thể chạy nó.Chúng ta có thể làm điều này bằng cách chạy lệnh sau trong cửa sổ thiết bị đầu cuối:

`` `
$ Python main.py
`` `

Điều này sẽ bắt đầu nghe bot nghe tin nhắn trên cổng 3978. Bây giờ bạn có thể gửi tin nhắn đến bot của mình bằng ứng dụng Messenger hoặc WhatsApp.

## kiểm tra bot

Bạn có thể kiểm tra bot của mình bằng trình giả lập khung bot.Trình giả lập là một công cụ miễn phí cho phép bạn kiểm tra bot trong môi trường mô phỏng.Để sử dụng trình giả lập, hãy tải xuống từ [trang web Bot Framework] (https://dev.botframework.com/).

Khi bạn đã cài đặt trình giả lập, hãy mở và chọn nút "mới".Trong hộp thoại xuất hiện, chọn "Từ mã" và nhập URL sau:

`` `
http: // localhost: 3978/api/tin nhắn
`` `

Điều này sẽ tạo ra một thể hiện bot mới trong trình giả lập.Bây giờ bạn có thể gửi tin nhắn đến bot của mình bằng cửa sổ trò chuyện của trình giả lập.

## triển khai bot

Khi bạn hài lòng với bot của mình, bạn có thể triển khai nó để sản xuất.Để làm điều này, bạn sẽ cần
=======================================
#MessengerBot #WhatsAppBot #ChatBot #bot #build ### Build a Messenger/WhatsApp Bot with Python

Messenger and WhatsApp are two of the most popular messaging apps in the world, with over 2 billion active users each. This makes them a great platform for reaching a large audience with your bot. In this tutorial, we'll show you how to build a simple Messenger/WhatsApp bot with Python.

## Prerequisites

To follow this tutorial, you'll need the following:

* A computer with Python 3 installed
* The [Bot API SDK for Python](https://github.com/botframework-python/botbuilder-python)
* A Facebook developer account and a WhatsApp Business API account

## Getting Started

First, we need to create a new project. We can do this by running the following command in a terminal window:

```
$ python3 -m venv venv
```

This will create a new virtual environment called `venv`. We can activate this environment by running the following command:

```
$ source venv/bin/activate
```

Once the environment is activated, we can install the Bot API SDK for Python by running the following command:

```
$ pip install botbuilder-python
```

## Creating a Bot

Now that we have the Bot API SDK installed, we can create a new bot. We can do this by running the following command:

```
$ botbuilder-python create --project mybot
```

This will create a new project called `mybot`. The project will include a `main.py` file, which is where we'll write the code for our bot.

## Coding the Bot

The first thing we need to do is import the Bot Framework SDK:

```python
from botbuilder.core import (
Bot,
BotFrameworkAdapter,
BotFrameworkConfiguration,
ConversationState,
MemoryStorage,
)
```

Next, we need to create a new bot:

```python
class MyBot(Bot):

def __init__(self, conversation_state: ConversationState, adapter: BotFrameworkAdapter):
super(MyBot, self).__init__(conversation_state, adapter)

# Add handlers for the messages that your bot will receive.
self.add_handler(self.on_message)

async def on_message(self, message: Message):
# Get the text of the message.
text = message.text

# If the message is a simple text message, reply with a greeting.
if text == "Hello":
await self.send_message(message.conversation.id, "Hello there!")

# Otherwise, the message is a complex message, so we'll just log it.
else:
print(f"Received a complex message: {text}")
```

## Running the Bot

Now that we've coded our bot, we can run it. We can do this by running the following command in the terminal window:

```
$ python main.py
```

This will start the bot listening for messages on port 3978. You can now send messages to your bot using the Messenger or WhatsApp app.

## Testing the Bot

You can test your bot using the Bot Framework Emulator. The emulator is a free tool that allows you to test bots in a simulated environment. To use the emulator, download it from the [Bot Framework website](https://dev.botframework.com/).

Once you've installed the emulator, open it and select the "New" button. In the dialog box that appears, select "From Code" and enter the following URL:

```
```

This will create a new bot instance in the emulator. You can now send messages to your bot using the emulator's chat window.

## Deploying the Bot

Once you're satisfied with your bot, you can deploy it to production. To do this, you'll need
 
Làm thế nào để trả lời các truy vấn của người dùng trên bot messenger trả lời các câu hỏi về sản phẩm?
 
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