rasa / telekit
创建Telegram机器人的库
v2.7.0
2023-05-21 01:49 UTC
Requires
- php: ^8.0
- ext-curl: *
- ext-pdo: *
- doctrine/dbal: ^3.6
- eftec/bladeone: ^4.9
- guzzlehttp/guzzle: ^6.5
- illuminate/collections: ^10.9
- illuminate/database: ^10.9
- symfony/console: ^6.2
- vlucas/phpdotenv: ^5.5
README
关于Telekit
Telekit是一个易于使用Laravel Eloquent ORM构建Telegram机器人的强大工具包。使用Telekit,开发者可以快速构建和部署机器人,节省时间和精力。
Telekit的一个突出特点是它对Laravel Eloquent ORM的支持,这使得开发者可以轻松地与数据库交互,并仅用几行代码执行复杂操作。这使得在您的Telegram机器人中管理数据变得简单,同时保持代码组织良好且易于维护。
Telekit还提供了一系列工具和实用程序,用于处理Telegram API,这使得处理传入消息、向用户发送消息和执行其他常见机器人任务变得容易。开发者可以利用Telekit直观的API快速构建和测试新的机器人功能,无需担心Telegram协议的底层细节。
要求
- PHP版本8.0
- Postgres、Mysql、Sqlite数据库
- 运行迁移(聊天表)
要使用Composer安装Telekit库,请按照以下步骤操作:
打开终端或命令提示符,导航到您的项目目录。
运行以下命令
composer require rasa/telekit
这将下载并安装Telekit库及其所有依赖项到您的项目vendor目录。
安装完成后,您可以通过在代码中包含其自动加载文件来开始使用Telekit
- 我的网站
- 在浏览器中接收更新 https://api.telegram.org/bot{TOKEN}/getUpdates
- 设置webhook https://api.telegram.org/bot{TOKEN}/setwebhook?url=https://test.com/example_bot/index.php
- 删除webhook https://api.telegram.org/bot{TOKEN}/deleteWebhook
"artisan"命令
使用"php artisan"命令与机器人交互。以下是一些示例:
轮询
使用轮询方法在本地计算机上处理请求,而不使用钩子
php artisan serve
查看所有可用命令
php artisan
发送消息
向所有用户发送
php artisan send all
指定聊天ID
php artisan send --to=CHAT_ID
php artisan send -t CHAT_ID
指定消息
php artisan send --to=CHAT_ID --message="hello world"
php artisan send -t CHAT_ID -m "hello world"
"make"命令
创建模板交互文件
php artisan make:interaction MyInteraction
创建模板触发文件
php artisan make:trigger myTrigger
创建模板迁移文件
php artisan make:migration table
创建模板模型文件
php artisan make:model User
"database"命令
获取数据库
php artisan database
参数
php artisan database:params
检查数据库版本
php artisan database:version
显示表
php artisan database:tables
输出表内容
php artisan database:table chats
描述表
php artisan database:table chats --desc
输出特定表字段的内容
php artisan database:showTable chats --columns="chat_id"
php artisan database:showTable chats --columns="chat_id" --columns="username" --columns="attempts"
运行迁移
php artisan migrate
撤销所有迁移
php artisan migrate --fresh
查看响应
php artisan responses
发送响应
回复消息
class Hi extends Trigger {
public function __construct($request)
{
$this->reply_message('Hi');
}
}
发送消息
<?php
namespace Triggers;
class Help extends Trigger {
public function __construct($request)
{
$this->sendMessage()
->chat_id($request['message']['chat']['id'])
->text('Помощь')
->send();
}
}
发送带有按钮的消息
$this->sendMessage()
->chat_id($request['message']['chat']['id'])
->text('Choose the option')
->parse_mode()
->reply_markup([
'one_time_keyboard' => true,
'resize_keyboard' => true,
'inline_keyboard' => [
[
[
'text' => 'About',
'callback_data' => 'About',
],
[
'text' => 'Help',
'callback_data' => 'help',
]
],
[
[
'text' => 'Settings',
'callback_data' => 'settings',
]
]
]
])
->send();
发送照片
$this->photo()
->protect_content(true)
->caption('Подпись')
->photo("image1.png", "кот.jpg", "image/jpg")
->send()
发送文档
$this->document()
->protect_content(true)
->caption('Sign')
->photo("image1.png", "cat.jpg", "image/jpg")
->send()
发送媒体文件
$this->mediaGroup()
->chat_id(id)
->media([
['type' => 'document', 'name' => 'mycat', 'path' => 'img/image1.png'],
['type' => 'document', 'name' => 'mycat2', 'path' => 'img/image1.png']
])
->send();
内联查询
namespace Inlines;
use Core\Responses\Interaction;
class Example extends Interaction {
public function __construct($request)
{
$result = [
[
"type" => "article",
"id" => "0",
"title" => "Do",
"description" => "something",
"input_message_content" => [
"message_text" => "result: <b> OK </b>",
"parse_mode" => "HTML"
]
],
[
"type" => "article",
"id" => "1",
"title" => "Do 2",
"description" => "something 2",
"input_message_content" => [
"message_text" => "result: <b> OK 2 </b>",
"parse_mode" => "HTML"
]
]
];
$this->answerInlineQuery()
->inline_query_id($request['inline_query']['id'])
->results($result)
->cache_time(1)
->is_personal(true)
->send(false, false);
}
}
发送发票
$this->send_invoice()
->title('Subscription')
->description("Buy subscription for an month.")
->payload('Subscription: month')
->currency('USD')
->prices(['label' => 'Subscription for an month', 'amount' => 100000])
->send();