DEV Community

Rafi Halilintar
Rafi Halilintar

Posted on

How to Create a WhatsApp Chatbot Using WhatsApp API in Laravel

Image description

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)

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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=
Enter fullscreen mode Exit fullscreen mode

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']);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

  • Log in to your CrunchzApp account.
  • Navigate to Channel > [Your Channel] > Webhook.
  • 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:

  • Channel status
  • 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
}
Enter fullscreen mode Exit fullscreen mode

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.

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

Top comments (0)