polyskalov / botman-viber-driver

Viber driver for botman —  a PHP library to build chat bots
https://botman.io
MIT License
7 stars 5 forks source link

How to save viber_id of user to database? #3

Closed rajendrap123 closed 4 years ago

rajendrap123 commented 4 years ago

@polyskalov

Thanks for this great package.

I would like to know how to save users viber_ids to database, so that we can send messages to users later?

Can you please point me to right direction for it?

Cheers

polyskalov commented 4 years ago

in routes/botman.php

$botman->on(
    'conversation_started',
    static function (array $payload, BotMan $bot) {
        $viberId = $payload['user']['id'] ?? null;
    }
);
rajendrap123 commented 4 years ago

@polyskalov

Amazing man, greatly appreciated thank you very much for putting time for this help.

I have a small suggestion, can we have a viber notification channel to send notifications to system users to viber after they subscribe to feature?

rajendrap123 commented 4 years ago

Hello @polyskalov

I tried to register the event listener, the way you mentioned.

$botman->on( 'conversation_started', static function (array $payload, BotMan $bot) { $viberId = $payload['user']['id'] ?? null; } );

But events callback are not being called?

Can you please let me know what possible cause for it may be? I have checked the logs and not any log found it in.

rajendrap123 commented 4 years ago

Hello @polyskalov

I think i am getting this error on events:

[2020-06-12 08:54:08] production.ERROR: Trying to access array offset on value of type null {"exception":"[object] (ErrorException(code: 0): Trying to access array offset on value of type null at /var/www/aayo.express/aayosathi/vendor/polyskalov/botman-viber-driver/src/ViberDriver.php:163)

Can you pls look into it?

Thanks

rajendrap123 commented 4 years ago

@polyskalov

Can you please guide me in the right direction for the below point:

I am now able to get the viber id, but as i have laravel app, i want to associate this viber_id to App\User model?

How do we associate viber id to its user? any clue?

Thanks in advance.

rajendrap123 commented 4 years ago

Update on above code:

`$botman->on( 'conversation_started', static function (array $payload, BotMan $bot) { $viberId = $payload['user_id'] ?? null';

}

);`

polyskalov commented 4 years ago

Package updated with bug fixing Please check v1.1

I have a small suggestion, can we have a viber notification channel

Maybe later, no time for now

How to save to database

As I mentioned earlier, you can use the user ID from the event and then manipulate it as you wish. Add to the database, for example (User model need to have viber_id field):

$botman->on(
    'conversation_started',
    static function (array $payload, BotMan $bot) {
        $user = \App\User::updateOrCreate(
            [
                'viber_id' => $bot->getUser()->getId()
            ],
            [
                'name' => $bot->getUser()->getUsername()
            ]
        );
    }
);

($bot->getUser() is the replacement for $payload['user']['id'] ?? null)

rajendrap123 commented 4 years ago

thanks