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>');
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 underConnectionService
, withgetWebhook
,setWebhook
API methods. Helpers use ONLYCurlConnectionService()
// 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>')