tahrz/simple-telegram-bot

包含连接服务和简单辅助功能的包

2.0.0 2023-10-29 21:27 UTC

This package is auto-updated.

Last update: 2024-09-29 23:26:08 UTC


README

simple-telegram-bot

这个库将帮助您创建任何复杂度的php Telegram机器人。

v 2.0.0

将库添加到composer

composer require tahrz/simple-telegram-bot

向配置组件添加全局变量

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

如何使用

要创建API请求,使用

// 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>'); 

此外,您还可以使用 webhookConfigurationHelper,它是在 ConnectionService 下构建的,具有 getWebhooksetWebhook API方法。辅助器仅使用 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() 将在您的 WEBHOOK_URL url上设置webhook。

对于通过webhook或不通过webhook获取更新,您可以使用

// 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();

您始终可以选择需要返回哪种类型的数据,arrayobject或甚至 不返回任何数据

对于发送消息,您可以使用 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>')