How to Create a WhatsApp Chatbot Using WhatsApp API in Laravel

Rafi Halilintar
3 min readSep 16, 2024

--

WhatsApp chatbots are becoming essential for businesses looking to automate interactions with customers. With Laravel’s powerful framework and a WhatsApp API like CrunchzApp, you can build a chatbot to automatically handle messages, queries, and more.

In this guide, we’ll show you how to create a WhatsApp chatbot using the WhatsApp API in Laravel.

Prerequisites

Before starting, ensure you have the following ready:

  • PHP (7.4 or above)
  • Laravel (8.x or 9.x)
  • A WhatsApp API (such as CrunchzApp)
  • A WhatsApp Business Account (if using the official API)

Free Trial
Get started with a free 7-day trial at
CrunchzApp and use your own WhatsApp number to integrate into your Laravel app!

Step 1: Install Laravel

If you haven’t installed Laravel yet, use this command to set up a new project:

composer create-project --prefer-dist laravel/laravel whatsapp-bot

Step 2: Install WhatsApp API SDK (CrunchzApp)

To interact with WhatsApp, we’ll use CrunchzApp, a platform that enables you to automate messaging via WhatsApp. Install the SDK by running:

composer require crunchzapp/crunchzapp-php-sdk

Step 3: Configure API Credentials

Add your CrunchzApp API credentials in the .env file like this:

CRUNCHZAPP_CHANNEL_TOKEN=
CRUNCHZAPP_GLOBAL_TOKEN=
CRUNCHZAPP_GLOBAL_OTP=

To get your API key and channel ID, register for an account at CrunchzApp.

Step 4: Set Up Routes for the Chatbot

To handle incoming messages, create a route in routes/web.php:

use App\Http\Controllers\ChatBotController;

Route::post('/webhook', [ChatBotController::class, 'handleWebhook']);

Step 5: Create the ChatBotController

Next, create a controller to process incoming messages and send automated replies. Use the following command:

php artisan make:controller ChatBotController

Inside ChatBotController.php, implement the chatbot logic:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use CrunchzApp\CrunchzApp;

class ChatBotController extends Controller
{
public function handleWebhook(Request $request)
{
$message = $request->input('body');
$from = $request->input('from');
// Basic chatbot logic
if ($message == 'hello') {
$this->sendMessage($from, 'Hello! How can I help you today?');
} elseif ($message == 'help') {
$this->sendMessage($from, 'Here are the available commands...');
} else {
$this->sendMessage($from, 'Sorry, I did not understand that. Type "help" for options.');
}
}
private function sendMessage($contact, $text)
{
CrunchzApp::channel()
->contact($contact)
->text(message: $text)
->send();
}
}

Step 6: Configure Webhook in CrunchzApp

In order to enable real-time messaging, you need to configure a webhook within your CrunchzApp dashboard.

To do this:

  1. Log in to your CrunchzApp account.
  2. Navigate to Channel > [Your Channel] > Webhook.
  3. Set the webhook URL to your Laravel app’s /webhook route: 'https://yourdomain.com/webhook'

CrunchzApp allows you to register two types of webhook events:

  1. Channel status
  2. Messages

For this chatbot, we’ll focus on the Messages event. When a message is received, the following payload will be sent to your webhook:

{
"message_id": "false_xxx@c.us_xxx",
"from": "xxx@c.us",
"body": "hello",
"status": "PENDING",
"timestamp": 1234561221
}

Step 7: Test Your WhatsApp Chatbot

Now that your webhook is set up, you can test your chatbot by sending a message to the WhatsApp number linked to your API. If configured correctly, you should receive automated responses based on your chatbot’s logic.

For example:

  • Send “hello” → receive: “Hello! How can I help you today?”
  • Send “help” → receive: “Here are the available commands…”

Step 8: Enhance Your Chatbot

To take your chatbot further, consider implementing:

  • Natural Language Processing (NLP): Use services like Dialogflow to make your bot smarter and more interactive.
  • User Data Storage: Store user preferences in a database to provide personalized interactions.
  • Error Handling: Ensure the chatbot handles unexpected inputs gracefully.

Conclusion

You’ve just created a basic WhatsApp chatbot using the CrunchzApp API and Laravel! This is just the starting point — you can enhance it by adding more features and integrations. Remember, you can sign up for a free 7-day trial at CrunchzApp to start automating your WhatsApp messages today.

--

--

Rafi Halilintar
Rafi Halilintar

Written by Rafi Halilintar

I am Laravel enthusiast since 2017.

Responses (1)