Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending message using a Facebook driver instance #1403

Open
dimar7 opened this issue Apr 17, 2024 · 0 comments
Open

Sending message using a Facebook driver instance #1403

dimar7 opened this issue Apr 17, 2024 · 0 comments

Comments

@dimar7
Copy link

dimar7 commented Apr 17, 2024

  • BotMan Version: 2.8.1
  • PHP Version:7.4.12
  • Messaging Service(s):
  • Cache Driver:

Description:

I'm attempting to utilize the Facebook driver instance to send a message to a Facebook user, but it's not functioning as expected. I'm receiving the error message 'Failed to retrieve Facebook driver or driver is not an instance of FacebookDriver.' My intention is to send the message when I make a request to 'https://myserver/api/send-message' using Postman. When I do get_class($facebookDriver) it throws: BotMan\BotMan\Drivers\NullDriver"

namespace App\Http\Controllers;

use BotMan\BotMan\BotMan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use BotMan\Drivers\Facebook\FacebookDriver;

public function sendMessage()
    {
        try {
            $botman = app('botman');
            $facebookDriver = $botman->getDriver(\BotMan\Drivers\Facebook\FacebookDriver::class);
    
            if ($facebookDriver && $facebookDriver instanceof FacebookDriver) {
                $recipient = '1**';
                $message = 'hello world';
    
                $payload = [
                    'messaging_type' => 'RESPONSE',
                    'recipient' => [
                        'id' => $recipient
                    ],
                    'message' => [
                        'text' => $message
                    ]
                ];
    
                $facebookDriver->sendPayload($payload);
                Log::info('Payload sent successfully to recipient: ' . $recipient);
                
                return response()->json(['status' => 'success']);
            } else {
                Log::error('Failed to retrieve Facebook driver or driver is not an instance of FacebookDriver.');
                return response()->json(['status' => 'error', 'message' => 'Failed to retrieve Facebook driver.'], 500);
            }
        } catch (\Exception $e) {
            Log::error('Error sending payload: ' . $e->getMessage());
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
    }
}

api.php:

Route::post('/send-message', [BotManController::class, 'sendMessage']);

Steps To Reproduce:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant