mavinoo/telegrambot

Telegram 通知驱动程序

dev-master 2017-08-28 10:16 UTC

This package is auto-updated.

Last update: 2024-09-15 17:26:58 UTC


README

Latest Version on Packagist Software License Total Downloads

此包可以轻松使用 Laravel 5.3 通过 Telegram Bot API 发送 Telegram 通知。

内容

安装

你可以通过 composer 安装此包

composer require mavinoo/telegrambot:dev-master

你必须安装服务提供者

// config/app.php
'providers' => [
    ...
    NotificationChannelsPlus\Telegrambot\TelegrambotServiceProvider::class,
],

设置你的 Telegram Bot

@BotFather 交流并生成一个 Bot API 令牌。

然后,配置你的 Telegram Bot API 令牌

// config/services.php
...
'telegram-bot-api' => [
    'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
],
...

使用 sendMessage

现在你可以在通知类中的 via() 方法中使用此频道。

use NotificationChannelsPlus\Telegrambot\TelegramChannel;
use NotificationChannelsPlus\Telegrambot\TelegramMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [TelegramChannel::class];
    }

    public function toTelegram($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        $tg = TelegramMessage::create()
		    ->to($this->user->telegram_user_id)
		    ->content("*HELLO!* \n One of your invoices has been paid!")
		    ->button('View Invoice', $url)
		    ->getResult();
	return $tg;
    }
}

使用 sendPhoto

现在你可以在通知类中的 via() 方法中使用此频道。

use NotificationChannelsPlus\Telegrambot\TelegramChannel;
use NotificationChannelsPlus\Telegrambot\TelegramMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [TelegramChannel::class];
    }

    public function toTelegram($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        $tg = TelegramMessage::create()
		    ->to($this->user->telegram_user_id)
		    ->sendPhoto([
			'caption'   =>  'Hello Mohammad',
			'photo'     =>  'http://www.ilovegenerator.com/large/i-love-mohamed-132309992962.png',
		    ])
		    ->button('View Invoice', $url)
		    ->getResult();
	    
	return $tg;
    }
}

替代方案

对于高级使用,请考虑使用 telegram-bot-sdk 代替。

许可

MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。