这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/DialogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KootLabs\TelegramBotDialogs;

use KootLabs\TelegramBotDialogs\Objects\BotInitiatedUpdate;
use KootLabs\TelegramBotDialogs\Storages\Store;
use Psr\SimpleCache\CacheInterface;
use Telegram\Bot\Api;
Expand Down Expand Up @@ -39,16 +40,35 @@ public function activate(Dialog $dialog): void
$this->storeDialogState($dialog);
}

/**
* Initiate a new Dialog from server side (e.g. by cron).
* Note, a User firstly should start a chat with a bot (bot can't initiate a chat — this is TG Bot API limitation).
* @api
* @experimental
* @throws \Telegram\Bot\Exceptions\TelegramSDKException
*/
public function startNewDialogInitiatedByBot(Dialog $dialog): void
{
$this->activate($dialog);

$this->proceed(new BotInitiatedUpdate($dialog));

$dialog->isEnd()
? $this->forgetDialogState($dialog)
: $this->storeDialogState($dialog);
}

/**
* @api
* Run next step of the active Dialog.
* This is a thin wrapper for {@see \KootLabs\TelegramBotDialogs\Dialog::proceed}
* to store and restore Dialog state between request-response calls.
* @throws \Telegram\Bot\Exceptions\TelegramSDKException
*/
public function proceed(Update $update): void
{
$dialog = $this->getDialogInstance($update);
if ($dialog === null) {
if (! $dialog instanceof Dialog) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Laravel/Facades/Dialogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

/**
* @api
* @mixin \KootLabs\TelegramBotDialogs\DialogManager
* @method static \KootLabs\TelegramBotDialogs\DialogManager setBot(\Telegram\Bot\Api $bot)
* @method static void activate(\KootLabs\TelegramBotDialogs\Dialog $dialog)
* @method static void proceed(\Telegram\Bot\Objects\Update $update)
*/
final class Dialogs extends Facade
{
Expand Down
21 changes: 21 additions & 0 deletions src/Objects/BotInitiatedUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace KootLabs\TelegramBotDialogs\Objects;

use KootLabs\TelegramBotDialogs\Dialog;
use Telegram\Bot\Objects\Update;

/** @api */
final class BotInitiatedUpdate extends Update
{
public Dialog $dialog;

public function __construct(Dialog $dialog, array $data = [])
{
$this->dialog = $dialog;

parent::__construct($data);
}
}