<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rafi Halilintar</title>
    <description>The latest articles on DEV Community by Rafi Halilintar (@rafihalilintar).</description>
    <link>https://dev.to/rafihalilintar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2516731%2Feeac1ea6-13c4-4e92-9aab-253771d39320.jpg</url>
      <title>DEV Community: Rafi Halilintar</title>
      <link>https://dev.to/rafihalilintar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rafihalilintar"/>
    <language>en</language>
    <item>
      <title>How to Create a WhatsApp Chatbot Using WhatsApp API in Laravel</title>
      <dc:creator>Rafi Halilintar</dc:creator>
      <pubDate>Tue, 03 Dec 2024 10:03:40 +0000</pubDate>
      <link>https://dev.to/rafihalilintar/how-to-create-a-whatsapp-chatbot-using-whatsapp-api-in-laravel-29lp</link>
      <guid>https://dev.to/rafihalilintar/how-to-create-a-whatsapp-chatbot-using-whatsapp-api-in-laravel-29lp</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijzgeabxipdive71y9ce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijzgeabxipdive71y9ce.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll show you how to create a WhatsApp chatbot using the WhatsApp API in Laravel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting, ensure you have the following ready:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PHP (7.4 or above)&lt;/li&gt;
&lt;li&gt;Laravel (8.x or 9.x)&lt;/li&gt;
&lt;li&gt;A WhatsApp API (such as CrunchzApp)&lt;/li&gt;
&lt;li&gt;A WhatsApp Business Account (if using the official API)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Install Laravel
&lt;/h2&gt;

&lt;p&gt;If you haven’t installed Laravel yet, use this command to set up a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project --prefer-dist laravel/laravel whatsapp-bot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Install WhatsApp API SDK (CrunchzApp)
&lt;/h2&gt;

&lt;p&gt;To interact with WhatsApp, we’ll use CrunchzApp, a platform that enables you to automate messaging via WhatsApp. Install the SDK by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require crunchzapp/crunchzapp-php-sdk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Configure API Credentials
&lt;/h2&gt;

&lt;p&gt;Add your CrunchzApp API credentials in the .env file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRUNCHZAPP_CHANNEL_TOKEN=
CRUNCHZAPP_GLOBAL_TOKEN=
CRUNCHZAPP_GLOBAL_OTP=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get your API key and channel ID, register for an account at CrunchzApp.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Set Up Routes for the Chatbot
&lt;/h2&gt;

&lt;p&gt;To handle incoming messages, create a route in routes/web.php:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Http\Controllers\ChatBotController;

Route::post('/webhook', [ChatBotController::class, 'handleWebhook']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Create the ChatBotController
&lt;/h2&gt;

&lt;p&gt;Next, create a controller to process incoming messages and send automated replies. Use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:controller ChatBotController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside ChatBotController.php, implement the chatbot logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
namespace App\Http\Controllers;

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

class ChatBotController extends Controller
{
    public function handleWebhook(Request $request)
    {
        $message = $request-&amp;gt;input('body');
        $from = $request-&amp;gt;input('from');

        // Basic chatbot logic
        if ($message == 'hello') {
            $this-&amp;gt;sendMessage($from, 'Hello! How can I help you today?');
        } elseif ($message == 'help') {
            $this-&amp;gt;sendMessage($from, 'Here are the available commands...');
        } else {
            $this-&amp;gt;sendMessage($from, 'Sorry, I did not understand that. Type "help" for options.');
        }
    }

    private function sendMessage($contact, $text)
    {
        CrunchzApp::channel()
            -&amp;gt;contact($contact)
            -&amp;gt;text(message: $text)
            -&amp;gt;send();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: Configure Webhook in CrunchzApp
&lt;/h2&gt;

&lt;p&gt;In order to enable real-time messaging, you need to configure a webhook within your CrunchzApp dashboard.&lt;/p&gt;

&lt;p&gt;To do this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in to your CrunchzApp account.&lt;/li&gt;
&lt;li&gt;Navigate to Channel &amp;gt; [Your Channel] &amp;gt; Webhook.&lt;/li&gt;
&lt;li&gt;Set the webhook URL to your Laravel app’s /webhook route: ‘&lt;a href="https://yourdomain.com/webhook%E2%80%99" rel="noopener noreferrer"&gt;https://yourdomain.com/webhook’&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CrunchzApp allows you to register two types of webhook events:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Channel status&lt;/li&gt;
&lt;li&gt;Messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this chatbot, we’ll focus on the Messages event. When a message is received, the following payload will be sent to your webhook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "message_id": "false_xxx@c.us_xxx",
  "from": "xxx@c.us",
  "body": "hello",
  "status": "PENDING",
  "timestamp": 1234561221
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 7: Test Your WhatsApp Chatbot
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send “hello” → receive: “Hello! How can I help you today?”&lt;/li&gt;
&lt;li&gt;Send “help” → receive: “Here are the available commands…”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 8: Enhance Your Chatbot
&lt;/h2&gt;

&lt;p&gt;To take your chatbot further, consider implementing:&lt;/p&gt;

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

&lt;p&gt;Get started with a free 7-day trial at &lt;a href="https://www.crunchz.app" rel="noopener noreferrer"&gt;CrunchzApp&lt;/a&gt; and use your own WhatsApp number to integrate into your Laravel app!&lt;/p&gt;

</description>
      <category>whatsapp</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
