stajor/telegram-bot

Telegram Bot

4.5.11 2022-03-30 06:22 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:40 UTC


README

PHP 包装器,用于 Telegram 的 Bot API

Minimal PHP version Build Status

安装

将此行添加到应用程序的 composer.json 中

{
    "require": {
        "stajor/telegram-bot": "^3.0"
    }
}

并运行 composer update

或者 在您的命令行中运行此命令

$ composer require stajor/telegram-bot

Telegram API 支持

支持 Telegram Bot API 3.6 的所有类型和方法。

配置

首先您需要创建自己的机器人并获得一个令牌

如何创建一个机器人?

用法

您可以使用 Telegram::Bot::Api 独立使用

<?php

$api = new \Telegram\Bot\Api('BOT TOKEN');
$user = $api->getMe();

或者使用 CommandsHandler 接收来自 Telegram 的更新

创建命令类

<?php

use Telegram\Bot\Command;

class StartCommand extends Command {
    /**
     * @var string Command Name
     */
    protected $name = "start";

    /**
     * @var string Command Description
     */
    protected $description = "Start Command to get you started";

    /**
     * @inheritdoc
     */
    public function handle() {
        // This will send a message using `sendMessage`
        $this->replyWithMessage(['text' => 'Welcome to my Bot']);
        
        // Trigger another command dynamically from within this command
        $this->triggerCommand('help');
    }
}

在您的控制器中添加

<?php
       
$handler = new CommandsHandler('BOT TOKEN');
$handler->addCommand(StartCommand::class);
$handler->handle();

发送照片

$api = new \Telegram\Bot\Api('BOT TOKEN');
$api->sendPhoto(['chat_id' => 123,'photo' => fopen('path_to_photo', 'rb')]);

贡献

欢迎在 GitHub 上提交错误报告和拉取请求 https://github.com/Stajor/telegram-bot。本项目旨在成为一个安全、欢迎的协作空间,并期望贡献者遵守 贡献者公约

许可证

此软件作为开源软件,根据 MIT 许可证 的条款提供。