定制 edvardpotter/telegram-bot-conversation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

edvardpotter/telegram-bot-conversation

Composer 安装命令:

composer require edvardpotter/telegram-bot-conversation

包简介

Telegram bot conversation

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

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:

  1. Templates with placeholders in {name} format
  2. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 edvardpotter/telegram-bot-conversation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-02