Skip to content

Commit

Permalink
Merge pull request #706 from nutgram/fix-server-side-inline-menu
Browse files Browse the repository at this point in the history
Better support for server-side inline menu
  • Loading branch information
Lukasss93 committed May 8, 2024
2 parents 0f4b743 + 7388d61 commit 4cc57f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Conversations/InlineMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ protected function showMenu(
return $message;
}

protected function beforeStep(Nutgram $bot)
{
parent::beforeStep($bot);

if ($this->chatId === null) {
$this->chatId = $this->getChatId();
}
}

/**
* @param string|null $finalText
* @param array $opt
Expand Down Expand Up @@ -262,6 +271,7 @@ protected function doOpen(string $text, InlineKeyboardMarkup $buttons, array $op
return $this->bot->sendMessage(...[
'reply_markup' => $buttons,
'text' => $text,
'chat_id' => $this->chatId,
...$opt,
]);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SergiX44\Nutgram\Tests\Fixtures\Conversations\ConversationWithSkipMiddlewareMultipleSteps;
use SergiX44\Nutgram\Tests\Fixtures\Conversations\NonSerializableConversation;
use SergiX44\Nutgram\Tests\Fixtures\Conversations\OneStepNotCompletedConversation;
use SergiX44\Nutgram\Tests\Fixtures\Conversations\ServerConversation;
use SergiX44\Nutgram\Tests\Fixtures\Conversations\SurveyConversation;
use SergiX44\Nutgram\Tests\Fixtures\Conversations\TwoStepConversation;
use SergiX44\Nutgram\Tests\Fixtures\CustomService;
Expand Down Expand Up @@ -278,3 +279,18 @@ function secondStep(Nutgram $bot)

expect($bot->get('test'))->toBe(66);
});

it('starts a conversation from server', function () {
$bot = Nutgram::fake();

ServerConversation::begin(
bot: $bot,
userId: 123456789,
chatId: 123456789
);

$bot->assertReply('sendMessage', [
'text' => 'First step',
'chat_id' => 123456789
]);
});
15 changes: 15 additions & 0 deletions tests/Feature/InlineMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,18 @@
'text' => 'Alert!',
], 1);
});

it('starts an inline menu from server', function () {
$bot = Nutgram::fake();

ValidWithFallbackMenu::begin(
bot: $bot,
userId: 123456789,
chatId: 123456789
);

$bot->assertReply('sendMessage', [
'text' => 'Choose a color:',
'chat_id' => 123456789
]);
});

0 comments on commit 4cc57f8

Please sign in to comment.