Skip to content

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

License

Notifications You must be signed in to change notification settings

tahrz/simple-telegram-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple PHP telegram bot library

simple-telegram-bot

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

define('BOT_TOKEN', '<YOUR_TOKEN>');
define('BASIC_API_URL', 'https://api.telegram.org/bot' . BOT_TOKEN . '/');
define('WEBHOOK_URL', '<WEBHOOK_HTTPS_URL>');

How to use

To create an API request, use:

// create instance of ConnectionService
$connectionService = new ConnectionService();

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

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

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

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

// 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

// 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

// create instance of MessageHelper
$messageHelper = new MessageHelper($connectionService);

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

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

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages