I'm using Botman 2.6 directly in a PHP 7.3 app (i.e. not Botman Studio/ Laravel).
The code is meant to start a conversation that asks 2 initial questions. Then ask another question one or more times, depending on the user's response to a prompt "Ask more?". (That's right, ask same question ad infinitum as long as reply is affirmative!)
It isn't working fully as expected so I'm working my way through what could be wrong.
The main PHP script has:
\BotMan\BotMan\Drivers\DriverManager::loadDriver(BotMan\Drivers\Telegram\TelegramDriver::class);
$botman = \BotMan\BotMan\BotManFactory::create($config);
$botman->hears('ask me', function(\BotMan\BotMan\BotMan $bot) {
$repeatingQuestionsParam = new RepeatingQuestionsConversation();
$bot->startConversation(new InitialQuestionsConversation($repeatingQuestionsParam));
});
The first conversation's class is:
class InitialQuestionsConversation extends BotMan\BotMan\Messages\Conversations\Conversation {
protected $FirstAnswer;
protected $SecondAnswer;
protected $repeatingQuestions;
function __construct( $repeatingQuestionsParam ) {
$this->repeatingQuestions = $repeatingQuestionsParam;
}
public function askFirstQuestion() {
$this->ask('What is your name', function(BotMan\BotMan\Messages\Incoming\Answer $answer) {
$this->FirstAnswer = $answer->getText();
$this->askSecondQuestion();
});
}
public function askSecondQuestion() {
$this->ask('How old are you', function(BotMan\BotMan\Messages\Incoming\Answer $answer) {
$this->SecondAnswer = $answer->getText();
$this->bot->startConversation($this->repeatingQuestions);
});
}
public function run() {
$this->askFirstQuestion();
}
}
When the code is run, it asks the first question but not the second. For this reason I am thinking the issue has nothing to do with the 2nd conversation class (though given below.) Is there something wrong with using a __construct function in the conversation class? In all google searched samples, I've never seen this done but it serves my purpose here.
The second conversation class:
class RepeatingQuestionsConversation extends BotMan\BotMan\Messages\Conversations\Conversation {
protected $stopAsking = false;
protected $latestAnswer;
public function askIfToContinue() {
$question = BotMan\BotMan\Messages\Outgoing\Question::create('Ask another?')
->addButtons([
Button::create('Yes')->value('yes'),
Button::create('No')->value('no'),
]);
$this->ask($question, function (BotMan\BotMan\Messages\Incoming\Answer $answer) {
if ($answer->isInteractiveMessageReply()) {
$selectedValue = $answer->getValue();
$selectedText = $answer->getText();
}
if ($selectedText = 'no') {
$this->stopAsking = true;
}
});
return $this->stopAsking;
}
public function askRecurringQuestion() {
$this->ask('Enter '.$question, function(BotMan\BotMan\Messages\Incoming\Answer $answer) {
$this->latestAnswer = $answer->getText();
});
}
public function run() {
while ( !$this-askIfToContinue() ) {
$this->askRecurringQuestion();
}
}
}