paranoiasystem / telegrambot-php-library
TelegramBot PHP非官方库。
1.0.3
2015-07-27 10:30 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is auto-updated.
Last update: 2024-09-10 22:41:44 UTC
README
TelegramBot是PHP中用于使用Telegram机器人API的非官方库。
安装
通过Composer
$ composer require paranoiasystem/telegrambot-php-library
用法
发送消息
<?php namespace Telegram; require_once __DIR__ . '/vendor/autoload.php'; $bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME'); $bot->sendMessage('chat_id', 'text');
发送照片
<?php namespace Telegram; require_once __DIR__ . '/vendor/autoload.php'; $bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME'); $bot->sendPhoto('chat_id', 'path_to_photo'); //or $bot->sendPhoto('chat_id', array('file_id' => 'file_id_value'));
机器人示例
设置WebHook
<?php namespace Telegram; require_once __DIR__ . '/vendor/autoload.php'; $bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME'); $response = $bot->setWebhook("https://url.to/hook.php"); //only https if($response->description == "Webhook was set") echo "Ok! The bot is ready!"; else{ echo "Ops! Error <br>"; print_r($response); } ?>
BotCore (hook.php)
<?php namespace Telegram; require_once __DIR__ . '/vendor/autoload.php'; $bot = new TelegramBot('YOUR_BOT_API_TOKEN', 'YOUR_BOT_USERNAME'); $response = $bot->hook(); $comand = $response->message->text; if(substr($comand, 0, strlen("/echo")) === "/echo") $bot->sendMessage($response->message->chat->id, str_replace("/echo", "", $comand)); if(substr($comand, 0, strlen("/img")) === "/img") $bot->sendPhoto($response->message->chat->id, 'path_to_photo'); ?>