justify/telegram-bot-api

该软件包已被废弃且不再维护。未建议替代软件包。

用于处理Telegram机器人API的简单库

v1.1.0 2019-03-11 13:46 UTC

This package is not auto-updated.

Last update: 2020-05-13 13:46:57 UTC


README

安装

composer require justify/telegram-bot-api

使用方法

创建客户端实例

define('TELEGRAM_TOKEN', 'Your token');

$bot = new Justify\TelegramBotApi\Client(TELEGRAM_TOKEN);

发送请求

您可以使用采集的方法发送请求

$bot->sendMessage($chatId, 'Hello!');
$bot->sendPhoto($chatId, 'File id || URL || path to file', [
    'caption' => 'My photo'
]);

几乎所有API方法都需要第一个参数 chat id 和最后一个 可选参数

如果您缺少内置方法,您可以通过 sendRequest 方法发送请求

$response = $bot->sendRequest('getFile', [
    'file_id' => $fileId
]);

处理请求

您可以使用 Justify\TelegramBotApi\Client 方法处理请求

$bot->handler(function ($message) use ($bot) {
    // Actions ...
});

您可以设置处理器的条件

// Will react on "start" command
$bot->command('start', function ($message) use ($bot) {
    // Actions ...
});

// Will react on "Some text" text
$bot->text('Some text', function ($message) use ($bot) {
    // Actions ...
});

// Will react if text messages will match to regexp pattern
$bot->regexp('Password (\d+)', function ($message, $matches) use ($bot) {
    // Actions ...
});

// Will react if message will have "photo" type
$bot->type('photo', function ($message) use ($bot) {
    // Actions ...
});
$

发送一些媒体

$bot->sendPhoto($chatId, 'Pass file id or file url or path to file');

发送媒体组

$photo = new Justify\TelegramBotApi\Types\InputMediaPhoto('Pass file id or file url or path to file', [
    'caption' => 'My photo'
]);
$video = new Justify\TelegramBotApi\Types\InputMediaVideo('Pass file id or file url or path to file', [
    'caption' => 'My video'
]);

$bot->sendMediaGroup($chatId, [$photo, $video]);

发送键盘

$keyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardMarkup([
    'resize_keyboard' => true,
    'ont_time_keyboard' => true
]);

$keyboard->row('1', '2', '3')
    ->row('4', '5', '6')
    ->row('7', '8', '9')
    ->row('0');

$bot->sendMessage($chatId, 'Choose a number', ['reply_markup' => $keyboard]);


// Removing keyboard
$removeKeyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardRemove();
$bot->sendMessage($chatId, 'Removing keyboard', ['reply_markup' => $removeKeyboard]);

运行应用程序

$bot->polling();

使用示例

require_once __DIR__ . '/vendor/autoload.php';

define('TELEGRAM_TOKEN', 'Pass your token here');

$bot = new Justify\TelegramBotApi\Client(TELEGRAM_TOKEN);

$bot->command('start', function ($message) use ($bot) {
    $keyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardMarkup([
        'resize_keyboard' => true
    ]);
    $keyboard->row('Photo', 'Audio')
        ->row('Document', 'Video')
        ->row('/stop');

    $bot->sendMessage($message->chat->id, 'Welcome!', ['reply_markup' => $keyboard]);
});

$bot->command('stop', function ($message) use ($bot) {
    $bot->sendMessage($message->chat->id, 'Completed', [
        'reply_markup' => new Justify\TelegramBotApi\Types\ReplyKeyboardRemove()
    ]);
});

$bot->text('Photo', function ($message) use ($bot) {
    $bot->sendPhoto($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Audio', function ($message) use ($bot) {
    $bot->sendAudio($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Document', function ($message) use ($bot) {
    $bot->sendDocument($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Video', function ($message) use ($bot) {
    $bot->sendVideo($message->chat->id, 'File id || URL || path to file');
});

try {
    $bot->polling();
} catch (Justify\TelegramBotApi\Exception $e) {
    echo $e->getMessage();
}