Coder Social home page Coder Social logo

Comments (11)

filippotoso avatar filippotoso commented on September 21, 2024

Which Messaging Service are you using?

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

Which Messaging Service are you using?

Are you talking about the driver? In that case it's web driver initially, next it will be Facebook messenger driver.

from botman.

filippotoso avatar filippotoso commented on September 21, 2024

Try using this example as starting point to make an instance working with the Web Driver:

#1308 (comment)

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

How do I pass the message to the user id or chat session?

from botman.

filippotoso avatar filippotoso commented on September 21, 2024

Almost all BotMan drivers send messages in response to an HTTP request. That request contains all the information that BotMan needs to send the response, including the user id or chat session. So, to answer a user message you only need:

$botman->hears('Hi', function (BotMan $bot) {
    $bot->reply('Hello!');
});

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

Sorry, I think I may have misunderstood. I need to respond to a message from another server via a web service. What would I need to respond in a specific conversation?

from botman.

filippotoso avatar filippotoso commented on September 21, 2024

Using the current version of the web widget you cannot send a message to the user outside an HTTP request initiated by the user. To implement an async functionality you could use a websocket server or some kind of pulling system.

With services like Telegram and Messenger you can do it as follows:

  1. Instantiate the driver with the correct configuration
  2. Build the payload accordingly to the service (i.e. check Messenger official documentation). The payload will include data such as the message, the session user ID, etc.
  3. Call the sendPayload() method on the driver instance.

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

Thank you, I was using WebDriver for testing. I will use Facebook as you say

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

2. Messenger official documentation

I have this code, but it'snot working:

Messenger official documentation

Using the current version of the web widget you cannot send a message to the user outside an HTTP request initiated by the user. To implement an async functionality you could use a websocket server or some kind of pulling system.

With services like Telegram and Messenger you can do it as follows:

  1. Instantiate the driver with the correct configuration
  2. Build the payload accordingly to the service (i.e. check Messenger official documentation). The payload will include data such as the message, the session user ID, etc.
  3. Call the sendPayload() method on the driver instance.

I have this code, but it's not working:

try {
    $botman = app('botman');
    $accessToken = '***';
    $recipientId = '***';
    $payload = [
        'access_token' => $accessToken,
        'recipient' => ['id' => $recipientId],
        'message' => ['text' => 'Hello, world!']
    ];
    $botman->sendPayload($payload);
    Log::info('Payload sent successfully.');
    return response()->json(['status' => 'success']);
} catch (\Exception $e) {
    Log::error('Error sending payload: ' . $e->getMessage());
    return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
}

Laravel log doesn't show anything, only the successful sent message

from botman.

filippotoso avatar filippotoso commented on September 21, 2024

You are not following the provided instructions.

  1. You are not "instantiating the driver with the correct configuration", you are just creating an instance of the driver.

This is the driver you have to instantiate: https://github.com/botman/driver-facebook/blob/master/src/FacebookDriver.php

  1. Build the payload accordingly to the service

Here you can find an example of payload: https://github.com/botman/driver-facebook/blob/master/src/FacebookDriver.php#L370

As you can see, and as per Messenger documentation, you are missing fields like messaging_type.

  1. Call the sendPayload() method on the driver instance.

You are calling the sendPayload() method on BotMan not on the driver.

from botman.

dimar7 avatar dimar7 commented on September 21, 2024

using the driver instance, should it be something like this?:

    public function handle()
    {
        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('/botman', [BotManController::class, 'handle']);

from botman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.