Skip to main content

PhpGram - A PHP library for interacting with the Telegram Bot API.

PhpGram

PhpGram is a PHP library for interacting with the Telegram Bot API, providing easy-to-use methods for sending messages, media, managing chats, stickers, inline queries, payments, and more.

{% github https://github.com/SH20RAJ/phpgram %}

Table of Contents

Installation

Install PhpGram via Composer:

composer require sh20raj/phpgram

Alternatively, you can clone the repository:

git clone https://github.com/SH20RAJ/phpgram.git

Usage

Initialization

First, include the library in your PHP file and initialize PhpGram with your bot token:

require_once 'path/to/phpgram.php';

$token = 'YOUR_BOT_TOKEN';
$bot = new PhpGram($token);

Basic Usage

// Example: Get bot information
$botInfo = $bot->getMe();
echo 'Bot Username: ' . $botInfo['result']['username'] . PHP_EOL;

Sending Messages and Media

Sending Text Messages

// Send a text message
$chatId = 'YOUR_CHAT_ID';
$message = 'Hello from PhpGram!';
$response = $bot->sendMessage($chatId, $message);

Sending Photos

// Send a photo
$photoPath = 'path/to/photo.jpg';
$response = $bot->sendPhoto($chatId, $photoPath, ['caption' => 'Check out this photo!']);

Sending Audio

// Send an audio file
$audioPath = 'path/to/audio.mp3';
$response = $bot->sendAudio($chatId, $audioPath, ['caption' => 'Listen to this audio!']);

Sending Documents

// Send a document
$documentPath = 'path/to/document.pdf';
$response = $bot->sendDocument($chatId, $documentPath, ['caption' => 'Here is your document.']);

Sending Videos

// Send a video
$videoPath = 'path/to/video.mp4';
$response = $bot->sendVideo($chatId, $videoPath, ['caption' => 'Watch this video!']);

Sending Animations

// Send an animation
$animationPath = 'path/to/animation.gif';
$response = $bot->sendAnimation($chatId, $animationPath, ['caption' => 'Enjoy this animation!']);

Sending Voice Messages

// Send a voice message
$voicePath = 'path/to/voice.ogg';
$response = $bot->sendVoice($chatId, $voicePath, ['caption' => 'Listen to this voice message!']);

Sending Video Notes

// Send a video note
$videoNotePath = 'path/to/video_note.mp4';
$response = $bot->sendVideoNote($chatId, $videoNotePath);

Sending Media Groups

// Send a media group
$mediaGroup = [
    ['type' => 'photo', 'media' => 'path/to/photo1.jpg'],
    ['type' => 'photo', 'media' => 'path/to/photo2.jpg'],
];
$response = $bot->sendMediaGroup($chatId, $mediaGroup);

Sending Locations

// Send a location
$response = $bot->sendLocation($chatId, 40.712776, -74.005974); // New York City coordinates

Sending Venues

// Send a venue
$response = $bot->sendVenue($chatId, 40.712776, -74.005974, 'Venue Name', 'Venue Address');

Sending Contacts

// Send a contact
$response = $bot->sendContact($chatId, 'PHONE_NUMBER', 'FirstName', ['last_name' => 'LastName']);

Sending Polls

// Send a poll
$response = $bot->sendPoll($chatId, 'Your Question?', ['Option 1', 'Option 2']);

Sending Dice

// Send a dice
$response = $bot->sendDice($chatId);

Managing Chats and Members

// 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');

Handling Stickers

// 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');

Inline Mode

// Answer an inline query
$inlineQueryId = 'INLINE_QUERY_ID';
$results = [ /* Array of InlineQueryResult objects */ ];
$response = $bot->answerInlineQuery($inlineQueryId, $results);

Payments

// 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);

Games

// 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);

Handling Updates

// 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();

Contributing

Contributions are welcome! Fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

████████████████████████████████████████████████████████████  ██╗  ██╗███████╗██╗     ██╗      ██████╗
████████████████████████████████████████████████████████████  ██║  ██║██╔════╝██║     ██║     ██╔═══██╗
███████████████████████████████████`.        ╙██████████████  ███████║█████╗  ██║     ██║     ██║   ██║
████████████████████████████████▀  ¿▓▓▓▓▓▓▓▓▄/ "████████████  ██╔══██║██╔══╝  ██║     ██║     ██║   ██║
██████████████████████████████▀.  ▓▓▓▓▓▓▓▓▓▓▓▓   ▐██████████  ██║  ██║███████╗███████╗███████╗╚██████╔╝▄█╗
██████████████████████████████ `  ▓▓▓▓▓▓▓▓▓▓▓▓  ` ██████████  ╚═╝  ╚═╝╚══════╝╚══════╝╚══════╝ ╚═════╝ ╚═╝
██████████████████████████████ `  ▓▓▓▓▓▓▓▓▓▓▓▓   ▄██████████
▀██████████████████████████████▌  ▀▀▓▓▓▓▓▓▓▌╓╖. ████████████  ███╗   ██╗██╗ ██████╗███████╗  ████████╗ ██████╗
█▄▀██████████████████████████████▄ ╩╦╙▀▀▀▀▀ ╣`,█████████████  ████╗  ██║██║██╔════╝██╔════╝  ╚══██╔══╝██╔═══██╗
▄▀█▄╙█████████████████████▀▀▀▀█████▄▄ .... ,▄███████▀███████  ██╔██╗ ██║██║██║     █████╗       ██║   ██║   ██║
██▄▀█▄╙█████████████████▀  ╪╢%╦══~╓,└ ╚▒▒▒ ╙▀|,╓╓═╤H   ▀████  ██║╚██╗██║██║██║     ██╔══╝       ██║   ██║   ██║
█▀▀▀-▀█▌▄▀█████████████   ║▒▒▒▒▒▒▒▒▒▒╢╦ ╘ -╣▒▒▒▒▒▒▒▒▒╢╕   ▀█  ██║ ╚████║██║╚██████╗███████╗     ██║   ╚██████╔╝
██▄▀██└║▄▄▄████████████▄          ═╕╕╕╕╕═╕═══════       ▄▄▄▄  ╚═╝  ╚═══╝╚═╝ ╚═════╝╚══════╝     ╚═╝    ╚═════╝
████▄▀█▌║███  ████████▌         ╕   ╩▒▒▒▒▒▒▒▒▒ร‘          ███
██████▌ร–▓▌   ▀██████████`╔▒▒╣ █ ▒▒m   ╚▒╢▒▒▒╩ -╣▒ ▌ ▒▒▒ ████  ███╗   ███╗███████╗███████╗████████╗  ██╗   ██╗ ██████╗ ██╗   ██╗
████ -"" ∞╙,▀.╙▀███████╜ ▒▒▒ ▄█ ร‘   -   S.  ═▒▒▒▒ █ ║▒▒╕└███  ████╗ ████║██╔════╝██╔════╝╚══██╔══╝  ╚██╗ ██╔╝██╔═══██╗██║   ██║
████████▄ -«   ∞▄.▀",╓═     ╒██   ═╣▒▒ `ร‘╛        █▌ ▒▒▒ ███  ██╔████╔██║█████╗  █████╗     ██║      ╚████╔╝ ██║   ██║██║   ██║
█████████▌ ยบ     ╤╣▒╣╩^",▄▄███▀  ▒▒╣"     ''''''' ▀▀     `██  ██║╚██╔╝██║██╔══╝  ██╔══╝     ██║       ╚██╔╝  ██║   ██║██║   ██║
█████████  ▌       ▄▄████████─         ---------    L'▒▒▒ ██  ██║ ╚═╝ ██║███████╗███████╗   ██║        ██║   ╚██████╔╝╚██████╔╝
▀▀▀▀▀▀▀▀▀▀▀▀▀-     ▀▀▀▀▀▀▀▀▀▀       '╧╧╧╧╧╧╧╧╧`     ╚ ╧╧╧- ▀  ╚═╝     ╚═╝╚══════╝╚══════╝   ╚═╝        ╚═╝    ╚═════╝  ╚═════╝

Comments

Popular posts from this blog

How to Add a VS Code Editor to Your Website

How to Add a VS Code Editor to Your Website The Monaco editor by Microsoft provides a code editor component that can be easily integrated into websites. With just a few lines of code, you can add a full-featured editor similar to VS Code in your web app. In this tutorial, we'll see how to do just that. Getting Started To use Monaco, we need to include it in our page. We can get it from a CDN: < script src = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs/loader.min.js" > </ script > This will load the Monaco library asynchronously. Next, we need a <div> in our HTML where we can instantiate the editor: < div id = "editor" ></ div > Now in our JavaScript code, we can initialize Monaco and create the editor: require .config({ paths : { 'vs' : 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs' }}); require ([ "vs/editor/editor.main" ], function ( ) { const ...

10 Free GitHub Copilot Alternatives for VS Code in 2024

10 Free GitHub Copilot Alternatives for VS Code in 2024 As developers, we're always on the lookout for tools that can help us write code more efficiently. GitHub Copilot has been a game-changer in this regard, but its premium pricing may be a deterrent for some. Fortunately, there are several free alternatives available that offer similar functionality. In this article, we'll explore 10 of the best free GitHub Copilot alternatives for Visual Studio Code in 2024. Comparison of Free GitHub Copilot Alternatives Tool Language Support Auto-Completion Code Generation Code Explanation Bito Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✓ Tabnine Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✗ Amazon CodeWhisperer Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP ✓ ✓ ✗ Codeium Python, JavaScript, TypeScript, Java, C#, C...

Top React UI Libraries ๐ŸŒŸ

๐ŸŒŸ The Ultimate Roundup of Top React UI Libraries for Your Next Project! ๐Ÿš€๐ŸŽจ Hey there, React wizards! ๐Ÿช„✨ Ready to take your frontend game to the next level? Let's dive into an even broader spectrum of incredible React UI libraries that'll make your interfaces shine like never before! ๐Ÿ’ป๐ŸŒˆ 1. Tremor UI ๐ŸŒŠ ๐ŸŒŸ Tremor UI is a rising star in the React UI galaxy! ✨ It offers a sleek and modern design language, perfect for crafting stylish buttons and more. ๐Ÿ”˜๐ŸŽจ With Tremor, you can effortlessly create eye-catching user interfaces with its intuitive API and customizable components. ๐Ÿช„✨ Key Features : Modern Design Aesthetic Easy Customization Focus on User Experience 2. Radix UI ๐ŸŒฑ ๐ŸŒŸ Radix UI is all about building accessible, powerful components for React. ๐Ÿ› ️๐Ÿ”ฉ From modals to tooltips, Radix UI provides a solid foundation for creating interactive and user-friendly interfaces. ๐ŸŒ๐Ÿงก Dive into Radix ...

Random Posts