gildonei's avatar

Laravel 9 - 3rd party API Integration

Hello all, I am starting with laravel and trying to find the correct way to build an integration with any 3rd party API.

My website will need to send scheduled WhatsApp messages waiting user to click on reply link to confirm or not the his choice (it will by like click on link "A" to agree or in link "B" to disagree), then I will update his choice and send another WhatsApp message confirming it.

Here is my point, I would like to create a Service or Helper or Facede or anything else that I could use in the Command (used by cron scheduler) or in a controller to send the response to users confirmation / cancelation.

I hope you can give some right paths to do this kind of integration

0 likes
6 replies
mvd's avatar

Hi @gildonei

So basically you want to use WhatsApp functionality?

What I would do:

  • If I need to use a specific company like MessageBird or Twillio for example, I will search if there is already a Laravel package for this company .
  • Otherwise, if I don't need a specific company, I will search for 'Laravel Whatsapp'
  • If you can not find a Laravel package, search services/companies who offer Whatsapp. Check if they have a PHP API and use their code (it doesn't have to be made specifically for Laravel, you can also use there classes instead of a Laravel Helper or Facede, it's not required).

If you find a package:

  • Check if it's not outdated, reviews, support (tickets), how many users are using the package, etc.
  • Optional/if you have time: if you can not find a Laravel package but there is a PHP code/library, can you make a laravel package for this.
gildonei's avatar

Hello @mvd , thank you for the suggestions, but there is no package.

The API is provided by https://mais.chat, a small Brazilian company, and I want to develop this package, and Laravel integration.

That's why I am asking the best way to do this integration with Laravel 9

mvd's avatar

@gildonei

There is no right or wrong way. The benefit of writing a package, for example, is that others can use it too.

So you can:

gildonei's avatar
gildonei
OP
Best Answer
Level 1

@mvd I found what I was looking for here in this link - https://www.twilio.com/pt-br/blog/como-criar-um-canal-de-notificacaoo-laravel-para-whatsapp-com-twilio.

It was not a package or vendor, I develop a new custom notification channel, doing it the right way, creating the ServiceProviders, Channels, Notification, settuping the credentials on .ENV and services, etc.

This way, I could send the confirmation and cancelation status, as well start the conversation using the notifications.

friedaritter's avatar

Laravel doesn't have built-in support for WhatsApp API, so you'll need to use a third-party package. Twilio, Nexmo, or other providers may offer solutions. Install the chosen package using Composer. Example (for Twilio): composer require twilio/sdk

Set up the configuration for your WhatsApp API in Laravel. This usually involves adding credentials and other necessary information to your .env file.

Now, create a service or helper class that encapsulates the logic for interacting with the WhatsApp API. This class should have methods for sending scheduled messages, handling user responses, and updating choices.

Example structure: // app/Services/WhatsAppService.php namespace App\Services;

class WhatsAppService { public function sendScheduledMessage($phoneNumber, $message, $scheduledTime) { // Logic to send scheduled message }

public function handleUserResponse($phoneNumber, $userChoice)
{
    // Logic to handle user response
}

}

charlesreily's avatar

Create a service class that will handle the communication with the third-party API. This class can be responsible for sending messages, updating user choices, etc. For example: // app/Services/WhatsAppService.php

namespace App\Services;

use Twilio\Rest\Client;

class WhatsAppService { protected $client;

public function __construct()
{
    $this->client = new Client(config('twilio.sid'), config('twilio.token'));
}

public function sendWhatsAppMessage($to, $message)
{
    // Use Twilio API to send WhatsApp messages
    $this->client->messages->create(
        "whatsapp:" . $to,
        [
            "from" => "whatsapp:" . config('twilio.from'),
            "body" => $message,
        ]
    );
}

// Add more methods as needed for your integration

}

Please or to participate in this conversation.