tje3d / telegram
1.3.1
2017-05-06 21:50 UTC
Requires
- php: >=5.5
- guzzlehttp/guzzle: ^6.2
This package is not auto-updated.
Last update: 2024-09-24 10:23:13 UTC
README
PHP Telegram Bot API,支持Laravel。
安装
composer require tje3d/telegram
仅Laravel
服务提供者
Tje3d\Telegram\Laravel\TelegramServiceProvider::class
外观
'Bot' => Tje3d\Telegram\Laravel\Facades\Bot::class
发布资源
artisan vendor:publish --tag=telegram
将创建一个名为telegram的配置文件(config/telegram.php)
示例
✔️ 初始化机器人
$bot = new \Tje3d\Telegram\Bot($token);
$info = $bot->getMe();
print_r($info);
✔️ 设置Web钩子
$bot->setWebhook('https://sld.tld');
✔️ 获取更新
$response = $bot->getUpdates();
...
$response = $bot->getUpdates($offset=0, $limit=100, $timeout=10); // Long pull
✔️ 发送文本消息(称为sendMessage)
$bot->sendMethod(
(new \Tje3d\Telegram\Methods\Text())
->text('test')
->chat_id($chatId)
);
⭐️ 或以数组的形式传递配置
$bot->sendMethod(
(new Tje3d\Telegram\Methods\Text(['text' => 'hi', 'chat_id' => $chatId]))
);
✔️ 键盘
⭐️ 回复键盘
$bot->sendMethod(
(new Tje3d\Telegram\Methods\Text)
->text('My Sample Text')
->chat_id($chatId)
->reply_markup(
(new Tje3d\Telegram\Markups\ReplayKeyboardMarkup)
->row(function($handler){
$handler->addButton(['text' => 'btn1']);
$handler->addButton(['text' => 'my special button ⭐️']);
})
->row(function($handler){
$handler->addButton(['text' => 'WOW']);
})
->row(function($handler){
$handler->addButton(['text' => 'Hey this is third line!']);
})
->row(function($handler){
$handler->addButton(['text' => '1']);
$handler->addButton(['text' => '2']);
$handler->addButton(['text' => '3']);
$handler->addButton(['text' => '4']);
})
)
);
⭐️ 内联键盘
$bot->sendMethod(
(new Tje3d\Telegram\Methods\Text)
->text('My Sample Text')
->chat_id($testChatId)
->reply_markup(
(new Tje3d\Telegram\Markups\InlineKeyboardMarkup)
->row(function($handler){
$handler->addButton(['text' => 'btn1', 'url' => 'http://sld.tld']);
$handler->addButton(['text' => 'my special button ⭐️', 'url' => 'http://sld.tld']);
})
->row(function($handler){
$handler->addButton(['text' => 'WOW', 'callback_data' => 'doSomethingSpecial']);
})
)
);
✔️ 图片、音频、视频、文档...
$bot->sendMethod(
(new \Tje3d\Telegram\Methods\Photo)
->chat_id($chatId)
->photo(realpath('pic.png'))
);
...
$bot->sendMethod(
(new \Tje3d\Telegram\Methods\Video)
->chat_id($chatId)
->video(realpath('video.mp4'))
->duration(10) // optional
->width(320) // optional
->height(320) // optional
);
...
$bot->sendMethod(
(new \Tje3d\Telegram\Methods\Audio)
->chat_id($chatId)
->audio(realpath('video.mp3'))
->duration(30) // optional
->performer('tje3d') // optional
->title('Great music') // optional
);
...
✔️ 聊天动作
$bot->sendMethod(
(new \Tje3d\Telegram\Methods\ChatAction)
->chat_id($testChatId)
->typing() // Could be: upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location
);
异常
如果sendMethod失败,则抛出Tje3d\Telegram\Exceptions\TelegramResponseException。
try {
$bot = new \Tje3d\Telegram\Bot($token);
$response = $bot->sendMethod(
(new \Tje3d\Telegram\Methods\Text())
->text($text)
->chat_id($chatId)
);
} catch (TelegramResponseException $e) {
print_r($e->response());
}