miqdadyyy/laraveltelegrambot

laravel 6 的 Telegram Bot

v1.0 2020-02-13 10:30 UTC

This package is auto-updated.

Last update: 2024-09-13 20:57:28 UTC


README

Laravel 6 包用于管理 Telegram Bot

准备

  • BotFather上创建自己的机器人
  • 将您的机器人密钥复制到您的.env文件中

TELEGRAM_BOT_KEY=1234567890:您的机器人密钥

  • 运行php artisan config:clear

安装

通过Composer

$ composer require miqdadyyy/laraveltelegrambot

用法

方法

获取机器人ID

此方法用于获取您的机器人身份

$result = LaravelTelegramBot::getId()

请求参数:-

响应

{
  "ok": true,
  "result": {
    "id": 123456789,
    "is_bot": true,
    "first_name": "MyExampleBot",
    "username": "my_example_bot",
    "can_join_groups": false,
    "can_read_all_group_messages": false,
    "supports_inline_queries": false
  }
}

获取更新

此方法用于获取所有您的机器人更新。机器人更新是用户到您的机器人的消息或回调

$result = LaravelTelegramBot::getUpdates()

请求参数:-

响应

{
  "ok": true,
  "result": [
    {
      "update_id": 123456789,
      "message": {
        "message_id": 1,
        "from": {
          "id": 123456789,
          "is_bot": false,
          "first_name": "Miqdad",
          "last_name": "Farcha",
          "username": "miqdadyyy"
        },
        "chat": {
          "id": 123456789,
          "first_name": "Miqdad",
          "last_name": "Farcha",
          "username": "miqdadyyy",
          "type": "private"
        },
        "date": 1581514530,
        "text": "/start",
        "entities": [
          {
            "offset": 0,
            "length": 6,
            "type": "bot_command"
          }
        ]
      }
    },
    {
      "update_id": 123456789,
      "message": {
        "message_id": 2,
        "from": {
          "id": 12345678,
          "is_bot": false,
          "first_name": "Miqdad",
          "last_name": "Farcha",
          "username": "miqdadyyy",
          "language_code": "en"
        },
        "chat": {
          "id": 12345678,
          "first_name": "Miqdad",
          "last_name": "Farcha",
          "username": "miqdadyyy",
          "type": "private"
        },
        "date": 1581514536,
        "text": "/id",
        "entities": [
          {
            "offset": 0,
            "length": 3,
            "type": "bot_command"
          }
        ]
      }
    }
  ]
}

创建消息

您可以通过chat_id发送消息给用户。

$message = LaravelTelegramBot::createMessage($chat_id, $text, $options);
  • $chat_id:您的客户端Telegram ID(字符串)
  • $text:发送给客户端的消息(字符串)
  • $options:发送消息的几个选项(数组)

这里有几个适用于Telegram机器人的选项

在创建带有多个选项的消息后,如果您要发送消息,应该添加

$message->send();

当您想要直接发送消息时,您可以使用

LaravelTelegramBot::sendMessage($chat_id, $text, $options);

内联键盘

此对象表示内联键盘的一个按钮。

示例
Inline Button

创建内联键盘

$inline_keyboard = new InlineKeyboard();

向键盘添加行

$row1 = new InlineKeyboardRow();
$inline_keyboard->addRow($row);

但您需要为每一行添加按钮

$row->addButton($title, $callback);

在这个包中,有2种内联键盘按钮类型(URL和回调),您可以为回调参数传递一个query_callback或一个url

创建类似示例按钮的完整代码是

$inline_keyboard = new InlineKeyboard(); // create inline keyboard

$row1 = new InlineKeyboardRow(); // create first row
$row1->addButton('Say Hello Juga', 'http://www.example.com'); // add first button on first row
$row1->addButton('Say Hello Juga 2', 'callback_query'); // add second button on first row
$inline_keyboard->addRow($row); // add row to inline keyboard

// There are a method to add a single button to inline keyboard
$inline_keyboard->addSingleLineButton('Say Hello Juga 3', 'i dont know what');

// then add keyboard to a message
$message->addInlineKeyboard($inline_keyboard);
$message->send(); // send message to user

如果您只想在消息中添加单个按钮,只需

$message->addSingleInlineButton('Hello', 'http://miqdad.codes');
// then send the message
$message->send();

键盘按钮

键盘按钮看起来像Inline Button

在这个包中,键盘按钮总是显示出来,但您可以更改按钮。当按钮被选中时,它只会向机器人发送一个消息,该消息是按钮的标题

为了使键盘按钮比内联键盘更容易使用

$keyboard = [
    ['Hello'],
    ['Help', 'Login']    
];
// set keyboard button from array
$message->setKeyboardButton($keyboard);
// send the messagee
$message->send();

Webhook

此webhook用于从用户获取消息到您的应用程序,您可以自定义地响应他们的消息。

要求

  • 您应该有一个带有ssl保护(https)的域名
  • 您的.env中的BASE_URL应该与您的域名相同

安装Webhook

  • 要设置webhook url,运行php artisan telegrambot:webhook
  • 然后通过运行php artisan vendor:publish --tag=telegrambot-webhook提取控制器
  • 然后它将在App\Http\Controllers\TelegramBot\WebhookUpdate.php上生成一个控制器

移除Webhook

当您将webhook安装到您的机器人时,方法getUpdates()将不会在开发中使用。要移除webhook,只需运行

php artisan telegrambot:webhook --option=remove

安全

如果您发现任何与安全相关的问题,请通过miqdad.farcha@gmail.com发送电子邮件,而不是使用问题跟踪器。

致谢

许可证

MIT。有关更多信息,请参阅许可证文件