PHPackages                             fonclub/telegram-bot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. fonclub/telegram-bot

ActiveLibrary[API Development](/categories/api)

fonclub/telegram-bot
====================

PHP Telegram bot

0.81.0(2y ago)024MITPHPPHP ^7.3|^8.0

Since Sep 3Pushed 2y agoCompare

[ Source](https://github.com/fonclub/telegram-bot)[ Packagist](https://packagist.org/packages/fonclub/telegram-bot)[ Docs](https://github.com/php-telegram-bot/core)[ RSS](/packages/fonclub-telegram-bot/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (77)Used By (0)

 PHP Telegram Bot

 [![PHP Telegram Bot logo](https://raw.githubusercontent.com/php-telegram-bot/assets/master/logo/512px/logo_plain.png "PHP Telegram Bot")](https://raw.githubusercontent.com/php-telegram-bot/assets/master/logo/512px/logo_plain.png)

============================================================================================================================================================================================================================================================

[](#----php-telegram-bot----)

A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.org/bots/api "Telegram Bot API")

[![API Version](https://camo.githubusercontent.com/4d9565de3f957d358185b4383b198ab234b367dbf8adac237cbeb059fe3570c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426f742532304150492d362e37253230253238417072696c253230323032332532392d3332613264612e737667)](https://core.telegram.org/bots/api-changelog#april-21-2023)[![Join the bot support group on Telegram](https://camo.githubusercontent.com/471c1499869f264b2640d365d2b92f8f0461afb6d9eef50960e862b37c08b93f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74656c656772616d2d405048505f5f54656c656772616d5f5f426f745f5f537570706f72742d3634363539642e737667)](https://telegram.me/PHP_Telegram_Bot_Support)[![Donate](https://camo.githubusercontent.com/e529dd950cc97c0a203bb51d44eb58499d67bb2a4e7dde44b6a895254abe3fec/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2546302539462539322539392d446f6e617465253230253246253230537570706f727425323055732d626c75652e737667)](#donate)

[![Tests](https://github.com/php-telegram-bot/core/actions/workflows/tests.yaml/badge.svg)](https://github.com/php-telegram-bot/core/actions/workflows/tests.yaml)[![Code Coverage](https://camo.githubusercontent.com/a5485a616d70b8b99c67fe86ab798c63de0c0c4a010ee9856f974b47203ce2a2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7068702d74656c656772616d2d626f742f636f72652f6d61737465722e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/php-telegram-bot/core/?b=master)[![Code Quality](https://camo.githubusercontent.com/ef5eb7391f890c02920f14904072f6582d74e859356e8e22965d3ae554e3a6a6/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7068702d74656c656772616d2d626f742f636f72652f6d61737465722e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/php-telegram-bot/core/?b=master)[![Latest Stable Version](https://camo.githubusercontent.com/b3d7db2a7b2f0351e9a1f8444fbe9228063860a9b88d355404f46dd4bc38c92e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f6e676d616e2f74656c656772616d2d626f742e737667)](https://packagist.org/packages/longman/telegram-bot)[![Dependencies](https://camo.githubusercontent.com/c18997f20ab9e6a98e61224314759dfe37eb6823f328a7b7b965319b8f495731/68747470733a2f2f746964656c6966742e636f6d2f6261646765732f6769746875622f7068702d74656c656772616d2d626f742f636f72653f7374796c653d666c6174)](https://tidelift.com/subscription/pkg/packagist-longman-telegram-bot?utm_source=packagist-longman-telegram-bot&utm_medium=referral&utm_campaign=enterprise&utm_term=repo "Learn more about the Tidelift Subscription")[![Total Downloads](https://camo.githubusercontent.com/b65ad1c4cc323e9b0c24955112fe0b4210526abba4ed30ab1a29d29deacaad9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f6e676d616e2f74656c656772616d2d626f742e737667)](https://packagist.org/packages/longman/telegram-bot)[![Downloads Month](https://camo.githubusercontent.com/f65f9987e2bb11356c941e8ddd89a12d42503d02cb8fcea0078adb7b174f7f45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6c6f6e676d616e2f74656c656772616d2d626f742e737667)](https://packagist.org/packages/longman/telegram-bot)[![Minimum PHP Version](https://camo.githubusercontent.com/8fbb4670ed6b24972b0bcc5e731ae7b8fcc345234210163bc96177d7c435db76/687474703a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344372e332d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/aad9aebce7ea1c1ec50fb209f26b9a9fc1fd27944a512546ed9fbcee4cec682c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6f6e676d616e2f74656c656772616d2d626f742e737667)](https://github.com/php-telegram-bot/core/LICENSE)

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Instructions](#instructions)
    - [Create your first bot](#create-your-first-bot)
    - [Require this package with Composer](#require-this-package-with-composer)
    - [Choose how to retrieve Telegram updates](#choose-how-to-retrieve-telegram-updates)
- [Using a custom Bot API server](#using-a-custom-bot-api-server)
- [Webhook installation](#webhook-installation)
    - [Self Signed Certificate](#self-signed-certificate)
    - [Unset Webhook](#unset-webhook)
- [getUpdates installation](#getupdates-installation)
    - [getUpdates without database](#getupdates-without-database)
- [Filter Update](#filter-update)
- [Support](#support)
    - [Types](#types)
    - [Inline Query](#inline-query)
    - [Methods](#methods)
    - [Send Message](#send-message)
    - [Send Photo](#send-photo)
    - [Send Chat Action](#send-chat-action)
    - [getUserProfilePhoto](#getuserprofilephoto)
    - [getFile and downloadFile](#getfile-and-downloadfile)
    - [Send message to all active chats](#send-message-to-all-active-chats)
- [Utils](#utils)
    - [MySQL storage (Recommended)](#mysql-storage-recommended)
        - [External Database connection](#external-database-connection)
    - [Channels Support](#channels-support)
- [Commands](#commands)
    - [Predefined Commands](#predefined-commands)
    - [Custom Commands](#custom-commands)
    - [Commands Configuration](#commands-configuration)
    - [Admin Commands](#admin-commands)
        - [Set Admins](#set-admins)
        - [Channel Administration](#channel-administration)
- [Upload and Download directory path](#upload-and-download-directory-path)
- [Logging](doc/01-utils.md)
- [Documentation](#documentation)
- [Assets](#assets)
- [Example bot](#example-bot)
- [Projects with this library](#projects-with-this-library)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [Security](#security)
- [Donate](#donate)
- [For enterprise](#for-enterprise)
- [License](#license)
- [Credits](#credits)

Introduction
------------

[](#introduction)

This is a pure PHP Telegram Bot, fully extensible via plugins.

Telegram announced official support for a [Bot API](https://telegram.org/blog/bot-revolution), allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one can simply write a bot and have interactions in a matter of minutes.

The Bot can:

- Retrieve updates with [webhook](#webhook-installation) and [getUpdates](#getupdates-installation) methods.
- Supports all types and methods according to Telegram Bot API 6.7 (April 2023).
- Supports supergroups.
- Handle commands in chat with other bots.
- Manage Channel from the bot admin interface.
- Full support for **inline bots**.
- Inline keyboard.
- Messages, InlineQuery and ChosenInlineQuery are stored in the Database.
- Conversation feature.

---

This code is available on [GitHub](https://github.com/php-telegram-bot/core). Pull requests are welcome.

Instructions
------------

[](#instructions)

### Create your first bot

[](#create-your-first-bot)

1. Message [`@BotFather`](https://telegram.me/BotFather) with the following text: `/newbot`

    If you don't know how to message by username, click the search field on your Telegram app and type `@BotFather`, where you should be able to initiate a conversation. Be careful not to send it to the wrong contact, because some users have similar usernames to `BotFather`.

    [![BotFather initial conversation](https://user-images.githubusercontent.com/9423417/60736229-bc2aeb80-9f45-11e9-8d35-5b53145347bc.png)](https://user-images.githubusercontent.com/9423417/60736229-bc2aeb80-9f45-11e9-8d35-5b53145347bc.png)
2. `@BotFather` replies with:

    ```
    Alright, a new bot. How are we going to call it? Please choose a name for your bot.

    ```
3. Type whatever name you want for your bot.
4. `@BotFather` replies with:

    ```
    Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

    ```
5. Type whatever username you want for your bot, minimum 5 characters, and must end with `bot`. For example: `telesample_bot`
6. `@BotFather` replies with:

    ```
    Done! Congratulations on your new bot. You will find it at
    telegram.me/telesample_bot. You can now add a description, about
    section and profile picture for your bot, see /help for a list of
    commands.

    Use this token to access the HTTP API:
    123456789:AAG90e14-0f8-40183D-18491dDE

    For a description of the Bot API, see this page:
    https://core.telegram.org/bots/api

    ```
7. Note down the 'token' mentioned above.

*Optionally set the bot privacy:*

1. Send `/setprivacy` to `@BotFather`.

    [![BotFather later conversation](https://user-images.githubusercontent.com/9423417/60736340-26439080-9f46-11e9-970f-8f33bbe39c5f.png)](https://user-images.githubusercontent.com/9423417/60736340-26439080-9f46-11e9-970f-8f33bbe39c5f.png)
2. `@BotFather` replies with:

    ```
    Choose a bot to change group messages settings.

    ```
3. Type (or select) `@telesample_bot` (change to the username you set at step 5 above, but start it with `@`)
4. `@BotFather` replies with:

    ```
    'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
    'Disable' - your bot will receive all messages that people send to groups.
    Current status is: ENABLED

    ```
5. Type (or select) `Disable` to let your bot receive all messages sent to a group.
6. `@BotFather` replies with:

    ```
    Success! The new status is: DISABLED. /help

    ```

### Require this package with Composer

[](#require-this-package-with-composer)

Install this package through [Composer](https://getcomposer.org/ "Composer"). Edit your project's `composer.json` file to require `longman/telegram-bot`.

Create *composer.json* file

```
{
    "name": "yourproject/yourproject",
    "type": "project",
    "require": {
        "php": ">=7.3",
        "longman/telegram-bot": "*"
    }
}
```

and run `composer update`

**or**

run this command in your command line:

```
composer require longman/telegram-bot
```

### Choose how to retrieve Telegram updates

[](#choose-how-to-retrieve-telegram-updates)

The bot can handle updates with [**Webhook**](#webhook-installation) or [**getUpdates**](#getupdates-installation) method:

WebhookgetUpdatesDescriptionTelegram sends the updates directly to your hostYou have to fetch Telegram updates manuallyHost with httpsRequiredNot requiredMySQLNot required([Not](#getupdates-without-database)) RequiredUsing a custom Bot API server
-----------------------------

[](#using-a-custom-bot-api-server)

**For advanced users only!**

As from Telegram Bot API 5.0, users can [run their own Bot API server](https://core.telegram.org/bots/api#using-a-local-bot-api-server "Using a Local Bot API Server") to handle updates. This means, that the PHP Telegram Bot needs to be configured to serve that custom URI. Additionally, you can define the URI where uploaded files to the bot can be downloaded (note the `{API_KEY}` placeholder).

```
Longman\TelegramBot\Request::setCustomBotApiUri(
    $api_base_uri          = 'https://your-bot-api-server', // Default: https://api.telegram.org
    $api_base_download_uri = '/path/to/files/{API_KEY}'     // Default: /file/bot{API_KEY}
);
```

**Note:** If you are running your bot in `--local` mode, you won't need the `Request::downloadFile()` method, since you can then access your files directly from the absolute path returned by `Request::getFile()`.

Webhook installation
--------------------

[](#webhook-installation)

Note: For a more detailed explanation, head over to the [example-bot repository](https://github.com/php-telegram-bot/example-bot "Example Bot repository") and follow the instructions there.

In order to set a [Webhook](https://core.telegram.org/bots/api#setwebhook "Webhook on Telegram Bot API") you need a server with HTTPS and composer support. (For a [self signed certificate](#self-signed-certificate) you need to add some extra code)

Create *[set.php](https://github.com/php-telegram-bot/example-bot/blob/master/set.php "example set.php")* with the following contents:

```
