In today’s fast-paced world, businesses and developers increasingly rely on WhatsApp for communication and customer engagement. With the CrunchzApp – Laravel PHP Package, you can integrate WhatsApp messaging into your Laravel applications without the need for a WhatsApp Business account. This tutorial will guide you through the steps to send messages via the WhatsApp API using CrunchzApp.
Why Use CrunchzApp for WhatsApp Integration?
CrunchzApp simplifies the process of sending WhatsApp messages by allowing you to use your existing WhatsApp account through an easy-to-use API. It eliminates the need for a WhatsApp Business account, which often has complex restrictions and charges per message.
By using the CrunchzApp – Laravel PHP Package, you can automate WhatsApp interactions in your Laravel applications efficiently, whether it’s for customer support, notifications, or marketing.
Prerequisites
Before you begin, ensure you have the following:
- A Laravel 8.x or higher application
- PHP 7.3 or higher
- Composer installed
- A CrunchzApp account and API Token
Step 1: Install the CrunchzApp Laravel PHP Package
To integrate WhatsApp messaging into your Laravel app, start by installing the CrunchzApp Laravel PHP Package using Composer:
composer require crunchzapp/crunchzapp-php-sdk
This will add the package to your Laravel project, making it available for use in your application.
Step 2: Configuration
After installing the package, you’ll need to configure it to use your CrunchzApp API key. First, publish the configuration file:
php artisan vendor:publish --tag=crunchzapp-config
Then, add your API Token in the .env
file of your Laravel project:
CRUNCHZAPP_CHANNEL_TOKEN=
CRUNCHZAPP_GLOBAL_TOKEN=
CRUNCHZAPP_GLOBAL_OTP=
Step 3: Sending a WhatsApp Message
Once the setup is complete, you can start sending WhatsApp messages using the CrunchzApp Laravel package. Here’s an example of how to send a message to a specified phone number using 2 kind of approach:
1. Parallel Method
<?php
namespace App\Http\Controllers;
use CrunchzApp\CrunchzApp;
class CrunchzAppController extends Controller
{
/**
* @throws \Exception
*/
public function index()
{
return CrunchzApp::channel()
->checkPhoneNumber(
phoneNumber: '6281357541790',
toVariable: true
)
->startTyping()
->text(
message: 'Hi there, what up dude?!'
)
->stopTyping()
->sendPool();
}
}
The code above will check the phone number and then get the response from endpoint checkPhoneNumber contact_id into variable. And then make this programmatic things into natural scheme by adding startTyping and stopTyping before and after the message is sent.
This approach known as Parallel method which you can combine any method available inside this package. Below are the example what are the return of that example.
[
{
"path": "/send-message/typing",
"body": {
"contact_id": "[email protected]"
},
"result": {
"success": true,
"message": "OK",
"data": {
"typing": true
}
}
},
{
"path": "/send-message/text",
"body": {
"contact_id": "[email protected]",
"message": "Hi there, what up dude?!"
},
"result": {
"success": true,
"message": "OK",
"data": {
"status": "PENDING",
"message": "Hi there, what up dude?!",
"timestamp": "1726388696"
}
}
},
{
"path": "/send-message/stop-typing",
"body": {
"contact_id": "[email protected]"
},
"result": {
"success": true,
"message": "OK",
"data": {
"typing": false
}
}
}
]
2. Single Method
<?php
namespace App\Http\Controllers;
use CrunchzApp\CrunchzApp;
class CrunchzAppController extends Controller
{
/**
* @throws \Exception
*/
public function index()
{
return CrunchzApp::channel()
->checkPhoneNumber(
phoneNumber: '6281357541790',
toVariable: true
)
->text(
message: 'Hi there, what up dude?!'
)
->send();
}
}
The code above is the same like before but instead of having multiple request, it just only have one request that are showed which is text message .
This approach known as Single Method and you can only have 1 request at the time ( checkPhoneNumber are not counted as a request as the parameter toVariable is set to true ). Below are the example result of Single Method.
{
"success": true,
"message": "OK",
"data": {
"status": "PENDING",
"message": "Hi there, what up dude?!",
"timestamp": "1726388818"
}
}
Best Practices for WhatsApp API Integration
- Use Environment Variables: Always store your API keys in the
.env
file for security reasons. - Handle Errors Gracefully: Ensure your application properly handles errors such as message failures or invalid phone numbers.
- Optimize API Calls: Only send WhatsApp messages when necessary to avoid overwhelming your users with too many notifications.
- Respect Privacy: Follow best practices when using WhatsApp for messaging to ensure that user data is protected and you’re complying with privacy laws.
Step 5: Explore More Features
The CrunchzApp – Laravel PHP Package offers several other powerful features like message history retrieval, account information, and automating multiple WhatsApp tasks. You can explore these in the official documentation.
Conclusion
Integrating WhatsApp messaging into your Laravel applications has never been easier. With the CrunchzApp – Laravel PHP Package, you can send messages, check statuses, and automate interactions using a simple and flexible API.
To learn more and explore advanced features, visit the CrunchzApp documentation. Start enhancing your customer communication and engagement today with WhatsApp automation!
By following this tutorial, you can easily integrate WhatsApp API into your Laravel applications and optimize the way you communicate with your users.