uzdevid / yii2-telegram
与Telegram集成
1.3.0
2023-04-26 18:02 UTC
Requires
- yiisoft/yii2: ~2.0.0
README
安装
安装此扩展的首选方法是通过 composer。
运行以下命令之一:
composer require uzdevid/yii2-telegram "1.2.0"
或者
"uzdevid/yii2-telegram": "1.2.0"
将其添加到您的 composer.json
文件的 require 部分。
用法
使用配置参数创建 telegram
扩展
$config = [ '_bot_' => [ 'token' => '5390057974:AAFsR6ySk6CTPHs9neYkAXdKTO5cer1cdho' ] ]; $telegram = new Telegram($config);
设置和删除webhook
$url
- 到您的处理器的url
$url = "https://example.com/telegrambot";
$telegram->bot->setWebHook($url);
$telegram->bot->deleteWebHook($url);
发送
发送文本 [文档]
$text
- [string
|required
] - 消息文本。$params
- [array
|optional
] - 其他参数:parse_mode, entities, disable_web_page_preview 等。请参阅Telegram机器人文档...$chat_id
- [integer
|required
] - Telegram用户聊天id。
$text = "Hello world!!!"; $params = [ 'parse_mode'=> 'HTML' ]; $chat_id = 1234567;
$telegram->bot->sender->text($text, $params)->send($chat_id);
注意:所有发送请求都会从Telegram返回响应
示例
$result = $telegram->bot->sender->text($text, $params)->send($chat_id); file_put_contents('test.json', json_encode($result, JSON_UNESCAPED_UNICODE));
发送照片 [文档]
$photo
- [string
|required
] - 照片路径。$params
- [array
|optional
] - 其他参数:parse_mode, caption_entities, disable_notification 等。请参阅Telegram机器人文档...$text
- [string
|optional
] - 照片的说明。$chat_id
- [integer
|required
] - Telegram用户聊天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);
或者您也可以发送没有说明的照片
$telegram->bot->sender->photo($photo, $params)->send($chat_id);
发送视频 [文档]
$video
- [string
|required
] - 视频路径。$params
- [array
|optional
] - 其他参数:duration, width, height 等。请参阅Telegram机器人文档...$text
- [string
|optional
] - 视频的说明。$chat_id
- [integer
|required
] - Telegram用户聊天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);
或者您也可以发送没有说明的视频
$telegram->bot->sender->video($photo, $params)->send($chat_id);
发送贴纸 [文档]
$sticker
[string
|required
] - 贴纸id。$params
- [array
|optional
] - 其他参数:disable_notification, protect_content, reply_to_message_id 等。请参阅Telegram机器人文档...$chat_id
- [integer
|required
] - Telegram用户聊天id。
$sticker = "CAACAgIAAxkBAAEFRRhiz-WSsSh7GsHDlj8_csvlad9-2gACHQADO3EfIqmCmmAwV9EZKQQ"; $params = [ 'disable_notification'=> true ]; $chat_id = 1234567;
$telegram->bot->sender->sticker($sticker, $params)->send($chat_id);
发送联系信息 [文档]
$phone
[string
|required
] - 电话号码。$first_name
[string
|required
] - 名字。$last_name
[string
|optional
] - 姓氏。$params
- [array
|optional
] - 其他参数:vcard, disable_notification, protect_content 等。请参阅Telegram机器人文档...$chat_id
- [integer
|required
] - Telegram用户聊天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);
发送投票 [文档]
$question
- [string
|required
] - 问题。$options
- [array
|required
] - 选项。$correct_option_id
- [integer
|optional
] - 正确选项id,在问答模式中必须。$params
- [array
|optional
] - 其他参数:type, allows_multiple_answers, explanation 等。请参阅Telegram机器人文档...
$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);
通过内联键盘和/或键盘发送消息、照片、视频、贴纸和投票
键盘
$telegram->bot->sender ->text($text) ->createKeyboard([['text' => "Button"]]) ->send($chat_id);
URL内联键盘
$telegram->bot->sender ->photo($photo) ->text($text) ->createInlineKeyboard([['text' => "URL button", 'url' => "https://devid.uz"]]) ->send($chat_id);
回调内联键盘
$callback_data = json_encode(['command' => '/callback', 'id' => 12021]); $telegram->bot->sender ->text($text) ->createInlineKeyboard([['text' => 'callback', 'callback_date' => $callback_data]]) ->send($chat_id);
处理器
注意:在处理请求时,当向请求发送响应时,不需要指定聊天id。
处理 /start
请求
$telegram->bot->handler->onMessage('/start', function ($body) use ($telegram) { // Your code $telegram->bot->sender->text("Welcome")->send(); });
处理回调查询
$telegram->bot->handler->onCommand('/callback', function ($body, $callback_data) use ($telegram) { // Your code $telegram->bot->sender->photo('/img/elephant.jpg')->send(); });
处理所有 (*) 内联查询
$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(); });