Skip to content

Instantly share code, notes, and snippets.

@LeonAlvarez
Last active March 27, 2017 08:30
Show Gist options
  • Save LeonAlvarez/982a01f1bf4349f60e18afbbe796cd8d to your computer and use it in GitHub Desktop.
Save LeonAlvarez/982a01f1bf4349f60e18afbbe796cd8d to your computer and use it in GitHub Desktop.
<?php
namespace App\Bot\Conversations;
use Mpociot\BotMan\Button;
use Mpociot\BotMan\Conversation;
use Mpociot\BotMan\Question;
class SimpleEditConversation extends Conversation
{
protected $cont;
/**
* Start the conversation
*/
public function run()
{
$this->cont=0;
$this->askAccion();
}
public function askAccion()
{
$question = Question::create('cont = '.$this->cont)
->fallback('Unable to create a new database')
->callbackId('create_database')
->addButtons([
Button::create('minus 1')->value('minus'),
Button::create( $this->cont)->value($this->cont),
Button::create('sum 1')->value('sum'),
]);
$this->ask($question, function () {
$this->simpleCallback();
});
}
public function keyboard()
{
$keyboard = [
'inline_keyboard' => array(
array( ['text' => 'minus 1', 'callback_data' => 'minus']),
array( ['text' => "$this->cont", 'callback_data' => "$this->cont"]),
array( ['text' => 'sum 1', 'callback_data' => 'sum']),
),
];
return json_encode($keyboard);
}
private function simpleCallback()
{
$value = $this->bot->getConversationAnswer()->getValue();
switch ($value) {
case "minus":
$text='Minus 1, now cont='.$this->cont;
$this->cont-=1;
break;
case "sum":
$text='Sum 1, now cont='.$this->cont;
$this->cont+=1;
break;
default:
$text='cont='.$this->cont;
$this->bot->reply($value);
}
$parameters = [
'text' => $text,
'message_id' => $this->bot->getConversationAnswer()->getMessage()->getPayload()['message_id'],
'parse_mode' => 'HTML',
'reply_markup' => $this->keyboard()
];
$this->bot->sendRequest('editMessageText', $parameters);
$this->bot->storeConversation($this,function() { $this->simpleCallback(); },
$text,['reply_markup' => $this->keyboard()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment