使用 PHP 创建 Telegram 机器人的简单库

维护者

详细信息

github.com/pembuatan/bot

主页

源代码

安装: 49

依赖: 0

建议: 0

安全: 0

星级: 0

关注者: 0

分支: 1

v0.0.2 2023-09-27 03:44 UTC

This package is auto-updated.

Last update: 2024-09-27 05:59:38 UTC


README

PHP 的简单 Telegram 机器人 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()

机器人属性

机器人方法

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