Answer the question
In order to leave comments, you need to log in
Telegram Bot API (PHP). How to start a new conversation in an existing one?
Hello. I am working with the following library: https://github.com/akalongman/php-telegram-bot
How can I start a new Conversation in the current one? I have a StartCommand.php and it uses Conversation and a menu in the form of Keyboard buttons. When the user selects the button, the bot will display some text. So, it is necessary that after pressing the "Feedback" button, a new Conversation is launched, where the user will be asked to send a message to the admins.
<?php
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\InlineKeyboard;
use Longman\TelegramBot\Entities\InlineKeyboardButton;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\KeyboardButton;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Request;
class StartCommand extends SystemCommand
{
protected $name = 'start';
protected $description = 'Start command';
protected $usage = '/start';
protected $version = '1.1.0';
public function execute()
{
$message = $this->getMessage();
$chat = $message->getChat();
$user = $message->getFrom();
$text = trim($message->getText(true));
$chat_id = $chat->getId();
$user_id = $user->getId();
$data = [
'chat_id' => $chat_id,
];
if ($chat->isGroupChat() || $chat->isSuperGroup()) {
$data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
}
// Conversation start
$this->conversation = new Conversation($user_id, $chat_id, $this->getName());
$notes = &$this->conversation->notes;
!is_array($notes) && $notes = [];
// cache data from the tracking session if any
$state = 0;
if (isset($notes['state'])) {
$state = $notes['state'];
}
$result = Request::emptyResponse();
switch ($state) {
case 0:
$keyboards = [];
$keyboards[] = new Keyboard(
['Information'],
['Books', 'Feedback'],
['Hide menu']
);
$keyboard = $keyboards[0]
->setResizeKeyboard(true)
->setOneTimeKeyboard(false)
->setSelective(false);
if ($text === '') {
$notes['state'] = 0;
$this->conversation->update();
$data['text'] = 'Please choose a section:';
$data['reply_markup'] = $keyboard;
$result = Request::sendMessage($data);
}
elseif ($text === 'Information') {
$this->conversation->update();
$data['text'] = file_get_contents(__DIR__ . '/../../other/texts/infrormation.txt');
$data['reply_markup'] = $keyboard;
$result = Request::sendMessage($data);
}
elseif ($text === 'Books') {
$this->conversation->update();
$data['text'] = file_get_contents(__DIR__ . '/../../other/texts/books.txt');
$data['reply_markup'] = $keyboard;
$result = Request::sendMessage($data);
}
elseif ($text === 'Feedback') {
/**
* HERE NEED START NEW CONSERVATION
* LIKE THESE (StartCommand or SurveyCommand)
*/
}
elseif ($text === 'Hide menu') {
$this->conversation->stop();
$data['text'] = 'Done!';
$data['reply_markup'] = Keyboard::remove();
$result = Request::sendMessage($data);
}
$notes['name'] = $text;
$text = '';
}
return $result;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question