dratejinn/telegrambot
Composer 安装命令:
composer require dratejinn/telegrambot
包简介
A Telegram bot api
README 文档
README
A PHP 7.0+ Telegram bot API used to create bots for Telegram. For more information check Telegrams bot API
Base API
The basis of the bot API can be found in src/API. The structure reflects telegrams API as much as possible.
Basic examples
All sendable objects have a ->call(\Telegram\API\Bot $bot) method which can be used to send the object to the telegram API. Some examples can be found in Examples/Examples.php
sending a chatmessage
<?php require_once 'vendor/autoload.php'; use Telegram\API\Bot; use Telegram\API\Method\SendMessage; $bot = new Bot('bottoken here'); $sendMessage = new SendMessage; $sendMessage->chatId = 1234566; $sendMessage->text = 'Hello world!'; $sendMessage->call($bot);
Sending a photo
<?php require_once 'vendor/autoload.php'; use Telegram\API\Bot; use Telegram\API\Method\SendPhoto; $bot = new Bot('bottoken here'); $sendPhoto = new SendPhoto; $sendPhoto->chatId = 1234566; $sendPhoto->photo = new InputFile('pathToFile'); $sendPhoto->call($bot);
Creating a bot
Creating a bot is very easy. Telegram accepts two type of bots. One where the bot is continuously requesting updates using the getUpdates API call and the other is using WebHooks. Both options are available.
A simple bot The bot
<?php declare(strict_types = 1); namespace Examples\Bot; use Telegram\Bot\ABot as TBot; class ExampleBot extends TBot { const TOKEN = ''; //place your token here (optionally) protected $_handlers = [ self::UPDATE_TYPE_MESSAGE => Handlers\MessageHandler::class ]; public function __construct(string $token = NULL) { if ($token === NULL) { $token = self::TOKEN; } parent::__construct($token); } }
Message handler
<?php declare(strict_types = 1); namespace Examples\Bot\Handlers; use Telegram\Bot\Handler\AMessageHandler; class MessageHandler extends AMessageHandler { protected $_commandHandlers = [ Command\ExampleCommandHandler::class, ]; public function handleText(string $text) { $this->sendTextMessage('You typed: ' . $text); } }
Command handler
<?php declare(strict_types = 1); namespace Examples\Bot\Handlers\Command; use Telegram\Bot\Handler\ACommandHandler; class ExampleCommandHandler extends ACommandHandler { public function handleCommand(string $command) { $this->sendTextMessage('You fired the command: ' . $command); $this->sendTextMessage('Provided with arguments: ' . implode(' ', $this->getArguments())); } public static function GetRespondsTo() : array { return [ 'test', 'example' ]; } }
Running the bot with continuous polling
<?php declare(strict_types = 1); namespace Examples\Bot; use Telegram\API; use Telegram\API\ConfigManager; use Telegram\Storage\PDO\ConnectionDetails\ConnectionDetailsMySQL; use Telegram\Storage\PDO\MySQLHandler; require_once __DIR__ . '/../vendor/autoload.php'; if (file_exists(__DIR__ . '/userCreds.php')) { require_once __DIR__ . '/userCreds.php'; $userCreds = mysqlUserCreds(); } else { $userCreds = NULL; } define('DEVELOPMENT_MODE', TRUE); API\ConfigManager::AddConfigFromINIFile(__DIR__ . '/Log.ini', 'Log'); ConfigManager::AddConfigFromINIFile(__DIR__ . '/../Tokens.ini', 'token'); $tokens = ConfigManager::GetConfig('token'); $token = $tokens['devbot']['token']; $bot = new ExampleBot($token); if ($userCreds) { $connectionDetails = new ConnectionDetailsMySQL; $mysqlHandler = new MySQLHandler($connectionDetails, $userCreds, 'telegrambot'); $bot->setStorageHandler($mysqlHandler); } $bot->run();
Running the bot using webhooks
<?php use Examples\Bot\ExampleBot; use Telegram\API\Type\Update; require_once __DIR__ . '/../vendor/autoload.php'; $input = file_get_contents('php://input'); if ($input) { $bot = new ExampleBot('tokenHere'); $update = json_decode($input); $updateObj = new Update($update); $bot->handleUpdate($update); }
dratejinn/telegrambot 适用场景与选型建议
dratejinn/telegrambot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.66k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dratejinn/telegrambot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dratejinn/telegrambot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-21