PHP 的简单 Telegram Bot API 库

v0.0.3 2024-01-08 17:51 UTC

This package is auto-updated.

Last update: 2024-09-08 19:15:27 UTC


README

PHP 的简单 Telegram Bot API 库

快速开始

使用 setToken 初始化,并使用 run 方法结束。

示例 1:启动

require 'Bot.php';

Bot::setToken(BOT_TOKEN, BOT_USERNAME); //init (required)
Bot::start('Welcome, I am a bot.'); //event
Bot::run(); //launch (required)

示例 2:键盘

Bot::chat('/help', function(){
    $keyboard = Bot::keyboard('
    [/info] [/admin]
    [/help]
    ');
    $options = ['reply_markup' => $keyboard];
    return Bot::sendMessage("List of Commands:", $options);
});

示例 3:内联键盘

Bot::chat('/inline_keyboard', function(){
    $inline_keyboard = Bot::inline_keyboard('
    [Google|https://www.google.com] [Facebook|https://#]
    [More|more_data]
    ');
    $options = ['reply_markup' => $inline_keyboard];
    return Bot::sendMessage("Options:", $options);
});

示例 4:发送文档

Bot::chat('/send', function($file){
    if (file_exists($file)) return Bot::sendDocument($file);
    return Bot::sendMessage("$file not exists");
});

示例 5:聊天数组

$help = function(){
    return Bot::sendMessage("This is <b>bold</b> and <i>italic</i> text.", ['parse_mode' => 'html']);
};

$info = function() {
    Bot::sendChatAction('typing');
    Bot::sendDocument('composer.json', ['caption' => 'for composer']);
};

Bot::chat_array([
    '/help' => $help,
    '/info' => $info,
]);

示例 6:正则表达式

Bot::regex('/^\/start (\d+)$/', function($m){
    return Bot::sendMessage($m[1]);
});

Bot::start(function(){
    return Bot::sendMessage('Press button bellow:',
        [
            'reply_markup' => Bot::inline_keyboard('
                [BUTTON|https://t.me/Testing58384bot?start=1234567890]
            ')
        ]
    );
});

文档

注意,所有属性和方法都是静态的。例如: Bot::$propertyBot::method()

Bot 属性

Bot 方法

Telegram 事件

注意事件参数最多2个。例如: Bot::start($param1, $param2)

  • all (当用户发送任何内容时,类似于 on('*', $response) 方法)
  • start (当用户发送 /start 文本消息或点击 START 按钮)
  • text (见: 文档
  • 动画
  • 音频
  • 文档
  • 照片
  • 贴纸
  • 视频
  • 视频笔记
  • 语音
  • 联系人
  • 骰子
  • 游戏
  • 投票
  • 地点
  • 位置
  • 消息自动删除定时器更改
  • 固定消息
  • 发票
  • 成功付款
  • 用户共享
  • 聊天共享
  • 允许写入访问
  • 护照数据
  • 触发接近警报
  • 论坛主题创建
  • 论坛主题编辑
  • 论坛主题关闭
  • 论坛主题重新打开
  • 一般论坛主题隐藏
  • 一般论坛主题取消隐藏
  • 语音聊天计划
  • 语音聊天开始
  • 语音聊天结束
  • 语音聊天参与者被邀请
  • 内联查询
  • 回调查询
  • 编辑后的消息
  • 频道帖子
  • 编辑后的频道帖子

Telegram 方法

所有 Telegram 方法都与该机器人兼容。例如

echo Bot::getMe();
echo Bot::setWebhook(WEBHOOK_URL);
echo Bot::getWebhookInfo();
echo Bot::deleteWebhook(true); //default is `false` for `drop_pending_updates`, see: https://core.telegram.org/bots/api#deletewebhook

查看更多: https://core.telegram.org/bots/api#available-methods