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
Bash

Alternatively, you can clone the repository:

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

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

Basic Usage

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

Sending Messages and Media

Sending Text Messages

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

Sending Photos

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

Sending Audio

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

Sending Documents

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

Sending Videos

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

Sending Animations

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

Sending Voice Messages

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

Sending Video Notes

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

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

Sending Locations

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

Sending Venues

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

Sending Contacts

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

Sending Polls

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

Sending Dice

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

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

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

Inline Mode

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

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

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

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

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 Get Free Unlimited Bandwidth and Storage Using jsDelivr and GitHub

How to Get Free Unlimited Bandwidth and Storage Using jsDelivr and GitHub Are you tired of paying for expensive content delivery networks (CDNs) and storage solutions for your web projects? Look no further! In this guide, we'll show you how to leverage jsDelivr and GitHub to get free unlimited bandwidth and storage. Whether you're a seasoned developer or just getting started, this solution will save you money and improve the performance of your web projects. What is jsDelivr? jsDelivr is a free, fast, and reliable CDN for open-source files. It provides a convenient way to serve your static assets (like JavaScript, CSS, images, and more) with the benefits of a global CDN, including faster load times and unlimited bandwidth. What is GitHub? GitHub is a popular platform for version control and collaboration. It allows you to host your code repositories and manage your projects with ease. By combining GitHub with jsD...

Best VS Code extensions for developers in 2024

Here are some of the best VS Code extensions for developers in 2024, including a range of productivity tools, debuggers, and visual enhancements to streamline your coding workflow. Additionally, you'll find some popular themes to customize your editor's appearance. Top VS Code Extensions for Developers in 2024 Shade Theme by SH20RAJ Enhance your code readability with this well-designed theme, perfect for long coding sessions. Shade Theme Prettier A widely used code formatter that ensures your code is styled consistently across your projects. Prettier GitLens Provides rich visualizations and insights into your Git repository, helping you understand code changes and history. GitLens Auto Rename Tag Automatically renames paired HTML/XML tags, reducing errors and saving time. Auto Rename Tag Bracket Pair Colorizer Colors matching brackets to improve code readability, especially useful for complex nested structures. Bracket Pair Colorizer CSS Peek...

Top 50 Useful Regex Patterns

Title: Mastering Regular Expressions in JavaScript: Top 50 Useful Regex Patterns Introduction: Regular expressions (regex) are a powerful tool for pattern matching and text manipulation in JavaScript. Whether you're validating user input, extracting data from strings, or replacing text, regex can streamline your coding tasks. In this article, we'll explore 50 useful regex patterns that every JavaScript developer should know. These regex patterns cover a wide range of common scenarios, from validating email addresses to parsing URLs and beyond. 1. Validating Email Addresses: const emailRegex = /^[ \w -]+( \. [ \w -]+)*@([ \w -]+ \. )+[a-zA-Z]{2,7}$/; 2. Validating URLs: const urlRegex = /^(https?: \/ \/ )?([ \d a-z.-]+) \. ([a-z.]{2,6})([/ \w .-]*)* \/ ?$/; 3. Validating Dates in YYYY-MM-DD Format: const dateRegex = /^ \ d {4} - \ d {2} - \ d {2} $/; 4. Validating Phone Numbers (US Format): const phoneRegex = /^ \ ( ?( \ d {3} ) \ ) ?[- ]?( \ d {3} )[- ]?( \ d {4} )...

Random Posts