lowel/telepath 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lowel/telepath

Composer 安装命令:

composer require lowel/telepath

包简介

Telegram bot sdk for laravel inspired by phptg/bot-api

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Telegram bot SDK for Laravel inspired by phptg/bot-api.

SDK supports routes and long pooling \ webhook handling

How To Install

Install package via composer and publish config file:

composer require lowel/telepath
php artisan vendor:publish --tag="telepath-config"

Then you need to create routes/telegram.php file and provide bot key into your .env file:

touch routes/telegram.php
TELEPATH_TOKEN=<YOUR_TOKEN_HERE>

That's it! now you can handle Update request in your telegram.php file.

Usage

use Lowel\Telepath\Facades\Telepath;
use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Type\Update\Update;

Telepath::middleware(function (TelegramBotApi $telegramBotApi, Update $update, callable $callback) {
    logger()->info('Middleware in');
    $callback();
    logger()->info('Middleware out');    
})->group(function () {
    Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
        $telegramBotApi->sendMessage($update->getMessage()->getChat()->getId(), 'Hello, world!');
    }, pattern: '/start');

    Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
        $message = $update->getMessage();
        if ($message) {
            $telegramBotApi->sendMessage($message->getChat()->getId(), $message->getText());
        }
    }, pattern: '/echo');
});

Start up:

php artisan telepath:run

Or using webhook:

php artisan telepath:hook:set https://your-domain.com/api/webhook

Documentation

  1. Installation
  2. Configuration
  3. Handlers
  4. Conversations
  5. Keyboards
  6. Exceptions
  7. Tests

Installation

You can install the package via composer and publish configuration file to your existing project:

composer require lowel/telepath
php artisan vendor:publish --tag="telepath-config"

Then you need to create routes/telegram.php file and provide bot key into your .env file:

touch routes/telegram.php
TELEPATH_TOKEN=<YOUR_TOKEN_HERE>

That's it! now you can handle Update request in your telegram.php file using Telepath facade.

Configuration

Below you can see how default configuration file looks like:

return [
    /*
    |--------------------------------------------------------------------------
    | Telepath Configuration
    |--------------------------------------------------------------------------
    |
    | This file is for storing the configuration of the Telepath package.
    | You can set your bot token and other settings here.
    |
    */

    'base_uri' => env('TELEPATH_BASE_URL', 'https://api.telegram.org'),
    'conversation' => [
        'ttl' => (int) env('TELEPATH_CONVERSATION_TIMEOUT', 60),
    ],

    /*
     * Routes path
     */
    'routes' => base_path(env('TELEPATH_ROUTES', 'routes/telegram.php')),

    'profile' => 'default',

    /**
     * see @link \Lowel\Telepath\Config\Profile
     */
    'profiles' => [
        'default' => [
            'token' => env('TELEPATH_TOKEN'),
            'offset' => (int) env('TELEPATH_OFFSET', 0),
            'limit' => (int) env('TELEPATH_LIMIT', 100),
            'timeout' => (int) env('TELEPATH_TIMEOUT', 30),
            'allowed_updates' => env('TELEPATH_ALLOWED_UPDATES', '*'),

            'whitelist' => env('TELEPATH_ADMINS', ''),
            'blacklist' => env('TELEPATH_BANNED', ''),

            // will send report about unhandled exceptions to the given chat_id instance (chat or dm)
            'chat_id_fallback' => (int) env('TELEPATH_CHAT_ID_FALLBACK', null),
        ],
    ],
];

The only one necessary thing is the token field. But lets try looks on all fields little closer:

  • base_uri - telegram api default domain (you can change if you are using self-hosted solution);
  • conversation - settings for dialog conversation that use memory to handle request in async mode:
    • ttl - decide how long conversation memory would work for each conversation message;
  • routes - path to the routes handlers definition context (default routes/telegram.php);
  • profile - default telegram bot profile name;
  • profiles - list of telegram bot profiles;
    • token - telegram bot token that you receive from @BotFather (REQUIRED, pass throw TELEPATH_TOKEN env field);
    • offset - telegram getUpdates offset field (default 0);
    • limit - telegram getUpdates limit field (default 100);
    • timeout - telegram getUpdates timeout field (default 30);
    • allowed_updates - telegram getUpdates allowed_updates field (default '*');
    • whitelist - comma separated list of user IDs that are allowed to interact with the bot (default empty);
    • blacklist - comma separated list of user IDs that are not allowed to interact with the bot (default empty);
    • chat_id_fallback - chat ID where unhandled exceptions will be reported (default null).

Handlers

You can define handlers in your routes/telegram.php file using Telepath facade:

use Lowel\Telepath\Facades\Telepath;

Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
    $telegramBotApi->sendMessage($update->getMessage()->getChat()->getId(), 'Hello, world!');
}, pattern: '/start');

Also you can use middleware to handle request before and after main handler:

Telepath::middleware(function (TelegramBotApi $telegramBotApi, Update $update, callable $callback) {
    logger()->info('Middleware in');
    $callback();
    logger()->info('Middleware out');
})->group(function () {
    Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
        $telegramBotApi->sendMessage($update->getMessage()->getChat()->getId(), 'Hello, world!');
    }, pattern: '/start');
});

If you want to store Handlers and Middlewares as separated files you can generate them using commands below:

  • Handlers
php artisan telepath:make:handler StartHandler
  • Middlewares
php artisan telepath:make:middleware LogMiddleware

Conversations

Conversations in Telepath simple as javascript Promises. You can generate a conversation using artisan:

php artisan telepath:make:conversation SampleConversation

And then use it in your handlers:

use Lowel\Telepath\Facades\Telepath;
use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Type\Update\Update;

Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
//
}, pattern: '/start-conversation')
    ->conversation(SampleConversation::class);

Keyboards

Telepath supports both inline and reply keyboards. You can create keyboards using the following artisan commands:

  • Inline Keyboard
php artisan telepath:make:inline-keyboard SampleInlineKeyboard
  • Reply Keyboard
php artisan telepath:make:reply-keyboard SampleReplyKeyboard

And then, after describing your keyboard layout in the generated files, you can use them in your handlers like this:

use Lowel\Telepath\Facades\Telepath;
use Phptg\BotApi\TelegramBotApi;
use Phptg\BotApi\Type\Update\Update;
use Lowel\Telepath\Keyboards\InlineKeyboard\SampleInlineKeyboard;

Telepath::on(function (TelegramBotApi $telegramBotApi, Update $update) {
    $keyboard = new SampleInlineKeyboard();
    $telegramBotApi->sendMessage(
        Extrasense::chat()->id,
        'Choose an option:',
        replyMarkup: $keyboard->make()->build()
    );
}, pattern: '/keyboard');

Telepath::keyboard(SampleInlineKeyboard::class);

Exceptions

Telepath has ExceptionHandler component. That allows you to catch unhandled exceptions and process them in your own way - just define wraps into your AppServiceProvider:

\Lowel\Telepath\Facades\Paranormal::wrap(function (Update $update, Throwable $e) {
    // Your code that may throw exceptions
})

By default Telepath will send unhandled exceptions report to the chat_id defined in the configuration file.

Tests

I am strive to coverage my code as much as possible with tests. But its almost features tests only. I hope its will changed soon:

php artisan test

License

The MIT License (MIT). Please see License File for more information.

lowel/telepath 适用场景与选型建议

lowel/telepath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「lowel」 「telepath」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lowel/telepath 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lowel/telepath 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-29