承接 codewiser/telegram-subscriber 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

codewiser/telegram-subscriber

Composer 安装命令:

composer require codewiser/telegram-subscriber

包简介

Telegram Subscriber for Laravel

README 文档

README

The package provides a solution for retrieving chat ID.

Installation and setup

Package uses irazasyed/telegram-bot-sdk. So above all follow Telegram Bot SDK instructions and set up your first Telegram Bot. Most likely you need to run:

php artisan vendor:publish --tag="telegram-config"

In config/telegram.php configuration file add bot name parameter and register DeeplinkCommand. You may not define webhook_url as it will be reconfigured on a fly.

# config/telegram.php

'bots' => [
    'my_bot' => [
        'name'             => env('TELEGRAM_BOT_NAME'),
        'token'            => env('TELEGRAM_BOT_TOKEN'),
        'certificate_path' => env('TELEGRAM_CERTIFICATE_PATH'),
        //'webhook_url'      => env('TELEGRAM_WEBHOOK_URL'),
        'commands'         => [
            \Codewiser\Telegram\Commands\DeeplinkCommand::class
        ],
    ],
]

Retrieving chat ID

Implement \Codewiser\Telegram\Contracts\TelegramNotifiable to a User model. You might need to write a migration...

use \Illuminate\Notifications\Notifiable;
use \Illuminate\Database\Eloquent\Model;
use \Codewiser\Telegram\Contracts\TelegramNotifiable;

class User extends Model implements TelegramNotifiable
{
    use Notifiable;
    
    public function routeNotificationForTelegram($notification = null): mixed
    {
        return $this->telegram_user_id;
    }

    public function setRouteForTelegram($route): void
    {
        $this->telegram_user_id = $route;
        
        $this->save();
    }
}

Now, create service to implement \Codewiser\Telegram\Contracts\TelegramNotifiableProvider. This is an example of implementation, your may implement it however you like.

use \Codewiser\Telegram\Contracts\TelegramNotifiableProvider;

class TelegramUserProvider implements TelegramNotifiableProvider
{
    /**
     * Issue and remember new token for a given notifiable.
     */
    public function generateToken(TelegramNotifiable $notifiable): string
    {
        $token = Str::random(40);

        cache()->set(
            $token,
            [
                'key'   => $notifiable->getKey(),
                'model' => get_class($notifiable),
            ],
            now()->addMinutes(5)
        );

        return $token;
    }
    
    /**
     * Find notifiable associated with a given token.
     */
    public function resolveToken(string $token): ?TelegramNotifiable
    {
        $notifiable = cache()->pull($token);

        if ($notifiable) {
            $key = $notifiable['key'];
            $model = $notifiable['model'];
            
            return $model::find($key);
        }

        return null;
    }
}

At last, register this service in AppServiceProvider of your application

public function register()
{
    $this->app->singleton(TelegramNotifiableProvider::class, fn() => new TelegramUserProvider);
}

We are ready to go.

Getting updates

Register webhook

If you are properly configure bot in config/telegram.php this is enough to use telegram:webhook command provided by Telegram Bot SDK package. We recommend to read help:

php artisan help telegram:webhook

This package provides webhook controller to deal with incoming messages.

For example DeeplinkCommand, that was mentioned above, used to handle /start command with deeplink token.

You are free to add any other command handlers to config/telegram.php.

Read more about Bot Commands.

Long polling

This package brings telegram:poll command to get updates without registering webhook. Just call a command.

Usage

Subscribe user

First, we need to issue a deeplink for a user.

use \Illuminate\Http\Request;
use \Codewiser\Telegram\TelegramService;

class DeeplinkController extends Controller
{
    public function __invoke(Request $request, TelegramService $service) {
        return $service->getDeeplink($request->user());
    }
}

User follows deeplink, opens telegram client and presses Start button.

Codewiser\Telegram\Commands\DeeplinkCommand handles incoming update, resolves deeplink token and update User with chat_id.

For now, this user has telegram route and may be notified via Telegram.

Unsubscribe user

If user blocks or delete a bot, app can not deliver notification: telegram responds with 403 status code. In that case we should count user as unsubscribed and may delete chat_id.

To do so, just register event listener:

use Codewiser\Telegram\Listeners\UnsubscribeTelegramNotifiable;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Support\Facades\Event;

Event::listen(NotificationFailed::class, UnsubscribeTelegramNotifiable::class);

codewiser/telegram-subscriber 适用场景与选型建议

codewiser/telegram-subscriber 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 281 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 codewiser/telegram-subscriber 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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