我想使用laravel的电报通知频道在17:00向特定用户发送电报消息,但是我似乎无法解决。我目前使用cmd命令进行测试,但是不断出错并且不知道该怎么办。
这是我的命令和通知文件:
SendNotification.php
<?php
namespace Rogier\Lab\Console;
use Illuminate\Console\Command;
use Rogier\Lab\Notifications\DailyTelegram;
class SendNotifications extends Command
{
protected $name = 'lab:notifications:send';
protected $description = 'Send notifications';
protected $userid = 919871501;
/**
* Execute the console command.
* @return void
*/
public function handle()
{
$this->output->writeln('Sending notifications');
$notification = new DailyTelegram($this->userid);
$notification->via()->toTelegram();
$this->output->writeln('Done');
}
}
和DailyTelegram.php
<?php
namespace Rogier\Lab\Notifications;
use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Notifiable;
class DailyTelegram extends Notification
{
protected $userid = 919871501;
public function via()
{
return [TelegramChannel::class];
}
public function toTelegram()
{
return TelegramMessage::create()
// Optional recipient user id.
->to($this->userid)
// Markdown supported.
->content("Hello there!\nYour invoice has been *PAID*");
}
}
我当前收到错误消息“在数组上调用成员函数toTelegram()”,但我感觉我尝试了所有操作,也许我做错了。有人知道我该怎么做吗?
预先感谢
是,您做错了。通知者具有notify()
方法,您应该使用它:
$user->notify(new DailyTelegram);
在此示例中,$user
是App\User
实例(开箱即用通知)。
您应该同时签出Laravel's sending notifications和laravel-notification-channels/telegram文档。