laravel-notification-channels / telegram

✈️ Telegram Notifications Channel for Laravel
https://laravel-notification-channels.com/telegram/
MIT License
989 stars 165 forks source link

Cannot get message_id of sent message #182

Closed MrWeb closed 1 year ago

MrWeb commented 1 year ago

Per this Telegram documentation (https://telegram-bot-sdk.readme.io/reference/sendmessage) I should be able to send a message from the bot and get the message_id of the message.

When I try to do it I get a Undefined method 'getMessageId' error:

$msg = TelegramMessage::create();

\Log::channel('telegram')->info(['data_my' => $msg->getMessageId()]);

Is there a way to get the message_id of a just sent message (from the bot, not sent from the user)?

irazasyed commented 1 year ago

You should use notification events, more on this in the docs: handling response.

Also, those docs you've mentioned are not related to this package.

MrWeb commented 1 year ago

Wow thank you @irazasyed for the fast response. I referred to that docs as they are linked inside "Available Methods" section.

Ok I'll have a look at the Laravel 10 version (https://laravel.com/docs/10.x/notifications#notification-events) and see what I can come up with. Tku very much.

irazasyed commented 1 year ago

No problem :)

It's actually linked to refer supported parameters which can be used with the options method to pass any additional params.

Probably should just replace the link to official documentation. Thanks for highlighting.

irazasyed commented 1 year ago

Updated links!

MrWeb commented 1 year ago

I have trouble in implementing it. So do I have to create a TelegramMessageSentEvent event, then add it to EventServiceProvider inside $listen array like this with a listener connected:

protected $listen = [
    //other methods
    TelegramMessageSentEvent::class => [
        TelegramNotificationStatus::class
    ]
];

Then inside Notifications\TelegramNotification add the event() helper:

public function toTelegram()
{
    $msg = TelegramMessage::create();

    if ($this->reply_to_message_id) {
        $msg = $msg->options(['reply_to_message_id' => $this->reply_to_message_id]);
    }

    event(new TelegramMessageSentEvent($msg));

    return $msg->content($this->text);
}

I'm lost what to do inside TelegramMessageSentEvent::class and TelegramNotificationStatus::class :(

irazasyed commented 1 year ago

No. You need to listen to the NotificationSent event.

// EventServiceProvider.php
use App\Listeners\LogTelegramNotification;
use Illuminate\Notifications\Events\NotificationSent;

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    NotificationSent::class => [
        LogTelegramNotification::class,
    ],
];
// LogTelegramNotification

/**
 * Handle the event.
 */
public function handle(NotificationSent $event): void
{
    // $event->channel
    // $event->notifiable
    // $event->notification
    // $event->response
}

There is no need for you to dispatch or do anything. Just listen to the standard notification sent event and you'll get complete data in your listener such as the channel, notifiable, the response from Telegram (or any other channel -- you should filter based on this available data).

irazasyed commented 1 year ago

There's also Notification Failed event which if you listen to that event will get some useful data to help you debug the issues or take appropriate action. https://github.com/laravel-notification-channels/telegram/blob/master/src/TelegramChannel.php#L59-L63

MrWeb commented 1 year ago

Amazing, I was able to log the message_id !

Now I noticed the API have the deleteMessage('message_id') method, but I don't see them in this package. Did I missed them?

I would like to delete a specific message after some seconds, so once is sent, inside LogTelegramNotification::class I can probably just do:


public function handle(NotificationSent $event): void
{
    //some logic
    sleep(60);
    deleteMessage($event->response['result']['message_id']);
}
irazasyed commented 1 year ago

That's not supported. You'll have to either use the Telegram Bot SDK for that or create a helper yourself. The core idea of this channel is to deliver one-way notifications.

The channel cannot be used to support full Telegram API methods as that's not the purpose of this package.