Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tahrz/simple-telegram-bot

Simple library, for create telegram bot. Only connector, interfaces and helpers nothing more, clean and simple.
https://github.com/tahrz/simple-telegram-bot

curl library php8 simple-telegram-bot telegram-bot webhook

Last synced: about 2 months ago
JSON representation

Simple library, for create telegram bot. Only connector, interfaces and helpers nothing more, clean and simple.

Awesome Lists containing this project

README

        

# Simple PHP telegram bot library

![simple-telegram-bot](https://img.shields.io/packagist/dt/tahrz/simple-telegram-bot.svg?style=for-the-badge)

This library will help you, to create php telegram bot of any complexity.
> **v 2.0.0**

**Add library to composer:**
```
composer require tahrz/simple-telegram-bot
```

**Add global variables, to config component**
```php
define('BOT_TOKEN', '');
define('BASIC_API_URL', 'https://api.telegram.org/bot' . BOT_TOKEN . '/');
define('WEBHOOK_URL', '');
```

#### How to use

To create an API request, use:
```php
// create instance of ConnectionService
$connectionService = new ConnectionService();

// make connection without answer
$connectionService->init('');

// make connection with array answer type
$arrayResult = $connectionService->withArrayResponse('');

// make connection with object anaswer type
$objectResult = $connectionService->withObjectResponse('');
```

> Also, you can use `webhookConfigurationHelper`, witch build under `ConnectionService`,
> with `getWebhook`, `setWebhook` API methods. Helpers use **ONLY** `CurlConnectionService()`

```php
// create instance of WebhookConfigurationHelper
$webhookConfigurationHelper = new WebhookConfigurationHelper($connectionService);

// get webhook info
$setWebhook = $webhookConfigurationHelper->getWebhookInfo();

// set new webhook
$getWebhook = $webhookConfigurationHelper->setWebhook();

// remove webhook
$removeWebhook = $webhookConfigurationHelper->removeWebhook();
```
`setWebhook()` will set a webhook on your `WEBHOOK_URL` url.

> For getting Updates with or without webhook, you can use
```php
// create instance of WebhookGetUpdateHelper
$webhookUpdateHelper = new WebhookGetUpdateHelper();

// create instance of GetUpdateHelper
$getUpdateHelper = new GetUpdateHelper($connectionService);

// work only if you add webhook
$updatesWithWebhookAsArray = $webhookUpdateHelper->asArray();
$updatesWithWebhookAsObject = $webhookUpdateHelper->asObject();

// work only without webhook
$updatesWithoutWebhook = $getUpdateHelper->asArray();
$updatesWithoutWebhook = $getUpdateHelper->asObject();
```
You always can choose, what type of data, you need to return, `array` or `object` or even **do not return you any data**.

> For sending messages, you can use a `MessageHelper`
```php
// create instance of MessageHelper
$messageHelper = new MessageHelper($connectionService);

// send message without response
$messageHelper->sendWithoutResponse(, '')

// send message with array response type
$messageSendWithArrayResponse = $messageHelper->sendWithArrayResponse(, '')

// send message with object response type
$messageSendWithObjectResponse = $messageHelper->sendWithObjectResponse(, '')