Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laklaker's avatar

Laravel Messenger Integration

TL;DR, Laravel app has service records for customers. Meanwhile, the business and the customer utilizes messenger for communication and updates. Mainly need to have Laravel automate and send some reminders, messages, or files to a messenger group chat instead of the users manually sending.

Context: I have a requirement where currently, the business staff + customer, interact inside messenger group chats.

  1. A group chat is created by an admin, with a customer, an "agent", and a "reviewer".

  2. The customer submits some files (requirements) to the group chat.

  3. The agent will also submit previous records of the customer.

  4. The reviewer checks the submitted files.

  5. Once reviewed, that reviewer exits the group, and a new user is added - the "approver".

  6. Same goes, once approved, another set of users will be added depending on the current stage of the cycle, and unrelated users will exit as well.

  7. Periodically, the customer will attach files to the group chat, as an update to the service being provided by the business.

This group will be active until the service provided to the customer is complete. This group chat will be kept, and once the customer asks for a new service, they will use the same group chat, and repeat the steps above.

Basically the business communication must utilize messenger. And with this, I'd like to automate some of the steps with a Laravel app. Just like step #3 above, where the agent manually adds the customer's previous records, I'd like for the Laravel app to send the data instead, since these data are already from the customer's history/record DB tables. Send reminders based on saved schedules per customer.

Another thing if possible, is to actually parse or download the files submitted to the group chat, and process it in the laravel app.

That said, Is there a better way to integrate messenger-sending to laravel? I searched for "laravel facebook messenger", hoping there are packages available, but either I get old results for old packages that are no longer maintained, or articles about socialite and login. I'm unable to find actual messenger+laravel integration.

Hopefully someone can point me to the right direction and thanks in advance

0 likes
1 reply
LaryAI's avatar
Level 58

Integrating Facebook Messenger with a Laravel application for automation purposes involves using the Facebook Messenger Platform. Unfortunately, as of my knowledge cutoff in early 2023, there isn't a widely-adopted Laravel package specifically designed for automating interactions with Messenger group chats. However, you can use the Facebook Graph API to send messages and manage group chats.

Here's a high-level approach to achieve your requirements:

  1. Setup Facebook App: Create a Facebook App in the Facebook Developer portal and get the necessary permissions to send messages on behalf of a page.

  2. Obtain Page Access Token: Get a page access token with the required scopes (pages_messaging and possibly others depending on your needs).

  3. Webhooks: Set up webhooks to listen for new messages or updates in the group chat.

  4. Send Messages: Use the Graph API to send messages to the group chat.

  5. Manage Group Chat: Use the Graph API to add or remove users from the group chat.

  6. File Uploads: Handle file uploads by listening to webhook events and using the Graph API to fetch attachments.

Here's a simplified example of how you might send a message using the Graph API in Laravel:

use Illuminate\Support\Facades\Http;

class MessengerService
{
    protected $pageAccessToken;

    public function __construct()
    {
        $this->pageAccessToken = 'YOUR_PAGE_ACCESS_TOKEN';
    }

    public function sendMessage($recipientId, $messageText)
    {
        $response = Http::post("https://graph.facebook.com/v14.0/me/messages", [
            'recipient' => [
                'id' => $recipientId
            ],
            'message' => [
                'text' => $messageText
            ],
            'access_token' => $this->pageAccessToken,
        ]);

        return $response->json();
    }
}

To automate the process, you could set up scheduled tasks (cron jobs) in Laravel that trigger these actions at specific times or under certain conditions.

For file parsing or downloading, you would need to handle webhook events that indicate a file has been uploaded, then use the Graph API to retrieve the file.

Remember that working with the Facebook API requires careful attention to permissions, authentication, and rate limits. Always ensure you're compliant with Facebook's Platform Policies and Data Use Policy.

Since this is a complex integration, you may need to write a lot of custom code or look for a third-party service that can handle some of these interactions for you. Keep in mind that Facebook's APIs and policies can change, so you'll need to stay updated with their documentation and announcements.

Lastly, always secure your application by keeping your access tokens and app secrets confidential, and validate all incoming requests from webhooks to ensure they're from Facebook.

Please or to participate in this conversation.