定制 uzdevid/yii2-telegram 二次开发

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

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

uzdevid/yii2-telegram

Composer 安装命令:

composer require uzdevid/yii2-telegram

包简介

Integration with Telegram

README 文档

README

Installation

The preferred way to install this extension is through composer.

Either run

composer require uzdevid/yii2-telegram "1.2.0"

or add

"uzdevid/yii2-telegram": "1.2.0"

to the require section of your composer.json file.

Usage

Create telegram extension with config params

$config = [
    '_bot_' => [
        'token' => '5390057974:AAFsR6ySk6CTPHs9neYkAXdKTO5cer1cdho'
    ]
];

$telegram = new Telegram($config);

Set and delete webhook

  • $url - url to Your handler
$url = "https://example.com/telegrambot";
$telegram->bot->setWebHook($url);
$telegram->bot->deleteWebHook($url);

Send

Send text [docs]

  • $text - [string | required] - message text.
  • $params - [array | optional] - additional params: parse_mode, entities, disable_web_page_preview and other. Read the telegram bot docs...
  • $chat_id - [integer | required] - telegram user chat id.
$text = "Hello world!!!";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;
$telegram->bot->sender->text($text, $params)->send($chat_id);

Note: All send requests return a response from telegram

Example

$result = $telegram->bot->sender->text($text, $params)->send($chat_id);
file_put_contents('test.json', json_encode($result, JSON_UNESCAPED_UNICODE));

Send photo [docs]

  • $photo - [string | required] - path to photo.
  • $params - [array | optional] - additional params: parse_mode, caption_entities, disable_notification and other. Read the telegram bot docs...
  • $text - [string | optional] - caption for photo.
  • $chat_id - [integer | required] - telegram user chat id.
$photo = '/img/elephant.jpg'
$text = "This is elephant photo";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;
$telegram->bot->sender->photo($photo, $params)->text($text)->send($chat_id);

or you can send a photo without a caption

$telegram->bot->sender->photo($photo, $params)->send($chat_id);

Send video [docs]

  • $video - [string | required] - path to video.
  • $params - [array | optional] - additional params: duration, width, height and other. Read the telegram bot docs...
  • $text - [string | optional] - caption for video.
  • $chat_id - [integer | required] - telegram user chat id.
$video = '/img/avengers-final.mp4'
$text = "Avengers: Final";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;
$telegram->bot->sender->video($photo, $params)->text($text)->send($chat_id);

or you can send a video without a caption

$telegram->bot->sender->video($photo, $params)->send($chat_id);

Send sticker [docs]

  • $sticker [string | required] - sticker id.
  • $params - [array | optional] - additional params: disable_notification, protect_content, reply_to_message_id and other. Read the telegram bot docs...
  • $chat_id - [integer | required] - telegram user chat id.
$sticker = "CAACAgIAAxkBAAEFRRhiz-WSsSh7GsHDlj8_csvlad9-2gACHQADO3EfIqmCmmAwV9EZKQQ";
$params = [
    'disable_notification'=> true
];
$chat_id = 1234567;
$telegram->bot->sender->sticker($sticker, $params)->send($chat_id);

Send contact [docs]

  • $phone [string | required] - phone number.
  • $first_name [string | required] - first name.
  • $last_name [string | optional] - last name.
  • $params - [array | optional] - additional params: vcard, disable_notification, protect_content and other. Read the telegram bot docs...
  • $chat_id - [integer | required] - telegram user chat id.
$phone = '+998993261330';
$first_name = 'Diyorbek';
$last_name = 'Ibragimov';
$params = [
    'disable_notification'=> true
];
$chat_id = 1234567;
$telegram->bot->sender->contact($url, $first_name, $last_name)->send($chat_id);

Send poll [docs]

  • $question - [string | required] - Question.
  • $options - [array | required] - Options.
  • $correct_option_id - [integer | optional] - Correct option id, Required for polls in quiz mode.
  • $params - [array | optional] - additional params: type, allows_multiple_answers, explanation and other. Read the telegram bot docs...
$question = "Question";
$options = ['variant id-0', 'variant id-1', 'variant id-2'];
$correct_option_id = 1;
$params = ['type' => 'quiz'];
$chat_id = 1234567;
$telegram->bot->sender->poll($question, $options, $correct_option_id, $params)->send($chat_id);

Send message, photo, video, sticker and poll with inline keyboard and/or keyboard

Keyboard

$telegram->bot->sender
    ->text($text)
    ->createKeyboard([['text' => "Button"]])
    ->send($chat_id);

URL inline keyboard

$telegram->bot->sender
    ->photo($photo)
    ->text($text)
    ->createInlineKeyboard([['text' => "URL button", 'url' => "https://devid.uz"]])
    ->send($chat_id);

callback inline keyboard

$callback_data = json_encode(['command' => '/callback', 'id' => 12021]);
$telegram->bot->sender
    ->text($text)
    ->createInlineKeyboard([['text' => 'callback', 'callback_date' => $callback_data]])
    ->send($chat_id);

Handlers

Note: When processing requests, there is no need to specify a chat id when sending a response to a request.

Processing /start request

$telegram->bot->handler->onMessage('/start', function ($body) use ($telegram) {
    // Your code
    $telegram->bot->sender->text("Welcome")->send();
});

Processing callback query

$telegram->bot->handler->onCommand('/callback', function ($body, $callback_data) use ($telegram) {
    // Your code
    $telegram->bot->sender->photo('/img/elephant.jpg')->send();
});

Processing all (*) inline query

$telegram->bot->handler->onQuery('*', function ($query, $body) use ($telegram) {
    // Your code
    $title = "Mode: InlineQuery";
    $description = "Query: {$query}";
    $content = "Answer content";
    
    $telegram->bot->sender->inline
        ->answer([$telegram->bot->sender->inline->article($title, $description, $content)])
        ->send();
});

uzdevid/yii2-telegram 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-4-Clause
  • 更新时间: 2022-07-13