Buildingiline Bots on Messenger/WhatsApp

phamtinhnhi

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àng tỷ người dùng hoạt động.Bots là một cách tuyệt vời để thêm chức năng cho các ứng dụng này và tương tác với người dùng của bạn theo cách hấp dẫn hơ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 đây:

* Môi trường phát triển Python (chúng tôi đề nghị [Pycharm] (https://www.jetbrains.com/pycharm/)))
* [API bot] (https://developers.facebook.com/docs/messenger-platform/getting-started/) cho messenger hoặc [whatsapp kinh doanh API] (https://developers.facebook.com/docs/whatsapp/kinh doanh-api/bắt đầu/) cho whatsapp
* The [Flask] (https://flask.palletsprojects.com/en/2.1.x/) Khung Web
* [Yêu cầu] (https://requests.readthedocs.io/en/master/) Thư viện

## Tạo bot

Bước đầu tiên là tạo bot.Bạn có thể làm điều này bằng cách làm theo các hướng dẫn trong [Messenger Bot QuickStart] (https://developers.facebook.com/docs/messenger-platform/getting-started/) hoặc [API kinh doanh WhatsApp] (https: //nhà phát triển.facebook.com/docs/whatsapp/business-api/getting-started/).

Khi bạn đã tạo bot, bạn sẽ cần phải có mã thông báo ** của nó **.Mã thông báo này được sử dụng để xác thực bot của bạn khi nó thực hiện các yêu cầu cho API Messenger hoặc WhatsApp.

## Gửi tin nhắn

Bây giờ bạn đã tạo một bot và có được mã thông báo truy cập của nó, bạn có thể bắt đầu gửi tin nhắn.Để làm điều này, bạn sẽ cần sử dụng [API Bot] (https://developers.facebook.com/docs/messenger-platform/send-messages/) cho Messenger hoặc [WhatsApp Business API] (https: //nhà phát triển.facebook.com/docs/whatsapp/business-api/send-messages/) cho whatsapp.

Mã sau đây cho thấy cách gửi tin nhắn bằng API bot:

`` `Python
Nhập yêu cầu

# Nhận mã thông báo truy cập bot của bạn.
Access_token = 'your_access_token'

# Tạo một đối tượng tin nhắn.
message = {
'Văn bản': 'Xin chào, Thế giới!'
}

# Gửi tin nhắn.
Trả lời = Yêu cầu.post (
'https://graph.facebook.com/v2.6/me/messages',
params = {'access_token': access_token},
json = tin nhắn
)

# In phản hồi.
in (phản hồi.status_code)
in (phản hồi.json ())
`` `

## Nhận tin nhắn

Ngoài việc gửi tin nhắn, bạn cũng có thể nhận tin nhắn từ người dùng của mình.Để làm điều này, bạn sẽ cần thiết lập một webhook.Một webhook là một URL được gọi khi bot của bạn nhận được tin nhắn.

Để thiết lập một webhook, bạn có thể sử dụng [API BOT] (https://developers.facebook.com/docs/messenger-platform/webhooks/) cho Messenger hoặc [API kinh doanh WhatsApp] (https: // phát triển.facebook.com/docs/whatsapp/business-api/webhooks/) cho whatsapp.

Mã sau đây cho thấy cách thiết lập webhook bằng API bot:

`` `Python
Nhập yêu cầu

# Nhận url webhook của bot của bạn.
Webhook_url = 'https://your-domain.com/webhook'

# Tạo một trình xử lý yêu cầu webhook.
Def Webhook_Handler (Yêu cầu):
# Nhận tải trọng tin nhắn.
POADLOAD = request.get_json ()

# Gửi phản hồi cho người gửi.
Trả lời = {
'Văn bản': 'Xin chào, Thế giới!'
}

# Trả lại phản hồi.
yêu cầu trả lại.post (
'https: // đồ thị.
=======================================
#MessengerBot #WhatsAppBot #ChatBot #bot #build #Chat **Build a Messenger/WhatsApp Bot with Python**

Messenger and WhatsApp are two of the most popular messaging apps in the world, with billions of active users. Bots are a great way to add functionality to these apps and interact with your users in a more engaging way. In this tutorial, we will show you how to build a simple Messenger/WhatsApp bot with Python.

## Prerequisites

To follow this tutorial, you will need the following:

* A Python development environment (we recommend [PyCharm](https://www.jetbrains.com/pycharm/))
* The [Bot API](https://developers.facebook.com/docs/messenger-platform/getting-started/) for Messenger or the [WhatsApp Business API](https://developers.facebook.com/docs/whatsapp/business-api/getting-started/) for WhatsApp
* The [Flask](https://flask.palletsprojects.com/en/2.1.x/) web framework
* The [requests](https://requests.readthedocs.io/en/master/) library

## Creating a Bot

The first step is to create a bot. You can do this by following the instructions in the [Messenger Bot Quickstart](https://developers.facebook.com/docs/messenger-platform/getting-started/) or the [WhatsApp Business API Quickstart](https://developers.facebook.com/docs/whatsapp/business-api/getting-started/).

Once you have created a bot, you will need to get its **access token**. This token is used to authenticate your bot when it makes requests to the Messenger or WhatsApp APIs.

## Sending Messages

Now that you have created a bot and obtained its access token, you can start sending messages. To do this, you will need to use the [Bot API](https://developers.facebook.com/docs/messenger-platform/send-messages/) for Messenger or the [WhatsApp Business API](https://developers.facebook.com/docs/whatsapp/business-api/send-messages/) for WhatsApp.

The following code shows how to send a message using the Bot API:

```python
import requests

# Get your bot's access token.
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'

# Create a message object.
message = {
'text': 'Hello, world!'
}

# Send the message.
response = requests.post(
'https://graph.facebook.com/v2.6/me/messages',
params={'access_token': ACCESS_TOKEN},
json=message
)

# Print the response.
print(response.status_code)
print(response.json())
```

## Receiving Messages

In addition to sending messages, you can also receive messages from your users. To do this, you will need to set up a webhook. A webhook is a URL that is called when your bot receives a message.

To set up a webhook, you can use the [Bot API](https://developers.facebook.com/docs/messenger-platform/webhooks/) for Messenger or the [WhatsApp Business API](https://developers.facebook.com/docs/whatsapp/business-api/webhooks/) for WhatsApp.

The following code shows how to set up a webhook using the Bot API:

```python
import requests

# Get your bot's webhook URL.
WEBHOOK_URL = 'https://your-domain.com/webhook'

# Create a webhook request handler.
def webhook_handler(request):
# Get the message payload.
payload = request.get_json()

# Send a response to the sender.
response = {
'text': 'Hello, world!'
}

# Return the response.
return requests.post(
'https://graph.
 
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