sh20raj / phpgram
一个用于与Telegram机器人API交互的PHP库。
v1
2024-07-18 02:44 UTC
Requires
- php: >=7.0
- ext-curl: *
README
PhpGram是一个用于与Telegram机器人API交互的PHP库,提供了发送消息、媒体、管理聊天、贴纸、内联查询、支付等功能,使用简单。
目录
安装
通过 Composer 安装 PhpGram
composer require sh20raj/phpgram
或者,你可以克隆仓库
git clone https://github.com/SH20RAJ/phpgram.git
使用
初始化
首先,在你的PHP文件中包含库,并使用你的机器人令牌初始化 PhpGram
require __DIR__ . '/../src/PhpGram.php'; use PhpGram\PhpGram; $token = 'YOUR_BOT_TOKEN'; $bot = new PhpGram($token);
基本使用
示例: Toss Bot (掷硬币或摇骰子得到结果)
// Example: Get bot information $botInfo = $bot->getMe(); echo 'Bot Username: ' . $botInfo['result']['username'] . PHP_EOL;
发送消息和媒体
发送文本消息
// Send a text message $chatId = 'YOUR_CHAT_ID'; $message = 'Hello from PhpGram!'; $response = $bot->sendMessage($chatId, $message);
发送照片
// Send a photo $photoPath = 'path/to/photo.jpg'; $response = $bot->sendPhoto($chatId, $photoPath, ['caption' => 'Check out this photo!']);
发送音频
// Send an audio file $audioPath = 'path/to/audio.mp3'; $response = $bot->sendAudio($chatId, $audioPath, ['caption' => 'Listen to this audio!']);
发送文档
// Send a document $documentPath = 'path/to/document.pdf'; $response = $bot->sendDocument($chatId, $documentPath, ['caption' => 'Here is your document.']);
发送视频
// Send a video $videoPath = 'path/to/video.mp4'; $response = $bot->sendVideo($chatId, $videoPath, ['caption' => 'Watch this video!']);
发送动画
// Send an animation $animationPath = 'path/to/animation.gif'; $response = $bot->sendAnimation($chatId, $animationPath, ['caption' => 'Enjoy this animation!']);
发送语音消息
// Send a voice message $voicePath = 'path/to/voice.ogg'; $response = $bot->sendVoice($chatId, $voicePath, ['caption' => 'Listen to this voice message!']);
发送视频笔记
// Send a video note $videoNotePath = 'path/to/video_note.mp4'; $response = $bot->sendVideoNote($chatId, $videoNotePath);
发送媒体组
// Send a media group $mediaGroup = [ ['type' => 'photo', 'media' => 'path/to/photo1.jpg'], ['type' => 'photo', 'media' => 'path/to/photo2.jpg'], ]; $response = $bot->sendMediaGroup($chatId, $mediaGroup);
发送位置
// Send a location $response = $bot->sendLocation($chatId, 40.712776, -74.005974); // New York City coordinates
发送地点
// Send a venue $response = $bot->sendVenue($chatId, 40.712776, -74.005974, 'Venue Name', 'Venue Address');
发送联系人
// Send a contact $response = $bot->sendContact($chatId, 'PHONE_NUMBER', 'FirstName', ['last_name' => 'LastName']);
发送投票
// Send a poll $response = $bot->sendPoll($chatId, 'Your Question?', ['Option 1', 'Option 2']);
发送骰子
// Send a dice $response = $bot->sendDice($chatId);
管理聊天和成员
// Kick a member from a chat $userId = 'USER_ID_TO_KICK'; $response = $bot->kickChatMember($chatId, $userId); // Unban a member from a chat $response = $bot->unbanChatMember($chatId, $userId); // Restrict a member in a chat $permissions = ['can_send_messages' => false]; $response = $bot->restrictChatMember($chatId, $userId, $permissions); // Promote a member to an admin $response = $bot->promoteChatMember($chatId, $userId); // Set custom title for an admin $response = $bot->setChatAdministratorCustomTitle($chatId, $userId, 'Custom Title');
处理贴纸
// Send a sticker $stickerPath = 'path/to/sticker.webp'; $response = $bot->sendSticker($chatId, $stickerPath); // Get a sticker set $stickerSetName = 'sticker_set_name'; $response = $bot->getStickerSet($stickerSetName); // Upload a sticker file $stickerFilePath = 'path/to/sticker.png'; $response = $bot->uploadStickerFile($userId, $stickerFilePath); // Create a new sticker set $stickerParams = [ 'name' => 'sticker_set_name', 'title' => 'Sticker Set Title', 'png_sticker' => 'path/to/sticker.png', 'emojis' => '😀', ]; $response = $bot->createNewStickerSet($userId, $stickerParams); // Add a sticker to a set $response = $bot->addStickerToSet($userId, 'sticker_set_name', 'path/to/sticker.png', '😀'); // Set sticker position in a set $response = $bot->setStickerPositionInSet('sticker_file_id', 0); // Delete a sticker from a set $response = $bot->deleteStickerFromSet('sticker_file_id');
内联模式
// Answer an inline query $inlineQueryId = 'INLINE_QUERY_ID'; $results = [ /* Array of InlineQueryResult objects */ ]; $response = $bot->answerInlineQuery($inlineQueryId, $results);
支付
// Send an invoice $invoiceParams = [ 'title' => 'Product Name', 'description' => 'Description of the product', 'payload' => 'unique_payload', 'provider_token' => 'PROVIDER_PAYMENT_TOKEN', 'start_parameter' => 'start_param', 'currency' => 'USD', 'prices' => json_encode([ ['label' => 'Product Price', 'amount' => 1000] ]), ]; $response = $bot->sendInvoice($chatId, $invoiceParams); // Answer a shipping query $shippingQueryId = 'SHIPPING_QUERY_ID'; $response = $bot->answerShippingQuery($shippingQueryId, true); // Answer a pre-checkout query $preCheckoutQueryId = 'PRE_CHECKOUT_QUERY_ID'; $response = $bot->answerPreCheckoutQuery($preCheckoutQueryId, true);
游戏
// Send a game $gameShortName = 'game_short_name'; $response = $bot->sendGame($chatId, $gameShortName); // Set game score $response = $bot->setGameScore($userId, 100); // Get game high scores $response = $bot->getGameHighScores($userId);
处理更新
// Get updates $updates = $bot->getUpdates(); // Set a webhook $response = $bot->setWebhook('https://yourdomain.com/webhook'); // Delete a webhook $response = $bot->deleteWebhook(); // Get webhook info $response = $bot->getWebhookInfo();
贡献
欢迎贡献!克隆仓库,做出你的更改,并提交拉取请求。
许可
本项目采用MIT许可证。有关详细信息,请参阅LICENSE文件。
重要链接
https://dev.to/sh20raj/how-to-create-a-telegram-bot-using-php-4hbd https://dev.to/sh20raj/phpgram-a-php-library-for-interacting-with-the-telegram-bot-api-3pip