Last active
March 21, 2017 20:26
-
-
Save exileed/919363684196d2f66ba2d64b0e74c10e to your computer and use it in GitHub Desktop.
Callback system for https://github.com/exileed/telegram-bot-sdk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Commands\Telegram\Bot; | |
use Telegram\Bot\Keyboard\Keyboard; | |
class PrivacyCommand extends AbstractCommand | |
{ | |
/** | |
* @var string Command Name | |
*/ | |
protected $name = "privacy"; | |
/** | |
* @var string Command Description | |
*/ | |
protected $description = "Включат и отключает сбор статистики у пользователя."; | |
/** | |
* @inheritdoc | |
*/ | |
public function handle($arguments) | |
{ | |
$inlineLayout = [ | |
[ | |
Keyboard::inlineButton( | |
[ | |
'text' => '🙈 Запретить', | |
'callback_data' => 'private off', | |
]), | |
Keyboard::inlineButton( | |
[ | |
'text' => '🙉 Разрешить', | |
'callback_data' => 'private on', | |
]), | |
] | |
]; | |
$keyboard = Keyboard::make( | |
[ | |
'inline_keyboard' => $inlineLayout, | |
] | |
); | |
$text ='Вы можете запретить отображения вас на страницах статистики.'.PHP_EOL; | |
$text .='И тогда никто не увидит, сколько вы пишете и где вы сидите.'; | |
$this->replyWithMessage( | |
[ | |
'text' => $text, | |
'parse_mode' => 'Markdown', | |
'reply_markup' => $keyboard | |
] | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Commands\Telegram\Callback; | |
/** | |
* Пример команды | |
* Команда парсится с "data": "private on" | |
* | |
* Class PrivateCallback | |
* @package App\Commands\Telegram\Callback | |
*/ | |
class PrivateCallback extends \Telegram\Bot\Callbacks\CallbackCommand | |
{ | |
/** | |
* @var string CallbackQuery command | |
*/ | |
protected $name = "private"; | |
/** | |
* @inheritdoc | |
*/ | |
public function handle($arguments) | |
{ | |
$arguments; // ['on'] | |
if($arguments[0]==='on'){ | |
$this->replyWithMessage([ | |
'text' => 'Приватность включена', | |
]); | |
}else { | |
$this->replyWithMessage([ | |
'text' => 'Приватность отключена', | |
]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
/* | |
|-------------------------------------------------------------------------- | |
| Default Bot Name | |
|-------------------------------------------------------------------------- | |
| | |
| Here you may specify which of the bots below you wish to use as | |
| your default bot for regular use. Of course, you may use many | |
| bots at once using the manager class. | |
| | |
*/ | |
'default' => 'common', | |
/* | |
|-------------------------------------------------------------------------- | |
| Telegram Bots | |
|-------------------------------------------------------------------------- | |
| | |
| Here are each of the telegram bots config. | |
| | |
| Supported Params: | |
| - username: Your Telegram Bot's Username. | |
| Example: (string) 'BotFather'. | |
| | |
| - token: Your Telegram Bot's Access Token. | |
Refer for more details: https://core.telegram.org/bots#botfather | |
| Example: (string) '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'. | |
| | |
| - commands: (Optional) Commands to register for this bot, | |
| Supported Values: "Command Group Name", "Shared Command Name", "Full Path to Class". | |
| Default: Registers Global Commands. | |
| Example: (array) [ | |
| 'admin', // Command Group Name. | |
| 'status', // Shared Command Name. | |
| Acme\Project\Commands\BotFather\HelloCommand::class, | |
| Acme\Project\Commands\BotFather\ByeCommand::class, | |
| ] | |
| - callbacks: (Optional) Callbacks to register for this bot, | |
| Supported Values: "Callbacks Group Name", "Shared Callbacks Name", "Full Path to Class". | |
| Default: Registers Global Callbacks. | |
| Example: (array) [ | |
| Acme\Project\Commands\BotFather\HelloCallbackCommand::class, | |
| Acme\Project\Commands\BotFather\ByeCallbackCommand::class, | |
| ] | |
*/ | |
'bots' => [ | |
'common' => [ | |
'username' => 'MyTelegramBot', | |
'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR-BOT-TOKEN'), | |
'commands' => [ | |
// Acme\Project\Commands\MyTelegramBot\BotCommand::class | |
], | |
'callbacks' => [ | |
// Acme\Project\Commands\MyTelegramBot\CallbackCommand::class | |
], | |
], | |
// 'second' => [ | |
// 'username' => 'MySecondBot', | |
// 'token' => '123456:abc', | |
// ], | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Asynchronous Requests [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| When set to True, All the requests would be made non-blocking (Async). | |
| | |
| Default: false | |
| Possible Values: (Boolean) "true" OR "false" | |
| | |
*/ | |
'async_requests' => env('TELEGRAM_ASYNC_REQUESTS', false), | |
/* | |
|-------------------------------------------------------------------------- | |
| HTTP Client Handler [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| If you'd like to use a custom HTTP Client Handler. | |
| Should be an instance of \Telegram\Bot\HttpClients\HttpClientInterface | |
| | |
| Default: GuzzlePHP | |
| | |
*/ | |
'http_client_handler' => null, | |
/* | |
|-------------------------------------------------------------------------- | |
| Resolve Injected Dependencies in commands [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| Using Laravel's IoC container, we can easily type hint dependencies in | |
| our command's constructor and have them automatically resolved for us. | |
| | |
| Default: true | |
| Possible Values: (Boolean) "true" OR "false" | |
| | |
*/ | |
'resolve_command_dependencies' => true, | |
/* | |
|-------------------------------------------------------------------------- | |
| Register Telegram Global Commands [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| If you'd like to use the SDK's built in command handler system, | |
| You can register all the global commands here. | |
| | |
| Global commands will apply to all the bots in system and are always active. | |
| | |
| The command class should extend the \Telegram\Bot\Commands\Command class. | |
| | |
| Default: The SDK registers, a help command which when a user sends /help | |
| will respond with a list of available commands and description. | |
| | |
*/ | |
'commands' => [ | |
Telegram\Bot\Commands\HelpCommand::class, | |
], | |
'callbacks' => [ | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Command Groups [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| You can organize a set of commands into groups which can later, | |
| be re-used across all your bots. | |
| | |
| You can create 4 types of groups: | |
| 1. Group using full path to command classes. | |
| 2. Group using shared commands: Provide the key name of the shared command | |
| and the system will automatically resolve to the appropriate command. | |
| 3. Group using other groups of commands: You can create a group which uses other | |
| groups of commands to bundle them into one group. | |
| 4. You can create a group with a combination of 1, 2 and 3 all together in one group. | |
| | |
| Examples shown below are by the group type for you to understand each of them. | |
*/ | |
'command_groups' => [ | |
/* // Group Type: 1 | |
'commmon' => [ | |
Acme\Project\Commands\TodoCommand::class, | |
Acme\Project\Commands\TaskCommand::class, | |
], | |
*/ | |
/* // Group Type: 2 | |
'subscription' => [ | |
'start', // Shared Command Name. | |
'stop', // Shared Command Name. | |
], | |
*/ | |
/* // Group Type: 3 | |
'auth' => [ | |
Acme\Project\Commands\LoginCommand::class, | |
Acme\Project\Commands\SomeCommand::class, | |
], | |
'stats' => [ | |
Acme\Project\Commands\UserStatsCommand::class, | |
Acme\Project\Commands\SubscriberStatsCommand::class, | |
Acme\Project\Commands\ReportsCommand::class, | |
], | |
'admin' => [ | |
'auth', // Command Group Name. | |
'stats' // Command Group Name. | |
], | |
*/ | |
/* // Group Type: 4 | |
'myBot' => [ | |
'admin', // Command Group Name. | |
'subscription', // Command Group Name. | |
'status', // Shared Command Name. | |
'Acme\Project\Commands\BotCommand' // Full Path to Command Class. | |
], | |
*/ | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Shared Commands [Optional] | |
|-------------------------------------------------------------------------- | |
| | |
| Shared commands let you register commands that can be shared between, | |
| one or more bots across the project. | |
| | |
| This will help you prevent from having to register same set of commands, | |
| for each bot over and over again and make it easier to maintain them. | |
| | |
| Shared commands are not active by default, You need to use the key name to register them, | |
| individually in a group of commands or in bot commands. | |
| Think of this as a central storage, to register, reuse and maintain them across all bots. | |
| | |
*/ | |
'shared_commands' => [ | |
// 'start' => Acme\Project\Commands\StartCommand::class, | |
// 'stop' => Acme\Project\Commands\StopCommand::class, | |
// 'status' => Acme\Project\Commands\StatusCommand::class, | |
], | |
'shared_callbacks' => [ | |
// 'start' => Acme\Project\Commands\StartCommand::class, | |
// 'stop' => Acme\Project\Commands\StopCommand::class, | |
// 'status' => Acme\Project\Commands\StatusCommand::class, | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment