Giter Site home page Giter Site logo

telegram-bot-php / core Goto Github PK

View Code? Open in Web Editor NEW
42.0 1.0 6.0 327 KB

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

Home Page: https://telegram.litehex.com/

License: MIT License

PHP 100.00%
php telegram api bot bot-api php-telegram-bot sdk-php

core's Introduction

logo

Build Status Code Quality Code Intelligence Status Code Coverage

Latest Stable Version Code Size Downloads License


Telegram Bot PHP

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.

Table of Contents

Introduction

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.

This library features:

  • 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...

Installation

composer require telegram-bot-php/core
Click for help with installation

Install Composer

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

Debian / Ubuntu

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"

Windows:

Download installer for Windows

Getting started

<?php
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); // {"ok": true, "result": {...}}

Webhook

Create set-hook.php with the following contents:

<?php
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',
]);

Delete webhook

\TelegramBot\Request::deleteWebhook();

Update Handling

Create a handler for updates

<?php

use TelegramBot\Entities\Update;
use TelegramBotTest\EchoBot\Plugins\MainPlugin;

class Handler extends \TelegramBot\UpdateHandler {

   public function __process(Update $update): void {
      self::addPlugins([
         MainPlugin::class,
      ]);
   }

}

Filter incoming updates

Filtering incoming updates by their type is easy.

$updateHandler->filterIncomingUpdates([
   Update::TYPE_MESSAGE,
   Update::TYPE_CALLBACK_QUERY,
]);

Or just go advanced:

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

Plugins

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

Create plugin for Handler class

<?php

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 {}

}

Supports

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

Error Handling

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

\TelegramBot\CrashPad::setDebugMode(259760855);

Troubleshooting

Please report any bugs you find on the issues page.

Code of Conduct

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

Contributing

Thank you for considering contributing to this project. please open an issue or pull request if you have any suggestions or just email [email protected].

License

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

core's People

Contributors

gekkone avatar shahradelahi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

core's Issues

DEPRECATED: Creation of dynamic property

This is my cod:

   
        <?php
        $br = PHP_EOL;
        $sp = DIRECTORY_SEPARATOR;
        require_once __DIR__ . $sp . "vendor" . $sp . "autoload.php";

        use TelegramBot\Entities\Update;
        use TelegramBot\Telegram;
        use TelegramBot\Request;

        $admin_id = 1269777916;
        $bot_token = "5801896630:AAEd0Z1FhgLZCFQqIbDOxe5E5KUj0ZlgRKw";
        $hook_url = "WEB HOOK URL";

        Telegram::setToken($bot_token);
        Telegram::setAdminId($admin_id);

        $response = Request::setWebhook([
            "url" => $hook_url,
            "certificate" => "./.cert/cert.crt"
        ]);

        if ($response->isOk()) {
            $result = Request::sendMessage([
                "chat_id" => $admin_id,
                "text" => $response->getResult()
            ]);
            echo $response->getDescription();
            exit(0);
        }

        print_r(Update::TYPE_MESSAGE);
        print_r($result->getRawData(false));

        ?>
      

This is my output:

      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$curlHandle is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 46



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$statusCode is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$headerSize is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$headers is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 80



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$body is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property TelegramBot\Entities\Response::$ok is deprecated in /data/data/com.termux/files/home/IA/vendor/telegram-bot-php/core/src/Entity.php on line 42



      Deprecated:  Creation of dynamic property TelegramBot\Entities\Response::$result is deprecated in /data/data/com.termux/files/home/IA/vendor/telegram-bot-php/core/src/Entity.php on line 42



      Deprecated:  Creation of dynamic property TelegramBot\Entities\Response::$description is deprecated in /data/data/com.termux/files/home/IA/vendor/telegram-bot-php/core/src/Entity.php on line 42



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$curlHandle is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 46



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$statusCode is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$headerSize is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$headers is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 80



      Deprecated:  Creation of dynamic property EasyHttp\Model\HttpResponse::$body is deprecated in /data/data/com.termux/files/home/IA/vendor/shahradelahi/easy-http/src/Model/HttpResponse.php on line 117



      Deprecated:  Creation of dynamic property TelegramBot\Entities\Response::$ok is deprecated in /data/data/com.termux/files/home/IA/vendor/telegram-bot-php/core/src/Entity.php on line 42



      Deprecated:  Creation of dynamic property TelegramBot\Entities\Response::$result is deprecated in /data/data/com.termux/files/home/IA/vendor/telegram-bot-php/core/src/Entity.php on line 42
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.