edvardpotter/telegram-bot-conversation
Composer 安装命令:
composer require edvardpotter/telegram-bot-conversation
包简介
Telegram bot conversation
关键字:
README 文档
README
A library for creating Telegram bots with support for declarative command descriptions and conversations (dialogue states).
Features
- Declarative description of commands and their handlers
- State system for creating multi-step dialogues
- Support for regular commands and inline buttons
- Support for patterns and regular expressions in commands
- Simple and intuitive API
Installation
composer require edvardpotter/telegram-bot-conversation
Usage
Simple Example
<?php use Edvardpotter\TelegramBotConversation\TelegramConversationHandler; use TelegramBot\Api\BotApi; // Create a Bot API $botApi = new BotApi('YOUR_BOT_TOKEN'); // Create a conversation handler $handler = new TelegramConversationHandler( $conversationStorage, // Your implementation of conversation storage $callbackDataStorage, // Your implementation of callback data storage $botApi ); // Define commands in a declarative style $handler->commands() // Simple /start command ->command('/start', function ($message, $conversation, $api) { $api->sendMessage( $message->getChat()->getId(), 'Hello! I am a bot with a declarative command system!' ); }) // Command with dialogue /register ->conversation('/register', function ($message, $conversation, Context $context, $api) { $api->sendMessage( $message->getChat()->getId(), 'Please enter your name:' ); return 'wait_name'; // next step }) ->step('wait_name', function ($message, $conversation, $context, $api) { $name = $message->getText(); $context->setProperty('name', $name); $api->sendMessage( $message->getChat()->getId(), "Thank you, {$name}! Registration complete." ); return 'finished'; // end conversation }); // Process webhook request $update = Update::fromResponse(json_decode(file_get_contents('php://input'), true)); $handler->handle($update);
Working with Inline Buttons
<?php use Edvardpotter\TelegramBotConversation\CallbackData\CallbackDataFactory; use TelegramBot\Api\Types\Inline\InlineKeyboardMarkup; // Create a factory for callback data $callbackDataFactory = new CallbackDataFactory($callbackDataStorage); // Define command and inline handlers $handler->commands() ->command('/menu', function ($message, $conversation, $api) use ($callbackDataFactory) { // Create callback data for buttons $option1CallbackId = $callbackDataFactory->create('menu_option1'); $option2CallbackId = $callbackDataFactory->create('menu_option2'); // Create keyboard $keyboard = new InlineKeyboardMarkup([ [ ['text' => 'Option 1', 'callback_data' => $option1CallbackId], ['text' => 'Option 2', 'callback_data' => $option2CallbackId] ] ]); $api->sendMessage( $message->getChat()->getId(), 'Select an option:', null, false, null, $keyboard ); }) // Handlers for inline buttons ->inline('menu_option1', function ($message, $conversation, $parameters, $api) { $api->sendMessage( $message->getChat()->getId(), 'You selected Option 1!' ); }) ->inline('menu_option2', function ($message, $conversation, $parameters, $api) { $api->sendMessage( $message->getChat()->getId(), 'You selected Option 2!' ); });
Using Patterns and Regular Expressions
The library supports two types of command patterns:
- Templates with placeholders in
{name}format - Regular expressions with capture groups
([0-9]+)
Command parameters are passed as separate arguments to the handler function:
<?php // Command with a simple template $handler->commands()->patternCommand('call me {name}', function (Message $message, Conversation $conversation, BotApi $api, string $name) { $api->sendMessage( $message->getChat()->getId(), "Hello, {$name}! Nice to meet you." ); }); // Command with multiple parameters in a template $handler->commands()->patternCommand('call me {name} the {adjective}', function (Message $message, Conversation $conversation, BotApi $api, string $name, string $adjective) { $api->sendMessage( $message->getChat()->getId(), "Greetings, {$name} the {$adjective}! Your greatness knows no bounds!" ); }); // Command using regular expression with a named parameter $handler->commands()->patternCommand('/I want ([0-9]+) apples/', function (Message $message, Conversation $conversation, BotApi $api, string $quantity) { $quantity = (int)$quantity; $api->sendMessage( $message->getChat()->getId(), "You want {$quantity} apples? That's a lot of fruit!" ); }, true); // Command with named capture groups in a regular expression $handler->commands()->patternCommand( '/I want (?<quantity>[0-9]+) and my age is (?<age>[0-9]+)/', function (Message $message, Conversation $conversation, BotApi $api, string $quantity, string $age) { $quantity = (int)$quantity; $age = (int)$age; $api->sendMessage( $message->getChat()->getId(), "You want {$quantity} items and you are {$age} years old." ); }, true );
Note: The order of parameters is important! System parameters always come first:
$message,$conversation,$api, followed by parameters extracted from the message text.
License
MIT
edvardpotter/telegram-bot-conversation 适用场景与选型建议
edvardpotter/telegram-bot-conversation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「telegram bot commands」 「telegram bot conversation」 「telegram bot finite state machine」 「telegram bot FSM」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 edvardpotter/telegram-bot-conversation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 edvardpotter/telegram-bot-conversation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 edvardpotter/telegram-bot-conversation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collection of handy Laravel artisan commands that most projects needs
Dialogflow php sdk
Additional artisan commands for Laravel
Laravel 5.6 Monolog custom telegram channel
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
PHP Wrapper for Telegram Bot API
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-02