Telegram api php lib

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A PHP library that makes using Telegram Bot API much easier.

License

telegram-bot-php/core

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

- Updated the `filterIncomingUpdates` method. - New Functionalities at `Entities`. - Added a few more tests.

Git stats

Files

Failed to load latest commit information.

README.md

Code Coverage

This library is a simple and easy to use library for creating Telegram API Bots, and this library is designed to provide a platform where one can simply write a bot and have interactions in a matter of minutes.

This is an official announcement of support, which allows integrators of all sorts to bring automated interactions with the Telegram Bot API to their users.

  • The easiest and simplest way for update handling
  • Support for all types and methods according to Telegram Bot API 6.0
  • Handling WebAppData and data encryption/validation
  • Crash handling and error reporting
  • The ability to create advanced Plugins with their asynchronous methods
  • The ability to manage Channels from the bot admin interface
  • Downloading and uploading large files
  • Full support for inline bots
  • Inline keyboard support
  • And many more.
composer require telegram-bot-php/core 

If the above step didn’t work, install composer and try again.

sudo apt-get install curl php-curl curl -s https://getcomposer.org/installer | php php composer.phar install 

Composer not found? Use this command instead:

php composer.phar require "telegram-bot-php/core" 
 require __DIR__ . '/vendor/autoload.php'; $admin_id = 123456789; $bot_token = 'your_bot_token'; \TelegramBot\Telegram::setToken($bot_token); \TelegramBot\CrashPad::setDebugMode($admin_id); $result = \TelegramBot\Request::sendMessage([ 'chat_id' => $admin_id, 'text' => 'text', ]); echo $result->getRawData(false); // >

Create set-hook.php with the following contents:

 require __DIR__ . '/vendor/autoload.php'; \TelegramBot\Telegram::setToken($bot_token); $response = \TelegramBot\Request::setWebhook([ 'url' => 'https://your-domain.com/webhook/' . $bot_token, ]); if ($response->isOk()) < echo $response->getDescription(); exit(0); >

Use self-signed certificate

\TelegramBot\Request::setWebhook([ 'url' => 'https://your-domain.com/webhook/' . $bot_token, 'certificate' => 'path/to/certificate.pem', ]);
\TelegramBot\Request::deleteWebhook();

Create a handler for updates

 use TelegramBot\Entities\Update; use TelegramBotTest\EchoBot\Plugins\MainPlugin; class Handler extends \TelegramBot\UpdateHandler < public function __process(Update $update): void < self::addPlugins([ MainPlugin::class, ]); > >

Filtering incoming updates by their type is easy.

$updateHandler->filterIncomingUpdates([ Update::TYPE_MESSAGE, Update::TYPE_CALLBACK_QUERY, ]);
$updateHandler->filterIncomingUpdates([ Update::TYPE_MESSAGE => function (\TelegramBot\Entities\Update $update) < return $update->getMessage()->getChat()->getId() === 259760855; > ]);

The Plugins are a way to create a bot that can do more than just echo back the message.

Create plugin for Handler class

 use TelegramBot\Entities\Message; use TelegramBot\Entities\WebAppData; class MainPlugin extends \TelegramBot\Plugin < public function onMessage(int $update_id, Message $message): \Generator < if ($message->getText() === '/start') < yield \TelegramBot\Request::sendMessage([ 'chat_id' => $message->getChat()->getId(), 'text' => 'Hello, ' . $message->getFrom()->getFirstName(), ]); > if ($message->getText() === '/ping') < yield \TelegramBot\Request::sendMessage([ 'chat_id' => $message->getChat()->getId(), 'text' => 'pong', ]); > > public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator < yield \TelegramBot\Request::sendMessage([ 'chat_id' => $webAppData->getUser()->getId(), 'text' => 'Hello, ' . $webAppData->getUser()->getFirstName(), ]); > >

Anonymous plugins and handlers

$commands = new class() extends \TelegramBot\Plugin < public function onUpdate(\TelegramBot\Entities\Update $update): \Generator < // Write your code here > >; $admin = new class() extends \TelegramBot\Plugin < // TODO: Write your code here >; (new \TelegramBot\UpdateHandler())->addPlugins([$commands, $admin])->resolve();

Available events and methods

