telegram-bot-sdk/standalone-starter

Telegram Bot SDK Standalone Starter 项目

dev-master 2023-04-08 01:44 UTC

This package is auto-updated.

Last update: 2024-09-08 04:53:44 UTC


README

为 PHP 创建 Telegram 机器人的 Telegram Bot SDK 独立启动器。

功能

  • 多机器人。
  • Webhook。
  • 更新轮询。
  • 命令。
  • 事件监听器。

用法

composer create-project telegram-bot-sdk/standalone-starter mybot
  • 使用 Botfather 创建机器人。
  • 打开 .env 并配置您的令牌和域名(如果已克隆此项目,请复制 .env.example)。
  • 探索 config/telegram.php
  • 查看文档。
  • 构建机器人!

辅助工具

启动器包含一些有用的辅助工具,方便使用,可在 bot/helpers.php 中找到。

  • telegram() - 从容器中获取 BotManager 实例。

目录结构

.
├── .env - Project environment variable.
├── bootstrap - Bot bootstrapping files.
├── bot - Your bot main files.
│   ├── Commands - Bot commands.
│   ├── Console - CLI console commands.
│   ├── Facades - Bot Facades.
│   ├── Http - Bot controllers.
│   └── Listeners - Event Listeners.
├── config - Config files.
|   └── telegram.php - Your main SDK configuration.
└── public - Public facing files.
    └── index.php - Project index file.
    └── pooling.php - Long-pooling update handler.
    └── webhook.php - Webhook update handler.

Webhook 设置

重要:Telegram 需要您的域名具有 SSL 证书(https)才能设置 webhook。

打开 .env 并填写您的域名

TELEGRAM_WEBHOOK_DOMAIN=www.yourdomain.com

独立启动器包含一个 CLI 辅助工具,用于设置机器人的 webhook。只需运行以下命令。

php telegram webhook:setup <botname>

这将设置您的机器人 webhook 到此 URL

https://<www.yourdomain.com>/webhook.php?token=YourBotToken&bot=YourBotName

webhook 文件将验证任何传入请求以确保其为有效的 Telegram 请求。它将拒绝任何其他请求。因此,您无需担心。

您可以使用事件监听器或直接在 bot/Http/WebhookController.php 中的 listenForUpdate() 方法中直接处理传入的更新。

许可

此项目根据 BSD 3-Clause 许可证 发布。