nullform/telegram-gateway
Composer 安装命令:
composer require nullform/telegram-gateway
包简介
PHP client for Telegram Gateway API
README 文档
README
The PHP package for sending verification codes via Telegram Gateway.
The Gateway API is an HTTP-based interface created for developers looking to deliver automated messages, such as verification codes, to users who registered their phone number on Telegram.
Requirements
- PHP >= 5.6
Installation
composer require nullform/telegram-gateway
First, you need to get an access token from your Telegram Gateway account settings: https://gateway.telegram.org.
Usage examples
Simplest example:
$client = new \Nullform\TelegramGateway\Client('API Token'); $parameters = new \Nullform\TelegramGateway\Parameters\SendVerificationMessageParameters(); $parameters->code = '123456'; try { $requestStatus = $client->sendVerificationMessage('+19999999999', $parameters); // ... } catch (\Exception $exception) { // ... }
Extended example:
use Nullform\TelegramGateway\Client; use Nullform\TelegramGateway\Parameters\SendVerificationMessageParameters; use Nullform\TelegramGateway\AbstractException; $token = 'API Token'; $number = '+19999999999'; // The phone number to which you want to send a verification message. $client = new Client($token); $client->setCurlOption(CURLOPT_TIMEOUT, 10); // You can set your own cURL option... $client->setCurlOptions([CURLOPT_TIMEOUT => 10]); // ... or multiple options. $parameters = new SendVerificationMessageParameters(); $parameters->code_length = 6; // Set your own code or code length. $parameters->payload = 'custom data'; // Custom payload. $parameters->ttl = 60; // Time-to-live (1 minute) before the message expires. $parameters->callback_url = 'https://example.com/webhook/telegram-gateway'; // Your callback URL. $parameters->sender_username = 'my_tg_channel'; // Channel from which the code will be sent. try { // Instance of RequestStatus. $requestStatus = $client->sendVerificationMessage($number, $parameters); // Last request info. $curlInfo = $client->getCurlInfo(); } catch (AbstractException $exception) { // Unable to send or error during sending. // ... }
You can check send ability first:
use Nullform\TelegramGateway\Client; use Nullform\TelegramGateway\Parameters\SendVerificationMessageParameters; use Nullform\TelegramGateway\AbstractException; $token = 'API Token'; $number = '+19999999999'; $client = new Client($token); try { // Throws an exception if there is no ability. $abilityStatus = $client->checkSendAbility(); try { $parameters = new SendVerificationMessageParameters(); // The unique identifier of a previous request from checkSendAbility. // If provided, this request will be free of charge. $parameters->request_id = $abilityStatus->request_id; $parameters->code = '123456'; // Your custom code. A fully numeric string of 4-8 chars. $requestStatus = $client->sendVerificationMessage($number, $parameters); // ... } catch (AbstractException $exception) { // Can't send. // ... } } catch (AbstractException $exception) { // No ability. // ... }
Use checkVerificationStatus method to check the status of a verification message that was sent previously. If the code was generated by Telegram for you, you can also verify the correctness of the code entered by the user using this method.
use Nullform\TelegramGateway\Client; use Nullform\TelegramGateway\AbstractException; use Nullform\TelegramGateway\Types\VerificationStatus; $token = 'API Token'; $client = new Client($token); $requestId = 'request id'; $code = $_POST['code']; try { $status = $client->checkVerificationStatus($requestId, $code); if ($status->verification_status->status == VerificationStatus::STATUS_CODE_VALID) { // Success. } } catch (AbstractException $exception) { // ... }
Report delivery
You can receive delivery reports from the Telegram Gateway API. Just pass your callback URL to the sendVerificationMessage method and handle the POST request:
use Nullform\TelegramGateway\ReportHandler; use Nullform\TelegramGateway\AbstractException; $token = 'API Token'; $handler = new ReportHandler($token); try { // Instance of RequestStatus. $requestStatus = $handler->receive(); } catch (AbstractException $exception) { // ... }
Methods
Client
- Client::__construct(string $token)
- Client::getCurlOptions(): array
- Client::setCurlOptions(array $curlOptions): Client
- Client::setCurlOption(int $option, mixed $value): Client
- Client::getCurlInfo(): array
- Client::sendVerificationMessage(string $phoneNumber, SendVerificationMessageParameters $parameters): RequestStatus
- Client::checkSendAbility(string $phoneNumber): RequestStatus
- Client::checkVerificationStatus(string $requestId, ?string $code = null): RequestStatus
- Client::revokeVerificationMessage(string $requestId): bool
ReportHandler
- ReportHandler::__construct(string $token)
- ReportHandler::receive(): RequestStatus
- ReportHandler::parseReport(string $payload): RequestStatus
- ReportHandler::checkRequest(array $httpRequestHeaders, string $httpRequestBody): bool
- ReportHandler::getRequestTimestamp(?array $customHttpRequestHeaders = null): int
Tests
For unit tests, you need to create a credentials.php file (see tests/credentials.example.php).
php composer.phar test tests
nullform/telegram-gateway 适用场景与选型建议
nullform/telegram-gateway 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.78k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「verification」 「telegram」 「telegram gateway api」 「telegram gateway」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nullform/telegram-gateway 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nullform/telegram-gateway 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nullform/telegram-gateway 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Package for easier user email verification.
Laravel 5.6 Monolog custom telegram channel
A PSR-7 compatible library for making CRUD API endpoints
PHP Wrapper for Telegram Bot API
The TOTP based on [RFC6238](https://tools.ietf.org/html/rfc6238)
This is an easy to use, powerful and extendable Laravel package for authorization user actions via custom channels.
统计信息
- 总下载量: 8.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-09