Comments (2)
Released Nutgram https://github.com/nutgram/nutgram/releases/tag/4.20.0
You will need to use the $this->getChatId()
method if you want to use manual methods (sendMessage, etc...) in the first step of the conversation.
To make a conversation work server-side, see these examples:
Conversation
class BaseConversation extends Conversation
{
public function start(Nutgram $bot): void
{
$bot->sendMessage(
text: 'What is your name?',
chat_id: $this->getChatId(), // <--
);
$this->next('getName');
}
public function getName(Nutgram $bot): void
{
$name = $bot->message()?->text ?? 'unknown';
$bot->sendMessage("Hello, $name!");
$this->end();
}
}
InlineMenu
class MenuConversation extends InlineMenu
{
public function start(Nutgram $bot): void
{
$bot->sendMessage(
text: 'Starting...',
chat_id: $this->getChatId(), // <--
);
$this->menuText('Message')
->addButtonRow(InlineKeyboardButton::make('Say hello', callback_data: "@hello"))
->addButtonRow(InlineKeyboardButton::make('Close', callback_data: "@end"))
->showMenu();
}
public function hello(Nutgram $bot): void
{
$bot->sendMessage('Hello');
$this->end();
}
}
from nutgram.
Hi, thanks for the feedback.
To make a conversation work server-side, see these examples:
Conversation
class BaseConversation extends Conversation
{
public function start(Nutgram $bot): void
{
//when a server-side conversation is started,
//the chatId parameter is not automatically filled by Nutgram
//because we are not in an Update context.
$bot->sendMessage(
text: 'What is your name?',
chat_id: $this->getChatId(), //so we need to set the chatId manually (only for this first step)
);
$this->next('getName');
}
public function getName(Nutgram $bot): void
{
$name = $bot->message()?->text ?? 'unknown';
$bot->sendMessage("Hello, $name!");
$this->end();
}
}
InlineMenu
class MenuConversation extends InlineMenu
{
protected function doOpen(string $text, InlineKeyboardMarkup $buttons, array $opt): Message|null
{
//we need to override this method to set the chatId manually
return $this->bot->sendMessage(...[
'reply_markup' => $buttons,
'text' => $text,
'chat_id' => $this->chatId, //required for the first step
...$opt,
]);
}
public function start(Nutgram $bot): void
{
//when a server-side conversation is started,
//the chatId parameter is not automatically filled by Nutgram
//because we are not in an Update context.
$this->chatId = $this->getChatId(); //so we need to set the chatId manually (only for this first step)
$this->menuText('Message')
->addButtonRow(InlineKeyboardButton::make('Say hello', callback_data: "@hello"))
->addButtonRow(InlineKeyboardButton::make('Close', callback_data: "@end"))
->showMenu();
}
public function hello(Nutgram $bot): void
{
$bot->sendMessage('Hello');
$this->end();
}
public function closing(Nutgram $bot): void
{
$bot->sendMessage('Bye!');
parent::closing($bot);
}
}
Small note
I know it is a bit confusing to make conversations work server-side. We will try to improve it.
from nutgram.
Related Issues (20)
- [BUG] Incompatible Hydrator Type in Configuration and Nutgram Classes HOT 1
- [BUG] Conversations not restoring state when using auto-wire container HOT 1
- [BUG] starting inline conversation from server
- [BUG] starting an inline conversation from custom step HOT 1
- [BUG] "Serialization of 'Closure' is not allowed" when using Dependency Injection HOT 2
- [BUG] Bot username regex HOT 1
- Better DI injection services for Commands and Conversations HOT 1
- Is it possible to set default params value in sendMessage (and other methods) when bot init? [FEATURE] HOT 2
- [FEATURE] Passing parameters to the next step of conversation without setUserData and serialised in webhook mode HOT 2
- [BUG] starting inline conversation from server HOT 2
- [BUG] Conversation does not store it's chat_id HOT 1
- [BUG] Can not sendMessage to Group by chat_id HOT 3
- [BUG] Inline menu HOT 2
- [FEATURE] sendMediaGroup
- [BUG] Can't get EntityManager in Command class HOT 4
- sendMessage on replayed message in general topic always set message_thread_id HOT 3
- [BUG] $clientOpt argument in sendAttachment used only for requestMultipart, not for requestJson HOT 1
- nutgram:listen command HOT 1
- [FEATURE] Read message history HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nutgram.