-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d89e80
commit 36c81f8
Showing
13 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Mateodioev\TgHandler\Commands\MessageCommand; | ||
use Mateodioev\TgHandler\Conversations\{MessageConversation, customTTl}; | ||
|
||
class TriggerConversationCommand extends MessageCommand | ||
{ | ||
protected string $name = 'temp'; | ||
protected string $description = 'Trigger a temporary conversation'; | ||
|
||
public function execute(array $args = []) | ||
{ | ||
$this->api()->sendMessage( | ||
$this->ctx()->message()->chat()->id(), | ||
'Send me a message. You only have 1 hour to do it.' | ||
); | ||
|
||
$actualTime = time(); | ||
|
||
// Expire the conversation after 1 hour | ||
return TemporaryConversation::fromContext($this->ctx()) | ||
->withCustomTTL(60 * 60) | ||
->setCreatedAt($actualTime); | ||
} | ||
|
||
} | ||
|
||
class TemporaryConversation extends MessageConversation | ||
{ | ||
use customTTl; | ||
|
||
private int $createdAt; | ||
|
||
public function setCreatedAt(int $time): static | ||
{ | ||
$this->createdAt = $time; | ||
return $this; | ||
} | ||
|
||
public function execute(array $args = []) | ||
{ | ||
$this->api()->sendMessage( | ||
$this->ctx()->message()->chat()->id(), | ||
'You sent: ' . $this->ctx()->message()->text() . "\n\nSend me another message (expire in " . date('Y-m-d H:i:s', $this->createdAt) . ').' | ||
); | ||
|
||
/// With this, the conversation will expire after 1 hour from the creation time | ||
|
||
$diff = time() - $this->createdAt; | ||
|
||
return TemporaryConversation::fromContext($this->ctx()) | ||
->withCustomTTL(60 * 60 - $diff) | ||
->setCreatedAt($this->createdAt); | ||
} | ||
|
||
public function onExpired(): void | ||
{ | ||
$this->api()->sendMessage($this->chatId, 'The conversation has expired.'); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mateodioev\TgHandler\Conversations; | ||
|
||
trait customTTl | ||
{ | ||
protected ?int $customTtl = null; | ||
|
||
public function withCustomTTL(int $ttl): static | ||
{ | ||
$this->customTtl = $ttl; | ||
return $this; | ||
} | ||
|
||
public function ttl(): ?int | ||
{ | ||
return $this->customTtl; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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