PHPackages                             shamanhead/telbot - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. shamanhead/telbot

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

shamanhead/telbot
=================

A simple library to create message bot in telegram

v0.301(4y ago)0293MITPHPPHP &gt;=7.2

Since Aug 5Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ShamanHead/telbot)[ Packagist](https://packagist.org/packages/shamanhead/telbot)[ Docs](https://packagist.org/packages/shamanhead/telbot)[ RSS](/packages/shamanhead-telbot/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

telbot
======

[](#telbot)

**Contents**

- [**Introducion**](#introducion)
- [Creating you bot](#creating_bot)

- [**Utils**](#utils)
    - [Creating keyboard](#utils_keyboard)
    - [Encoding files](#utils_encoding)
    - [Building inline query result](#utils_inline_query_result)
- [**Mysql features**](#mysql_features)
    - [Context](#context)
    - [Chats](#mysql_chats)
- [**Inquiry**](#inquiry)
    - [Supported methods](#inquiry_support)
    - [Sending simple answers](#inquiry_simple)
    - [Sending callback answers](#inquiry_callback)
    - [Sending inline answers](#inquiry_inline)
    - [Sending files](#inquiry_files)
- [**InputHandle**](#input_handle)
    - [Creating a new InputHandle object](#input_handle_create)
    - [Working with data](#input_handle_working)
- [**Privilege**](#privilege)
- [**Examples**](#examples)
- [**License**](#license)

Introducion
-----------

[](#introducion)

If you want to create your bot, for the first you need to register him.You can do it with @BotFather. Open the dialog with him and write /newbot , like as in this image:

[![Снимок](https://user-images.githubusercontent.com/31220669/77857189-c0e6ac80-7204-11ea-9f7b-a6f204143cac.PNG)](https://user-images.githubusercontent.com/31220669/77857189-c0e6ac80-7204-11ea-9f7b-a6f204143cac.PNG)

[![Снимок6](https://user-images.githubusercontent.com/31220669/77857225-eb386a00-7204-11ea-9a2e-7cd4511719e6.PNG)](https://user-images.githubusercontent.com/31220669/77857225-eb386a00-7204-11ea-9a2e-7cd4511719e6.PNG)

After that you need to set webhook on your bot.Webhook its a system, who sending queries to your server, if telegram gets one.

Before start creating a webhook you need bot api token and server.If you dont have a server, you can use [heroku](https://heroku.com) to create one.

You can find your bot api token by writing /mybots, then select your bot, and then select button "API Token".Example:

[![Снимок2](https://user-images.githubusercontent.com/31220669/77857239-fe4b3a00-7204-11ea-87b0-7f45defc1170.PNG)](https://user-images.githubusercontent.com/31220669/77857239-fe4b3a00-7204-11ea-87b0-7f45defc1170.PNG)

[![Снимок 3PNG](https://user-images.githubusercontent.com/31220669/77857243-01dec100-7205-11ea-93fa-50d83bc4670d.PNG)](https://user-images.githubusercontent.com/31220669/77857243-01dec100-7205-11ea-93fa-50d83bc4670d.PNG)

[![Снимок3](https://user-images.githubusercontent.com/31220669/77857248-04411b00-7205-11ea-9e6d-0ae54248e6a3.PNG)](https://user-images.githubusercontent.com/31220669/77857248-04411b00-7205-11ea-9e6d-0ae54248e6a3.PNG)

When you finish all this actions, you can set webhook to your bot.For this you need to use bot api method setWebhook:

[![Снимок8](https://user-images.githubusercontent.com/31220669/77857325-731e7400-7205-11ea-9130-252cbbb32585.PNG)](https://user-images.githubusercontent.com/31220669/77857325-731e7400-7205-11ea-9130-252cbbb32585.PNG)

Then, if you done all right, you will see this answer:

[![Снимок4](https://user-images.githubusercontent.com/31220669/77857285-305c9c00-7205-11ea-9d4e-984c29d116e4.PNG)](https://user-images.githubusercontent.com/31220669/77857285-305c9c00-7205-11ea-9d4e-984c29d116e4.PNG)

After that you can start working with your bot.But how I can work?You can ask me.Lets see, how.

Creating you bot
----------------

[](#creating-you-bot)

How to create bot?Very easy!Just create a new bot class:

```
	use \Telbot\Bot as Bot;

	$bot = new Bot('BOT_API_KEY HERE');
```

So, lets create a script, who will send a text message as test.But how?The Inquiry class will help us with it:

```
	use \Telbot\Bot as Bot;
	use \Telbot\Inquiry as Inquiry;
	use \Telbot\InputHandle as InputHandle;

	$bot = new Bot('API_TOKEN');
	$InputHandle = new InputHandle();

	Inquiry::send($bot
			,'sendMessage',
		[
			'chat_id' => $InputHandle->getChatId(),
			'text' => 'Testing your bot.'
		]
	);
```

[![Снимок](https://user-images.githubusercontent.com/31220669/77940068-ca354f00-72c0-11ea-9756-0b0a3b030594.PNG)](https://user-images.githubusercontent.com/31220669/77940068-ca354f00-72c0-11ea-9756-0b0a3b030594.PNG)

Utils
-----

[](#utils)

Supporting class for ease of work with telegram bot api types.

Creating keyboard
-----------------

[](#creating-keyboard)

You can create keyboards easy using this way:

```
	use \Telbot\Utils\ as Utils;
	use \Telbot\Bot as Bot;

	Utils::buildInlineKeyboard([[['one', 'text']], [['two', 'text']], [['three', 'test']]])

	Utils::buildKeyboard([[['third'], ['second'], ['first']]])
```

Examples:

```
	use \Telbot\Utils as Utils;
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('API_TOKEN');
	$InputHandle = new InputHandle();

	Inquiry::send($bot, 'sendMessage', [
		'chat_id' => $InputHandle->getChatId(),
		'text' => 'Simple text.',
		'reply_markup' => Utils::buildInlineKeyboard([[['one', 'callback']], [['two', 'callback']], [['three', 'callback']]])
	]);
```

[![Снимок3](https://user-images.githubusercontent.com/31220669/77940081-d02b3000-72c0-11ea-9300-3d035a16625b.PNG)](https://user-images.githubusercontent.com/31220669/77940081-d02b3000-72c0-11ea-9300-3d035a16625b.PNG)

Encoding files
--------------

[](#encoding-files)

If you want to send video or photo to user, you need to encode them to CURl format.For this use this method:

```
	Utils::encodeFile($filePath) //return encoded CURlfile object.
```

Parameter $filePath need to indicate path to file you want to send.

Building inline query result
----------------------------

[](#building-inline-query-result)

If you want to send an answer to inline query, you need to build answer object.For this use this method:

```
	Utils::buildInlineQueryResult($resultType ,$data) //returns json encoded array of $data with type of result $resultType
```

You can check example [here](#inquiry_inline)

Mysql features
--------------

[](#mysql-features)

To start work with mysql, first you need to do is enable sql connection in your bot object:

```
	$bot->enableSql();
```

You can also disable sql by using similar method:

```
	$bot->disableSql();
```

Warning: if your sql connection doesnt exist, you can not use this modules:User, Chat. In this case Context instead of writing context values ​​to the database would be create new file with context(one to user).

Later you need to specify sql credentials:

```
	$bot->sqlCredentials(
		[
			'database_server' => '',
			'database_name' => '',
			'username' => '',
			'password' => ''
		]
		);
```

Or you can indicate your external pdo connection as sql credentials:

```
	$bot->externalPDO($PDO_CONNECTION);
```

After all this actions, you can start to work with database.

Context
-------

[](#context)

Using class Context you can create context dependence:

```
	use \Telbot\Context as Context; //We include new class Context
	use \Telbot\Bot as Bot;
	use \Telbot\Inquiry as Inquiry;
	use \Telbot\InputHandle as InputHandle;

	$InputHandle = new InputHandle();
	$bot = new Bot('API_TOKEN');
	$DBH = new PDO();
	$bot->externalPDO($DBH);
	$bot->enableSql();

	if(!Context::read($bot, $InputHandle->getChatId(), $InputHandle->getUserId())){ //reading context
		Inquiry::send($bot
				,'sendMessage',
			[
				'chat_id' => $InputHandle->getChatId(),
				'text' => 'Write smth'
			]
		);
		Context::write($bot, $InputHandle->getChatId(), $InputHandle->getUserId(), 'smth'); //creating new context
	}else{
		Inquiry::send($bot
			,'sendMessage',
			[
			'chat_id' => $InputHandle->getChatId(),
			'text' => 'Okay, you writed!'
			]
		);
		Context::delete($bot, $InputHandle->getChatId(), $InputHandle->getUserId()); //delete context
	}
```

[![Снимок2](https://user-images.githubusercontent.com/31220669/77940072-cd303f80-72c0-11ea-837e-903d9c83020e.PNG)](https://user-images.githubusercontent.com/31220669/77940072-cd303f80-72c0-11ea-837e-903d9c83020e.PNG)

Working with chats in database
------------------------------

[](#working-with-chats-in-database)

All the same, but a bit different:

To add chats to database, you need to use this function:

```
	Chat::add($bot, $chatId);
```

To delete chats:

```
	Chat::delete($bot, $chatId);
```

To get chats:

```
	Chat::get($bot, $chatId);
```

This function returns array with row information of this chat(row id, chat id, bot token)

To get all chats:

```
	Chat::getAll($bot);
```

Inquiry
-------

[](#inquiry)

Its the main class in this library.This paragraph shows all capabilities of this class.

This class has only one method - Inquiry::send().With this class you can send both simple text messages and complex answers.

```
	Inquiry::send($bot, $method, $data);
```

Supported methods
-----------------

[](#supported-methods)

All method supported during version of API 4.7

Sending simple answer
---------------------

[](#sending-simple-answer)

```
	Inquiry::send($bot, 'sendMessage', [
		'chat_id' => $InputHandle->getChatId(),
		'text' => 'This is a testing message'

	]);
```

Sending callback query answer
-----------------------------

[](#sending-callback-query-answer)

```
	Inquiry::send($bot, 'answerCallbackQuery', [
		'callback_query_id' => $InputHandle->getCallbackQueryId(),
		'text' => 'This is a callback answer, who lool like common notification'
	]);
```

Sending Inline query answer
---------------------------

[](#sending-inline-query-answer)

```
	Inquiry::answerInlineQuery($bot, [
		'inline_query_id' => $InputHandle->getInlineQueryId(),
		'results' => Utils::buildInlineQueryResult('article', [
		'title' => 'test',
		'input_message_content' => [
			'message_text' => 'Yes, its just test'
		]])
	]);
```

You can send any of telegram methods with this method send.All of this supported.

Sending files
-------------

[](#sending-files)

```
	//sending photo
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A');
	$InputHandle = new InputHandle();

	Inquiry::send($bot ,'sendPhoto', [
		'chat_id' => $InputHandle->getChatId(),
		'photo' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Telegram_2019_Logo.svg/1200px-Telegram_2019_Logo.svg.png',
		'caption' => 'This is an image!So beautiful'
	]);
```

To send files from your server, you need to encode file to CURl format.For this, you need to use method [Utils::encodeFile](#utils_encoding).

```
	use \Telbot\Utils as Utils;
	use \Telbot\Bot as Bot;
	use \Telbot\InputHandle as InputHandle;
	use \Telbot\Inquiry as Inquiry;

	$bot = new Bot('927942575:AAHZpZoG2pBRw25Lw-pPaw8FU15t00Lsf3A');
	$InputHandle = new InputHandle();

	Inquiry::send($bot ,'sendPhoto', [
		'chat_id' => $InputHandle->getChatId(),
		'photo' => Utils::encodeFile('app/something/telegram.png'),
		'caption' => 'This is an image!So beautiful'
	]);
```

Input Handle
------------

[](#input-handle)

This class needs for comfortable work with telegram answer query.

Creating a new InputHandle object
---------------------------------

[](#creating-a-new-inputhandle-object)

```
	$InputHanle = new InputHandle();
```

Working with data
-----------------

[](#working-with-data)

```
	$InputHandle->getUpdateId() // returns an update id of telegram answer query.

	$InputHandle->getQueryType() // returns a query type of telegram answer query(callback_query,inline_query,message).

	$InputHandle->getInstance() // returns an array of telegram answer.

	$InputHandle->getCallbackData() //  returns a callback data from telegram answer query.

	$InputHandle->getCallBackQueryId() // returns a callback query id from telegram answer query.

	$InputHandle->getUserId() // returns user id.

	$InputHandle->userIsBot() // return true if user who send quiry is bot.

	$InputHandle->getUserName() // returns name of user, who sends query.

	$InputHandle->getMessageId() // returns user's message id.

	$InputHandle->getUserFirstName() // returns user's first name.

	$InputHandle->getLanguageCode() // returns user's language code.

	$InputHandle->getChatType() // returns chat type.

	$InputHandle->getChat() // returns chat array from telegram answer query.

	$InputHandle->newChatMember() // returns true when new member comes to telegram chat.

	$InputHandle->getChatId() // returns a chat id, where the message come.

	$InputHandle->getDate() // returns date when telegram answer query was send.

	$InputHandle->getEntities() // returns message entities from telegram answer query.

	$InputHandle->getChatTitle() // returns title of chat where bot gets query.

	$InputHandle->getInlineQueryText() // returns query data from inline query.

	$InputHandle->getInlineQueryOffset() // returns query offset from inline query.

	$InputHandle->getInlineQueryId() // returns an inline query in from telegram answer query.
```

Privelege
---------

[](#privelege)

You can give a user a specific privilege in the chat(or all chats). This can be used to give access to certain commands to that particular user.

```
	Privilege::setToChat($bot, $value, $userId, $chatId); //for one chat
```

```
	Privilege::setToAllChats($bot, $value, $userId); //for all chats
```

```
	Privilege::get($bot, $userId); //getting a privelege
```

Examples
--------

[](#examples)

See examples at 'examples' folder.

License
-------

[](#license)

Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1745d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/31220669?v=4)[Arseniy](/maintainers/ShamanHead)[@ShamanHead](https://github.com/ShamanHead)

---

Top Contributors

[![ShamanHead](https://avatars.githubusercontent.com/u/31220669?v=4)](https://github.com/ShamanHead "ShamanHead (69 commits)")

---

Tags

botbot-apiphptelegramwebhookphppackagebottelegramTelegramAPI

### Embed Badge

![Health badge](/badges/shamanhead-telbot/health.svg)

```
[![Health](https://phpackages.com/badges/shamanhead-telbot/health.svg)](https://phpackages.com/packages/shamanhead-telbot)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
