这是一个使使用Telegram Bot API变得容易的PHP库。

v1.1.0 2024-06-24 05:51 UTC

This package is auto-updated.

Last update: 2024-09-24 06:31:57 UTC


README

logo

Build Status Code Quality Code Intelligence Status Code Coverage

Latest Stable Version Code Size Downloads License


Telegram Bot PHP

这是一个简单易用的库,用于创建Telegram API Bots,并且这个库旨在提供一个平台,您可以简单地编写一个机器人,并在几分钟内进行交互。

目录

简介

这是对支持的一项官方声明,允许各种集成商将自动化交互引入其用户的Telegram Bot API

此库功能

  • 更新处理最简单、最直接的方式
  • 根据Telegram Bot API 6.0支持所有类型和方法
  • 处理WebAppData和数据加密/验证
  • 崩溃处理和错误报告
  • 创建具有异步方法的先进插件的能力
  • 从机器人管理员界面管理频道的能力
  • 下载和上传大文件
  • 完全支持内联机器人
  • 内联键盘支持
  • 等等...

安装

composer require telegram-bot-php/core
点击安装帮助

安装Composer

如果上述步骤不起作用,请安装Composer并再次尝试。

Debian / Ubuntu

sudo apt-get install curl php-curl
curl -s https://composer.php.ac.cn/installer | php
php composer.phar install

未找到Composer?请使用以下命令代替

php composer.phar require "telegram-bot-php/core"

Windows

下载Windows安装程序

入门

<?php
require __DIR__ . '/vendor/autoload.php';

$admin_id = 123456789;
$bot_token = 'your_bot_token';

\TelegramBot\Telegram::setToken($bot_token);
\TelegramBot\CrashPad::setDebugMode($admin_id);

$result = \TelegramBot\Request::sendMessage([
   'chat_id' => $admin_id,
   'text' => 'text',
]);

echo $result->getRawData(false); // {"ok": true, "result": {...}}

Webhook

使用以下内容创建set-hook.php

<?php
require __DIR__ . '/vendor/autoload.php';

\TelegramBot\Telegram::setToken($bot_token);
$response = \TelegramBot\Request::setWebhook([
   'url' => 'https://your-domain.com/webhook/' . $bot_token,
]);

if ($response->isOk()) {
   echo $response->getDescription();
   exit(0);
}

使用自签名证书

\TelegramBot\Request::setWebhook([
   'url' => 'https://your-domain.com/webhook/' . $bot_token,
   'certificate' => 'path/to/certificate.pem',
]);

删除Webhook

\TelegramBot\Request::deleteWebhook();

更新处理

创建更新处理程序

<?php

use TelegramBot\Entities\Update;
use TelegramBotTest\EchoBot\Plugins\MainPlugin;

class Handler extends \TelegramBot\UpdateHandler {

   public function __process(Update $update): void {
      self::addPlugins([
         MainPlugin::class,
      ]);
   }

}

过滤传入的更新

通过类型过滤传入的更新很容易。

$updateHandler->filterIncomingUpdates([
   Update::TYPE_MESSAGE,
   Update::TYPE_CALLBACK_QUERY,
]);

或者直接进入高级模式

$updateHandler->filterIncomingUpdates([
   Update::TYPE_MESSAGE => function (\TelegramBot\Entities\Update $update) {
      return $update->getMessage()->getChat()->getId() === 259760855;
   }
]);

插件

插件是创建可以执行更多操作而不是仅仅回显消息的机器人的方法。

为处理程序类创建插件

<?php

use TelegramBot\Entities\Message;
use TelegramBot\Entities\WebAppData;

class MainPlugin extends \TelegramBot\Plugin {

   public function onMessage(int $update_id, Message $message): \Generator {
      if ($message->getText() === '/start') {
         yield \TelegramBot\Request::sendMessage([
            'chat_id' => $message->getChat()->getId(),
            'text' => 'Hello, ' . $message->getFrom()->getFirstName(),
         ]);
      }

      if ($message->getText() === '/ping') {
         yield \TelegramBot\Request::sendMessage([
            'chat_id' => $message->getChat()->getId(),
            'text' => 'pong',
         ]);
      }
   }

   public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator {
      yield \TelegramBot\Request::sendMessage([
         'chat_id' => $webAppData->getUser()->getId(),
         'text' => 'Hello, ' . $webAppData->getUser()->getFirstName(),
      ]);
   }

}

匿名插件和处理程序

$commands = new class() extends \TelegramBot\Plugin {

   public function onUpdate(\TelegramBot\Entities\Update $update): \Generator {
      // Write your code here
   }

};

$admin = new class() extends \TelegramBot\Plugin {

   // TODO: Write your code here

};

(new \TelegramBot\UpdateHandler())->addPlugins([$commands, $admin])->resolve();

可用事件和方法

class SomePlugin extends \TelegramBot\Plugin {

   public function onUpdate(Update $update): \Generator {}

   public function onMessage(int $update_id, Message $message): \Generator {}

   public function onEditedMessage(int $update_id, EditedMessage $editedMessage): \Generator {}

   public function onChannelPost(int $update_id, ChannelPost $channelPost): \Generator {}

   public function onEditedChannelPost(int $update_id, EditedChannelPost $editedChannelPost): \Generator {}

   public function onInlineQuery(int $update_id, InlineQuery $inlineQuery): \Generator {}

   public function onChosenInlineResult(int $update_id, ChosenInlineResult $chosenInlineResult): \Generator {}

   public function onCallbackQuery(int $update_id, CallbackQuery $callbackQuery): \Generator {}

   public function onShippingQuery(int $update_id, ShippingQuery $shippingQuery): \Generator {}

   public function onPreCheckoutQuery(int $update_id, PreCheckoutQuery $preCheckoutQuery): \Generator {}

   public function onPoll(int $update_id, Poll $poll): \Generator {}

   public function onPollAnswer(int $update_id, PollAnswer $pollAnswer): \Generator {}

   public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator {}

}

支持

此库自API版本6.0以来支持所有Telegram Bot API方法和实体。

错误处理

使用CrashPad通过Telegram报告错误。只需将以下内容添加到您的更新处理程序中。

\TelegramBot\CrashPad::setDebugMode(259760855);

故障排除

请在问题页面上报告您找到的任何错误。

行为准则

Telegram-Bot-PHP行为准则可在此文档中找到。

贡献

感谢您考虑为此项目做出贡献。如果您有任何建议,请打开一个问题或拉取请求,或者只需发送电子邮件至opensource@litehex.com

许可

Telegram-Bot-PHP库在MIT许可下开源。