class SomePlugin extends \TelegramBot\Plugin < public function onUpdate(Update $update): \Generator <> public function onMessage(int $update_id, Message $message): \Generator <> public function onEditedMessage(int $update_id, EditedMessage $editedMessage): \Generator <> public function onChannelPost(int $update_id, ChannelPost $channelPost): \Generator <> public function onEditedChannelPost(int $update_id, EditedChannelPost $editedChannelPost): \Generator <> public function onInlineQuery(int $update_id, InlineQuery $inlineQuery): \Generator <> public function onChosenInlineResult(int $update_id, ChosenInlineResult $chosenInlineResult): \Generator <> public function onCallbackQuery(int $update_id, CallbackQuery $callbackQuery): \Generator <> public function onShippingQuery(int $update_id, ShippingQuery $shippingQuery): \Generator <> public function onPreCheckoutQuery(int $update_id, PreCheckoutQuery $preCheckoutQuery): \Generator <> public function onPoll(int $update_id, Poll $poll): \Generator <> public function onPollAnswer(int $update_id, PollAnswer $pollAnswer): \Generator <> public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator <> >

This library supports evey Telegram Bot API method and entity since API version 6.0.

Using CrashPad for reporting error through telegram. just add below to your Update handler.

\TelegramBot\CrashPad::setDebugMode(259760855);

Please report any bugs you find on the issues page.

The Telegram-Bot-PHP Code of Conduct can be found at this document.

Thank you for considering contributing to this project. please open an issue or pull request if you have any suggestions or just email opensource@litehex.com.

The Telegram-Bot-PHP library is open-sourced under the MIT license.

About

A PHP library that makes using Telegram Bot API much easier.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Native PHP Wrapper for Telegram BOT API

License

TelegramBot/Api

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Fix phpDoc for $allowedUpdates parameters

Git stats

Files

Failed to load latest commit information.

README.md

An extended native php wrapper for Telegram Bot API without requirements. Supports all methods and types of responses.

Bots: An introduction for developers

Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats.

You control your bots using HTTPS requests to bot API.

The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. To learn how to create and set up a bot, please consult Introduction to Bots and Bot FAQ.

$ composer require telegram-bot/api
$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN'); $bot->sendMessage($chatId, $messageText);
$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN'); $document = new \CURLFile('document.txt'); $bot->sendDocument($chatId, $document);

Send message with reply keyboard

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN'); $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), true); // true for one-time keyboard $bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);

Send message with inline keyboard

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN'); $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup( [ [ ['text' => 'link', 'url' => 'https://core.telegram.org'] ] ] ); $bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);
$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN'); $media = new \TelegramBot\Api\Types\InputMedia\ArrayOfInputMedia(); $media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727')); $media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727')); // Same for video // $media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaVideo('http://clips.vorwaerts-gmbh.de/VfE_html5.mp4')); $bot->sendMediaGroup($chatId, $media);
require_once "vendor/autoload.php"; try < $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN'); // or initialize with botan.io tracker api key // $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY'); //Handle /ping command $bot->command('ping', function ($message) use ($bot) < $bot->sendMessage($message->getChat()->getId(), 'pong!'); >); //Handle text messages $bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) < $message = $update->getMessage(); $id = $message->getChat()->getId(); $bot->sendMessage($id, 'Your message: ' . $message->getText()); >, function () < return true; >); $bot->run(); > catch (\TelegramBot\Api\Exception $e) < $e->getMessage(); >

Botan SDK (not supported more)

Botan is a telegram bot analytics system based on Yandex.Appmetrica. In this document you can find how to setup Yandex.Appmetrica account, as well as examples of Botan SDK usage.

  • Register at http://appmetrica.yandex.com/
  • After registration you will be prompted to create Application. Please use @YourBotName as a name.
  • Save an API key from settings page, you will use it as a token for Botan API calls.
  • Download lib for your language, and use it as described below. Don`t forget to insert your token!

Since we are only getting started, you may discover that some existing reports in AppMetriсa aren’t properly working for Telegram bots, like Geography, Gender, Age, Library, Devices, Traffic sources and Network sections. We will polish that later.

$tracker = new \TelegramBot\Api\Botan('YOUR_BOTAN_TRACKER_API_KEY'); $tracker->track($message, $eventName);
$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY'); $bot->track($message, $eventName);

You can use method ‘getUpdates()’and all incoming messages will be automatically tracked as Message -event.

$bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');

All registered commands are automatically tracked as command name

Please see CHANGELOG for more information what has changed recently.

If you discover any security related issues, please email mail@igusev.ru instead of using the issue tracker.

The MIT License (MIT). Please see License File for more information.

About

Native PHP Wrapper for Telegram BOT API

Источник

Читайте также:  Сделать задание на html
Оцените статью