![]() |
PhpBotFramework
2.0.2
A framework for Telegram Bots' APIs.
|
Create a state-based bot using these methods. More...
Functions | |
| PhpBotFramework\Utilities\BotState::__construct (BasicBot &$bot) | |
| int | PhpBotFramework\Utilities\BotState::getStatus (int $default_status=-1) |
| Get current user status from Redis and set it in status variable. More... | |
| PhpBotFramework\Utilities\BotState::setStatus (int $status) | |
| Set the status of the bot in both Redis and $status. More... | |
Create a state-based bot using these methods.
The bot will answer in different ways based on its internal state.
Below an example where we save user's credentials using bot states:
<?php
// Include the framework
require './vendor/autoload.php';
// Define bot state
define("SEND_USERNAME", 1);
define("SEND_PASSWORD", 2);
// Create the class for the bot that will handle login
class LoginBot extends PhpBotFramework\Bot {
// Add the function for processing messages
protected function processMessage($message) {
switch($this->getStatus()) {
case SEND_USERNAME:
$this->sendMessage("Please, send your password.");
// Update the bot state
$this->setStatus(SEND_PASSWORD);
break;
// Or if we are expecting a password from the user
case SEND_PASSWORD:
$this->sendMessage("The registration is complete");
break;
}
}
}
$bot = new LoginBot("token");
$bot->redis = new Redis();
$bot->redis->connect('127.0.0.1');
// Create the answer to the <code>/start</code> command
$start_closure = function($bot, $message) {
$bot->sendMessage("Please, send your username.");
$bot->setStatus(SEND_USERNAME);
};
$bot->addMessageCommand("start", $start_closure);
$bot->getUpdatesLocal();| int PhpBotFramework\Utilities\BotState::getStatus | ( | int | $default_status = -1 | ) |
Get current user status from Redis and set it in status variable.
Throws an exception if the Redis connection is missing.
| int | $default_status | Optional. The default status to return if there is no status for the current user. |
| PhpBotFramework\Utilities\BotState::setStatus | ( | int | $status | ) |
Set the status of the bot in both Redis and $status.
Throws an exception if the Redis connection is missing.
| int | $status | The new status of the bot. |
1.8.13