prowebcraft / yii2-telebot
适用于 Yii2 框架的 Telegram Bot 扩展
dev-main
2023-12-16 11:11 UTC
Requires
- php: >=7.4.0
- prowebcraft/telebot: dev-master
- prowebcraft/yii2-model-params: dev-master
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-09-16 12:49:20 UTC
README
适用于 Yii2 框架的 Telegram Bot 扩展
安装
将依赖添加到您的 yii2 项目中
composer require prowebcraft/yii2-telebot
将迁移路径添加到您的 yii2 配置中
'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => null, 'migrationNamespaces' => [ 'console\migrations', // Common migrations for the your application 'prowebcraft\yii2telebot\migrations', // Migrations for Yii2 Telebot ], ], ],
使用 php yii migrate
安装迁移
这将创建用于在数据库中存储机器人、聊天和消息的表。
使用方法
为您的机器人创建类,继承自 \prowebcraft\yii2telebot\YiiBot
将您的机器人令牌放置在 Yii2 参数文件中(例如
<?php return [ 'bots' => [ 'your_bot_name' => [ 'token' => '111111:AABBCCDDEEFFGG' //place your bot token here ] ] ];
创建基本命令
/** * Say hello to you */ public function hiCommand() { $this->reply('Hello! ^)'); }
创建控制台命令以在守护进程模式下运行您的机器人(例如 console/controllers/BotController.php
)
<?php namespace console\controllers; use common\models\YourBot; class BotController extends \yii\console\Controller { /** * Run bot in daemon mode */ public function actionRun() { $bot = new YourBot('your_bot_name'); $bot->start(); } }
使用命令 php yii bot/run
运行您的机器人;
向您的 Telegram 机器人发送 /hi