prowebcraft/yii2-telebot

适用于 Yii2 框架的 Telegram Bot 扩展

安装: 98

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

dev-main 2023-12-16 11:11 UTC

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