mitokande/php-telegram

一个用于发送Telegram机器人消息的PHP包

1.0.2 2023-08-30 11:04 UTC

This package is auto-updated.

Last update: 2024-09-30 01:51:17 UTC


README

#PHP TELEGRAM 由 Mithat Can Turan 编写

V1.0.2

更新内容是什么?

-现在您可以使用 TelegramBotCreator 类创建您的机器人。

-向 TelegramBot 类添加了发送多条消息的功能。

首先使用composer导入我的包

composer require mitokande/php-telegram
use Mitokande\PhpTelegram\TelegramBotCreator;
use Mitokande\PhpTelegram\TelegramMessage;

TelegramBotCreator::SetTelegramBot(__Your_Token__);
TelegramBotCreator::$TelegramBot->SendTelegramMessage(new TelegramMessage(__RECEIVER_CHAT_ID__, __YOUR_MESSAGE_));

对于一次性向多个用户发送消息

use Mitokande\PhpTelegram\TelegramBotCreator;
use Mitokande\PhpTelegram\TelegramMessage;

TelegramBotCreator::SetTelegramBot(__Your_Token__);
TelegramBotCreator::$TelegramBot->SendMultipleTelegramMessage(__ARRAY_OF_CHAT_ID_STRINGS__, __YOUR_MESSAGE__);

如果您要在类中使用它,您可以在类的构造函数中创建您的机器人,并在所有方法中使用静态的bot变量。

use Mitokande\PhpTelegram\TelegramBotCreator;
use Mitokande\PhpTelegram\TelegramMessage;
use Mitokande\PhpTelegram\TelegramMessageResult;

class TelegramMessager
{
    public function __construct()
    {
        TelegramBotCreator::SetTelegramBot(__Your_Token__);
    }

    public function SendWelcomeMessage($chat_id): TelegramMessageResult
    {
        $bot = TelegramBotCreator::$TelegramBot;
        return $bot->SendTelegramMessage(new TelegramMessage($chat_id, __WELCOME_MESSAGE_FOR_USERS__));
    }
    public function SendPingMessage($chat_id): TelegramMessageResult
    {
        return TelegramBotCreator::$TelegramBot->SendTelegramMessage(new TelegramMessage(__RECEIVER_CHAT_ID__, __YOUR_MESSAGE_));
    }
    public function SendMultiplePingMessage($chat_id_list): TelegramMessageResult
    {
        return TelegramBotCreator::$TelegramBot->SendMultipleTelegramMessage($chat_id_list, __YOUR_MESSAGE_));
    }
}

V1.0.1 首先使用composer导入我的包

composer require mitokande/php-telegram@1.0.2

然后在您的代码中导入所需的类

use Mitokande\PhpTelegram\TelegramBot;
use Mitokande\PhpTelegram\TelegramMessage;

使用您的Token创建一个Telegram机器人实例

$bot = TelegramBot::GetInstance(__Your_Token__);

请注意,您要发送消息的用户必须与您的机器人开始过对话。创建一个带有 chatID 和消息内容的Telegram消息

$newMessage = new TelegramMessage(__Your_ChatID__, __Your_Message__);

使用您的机器人发送消息并获取 TelegramMessageResult

$result = $bot->SendTelegramMessage($newMessage);