1

我使用那个 PHP Telegram SDK:https ://github.com/akalongman/php-telegram-bot

如何在当前对话中运行新对话?我有一个 KeyboardButtons,当用户单击“反馈”按钮时,机器人会启动新的对话框(对话),例如“为管理员写消息”。我有一个带有代码的 StartCommand:

<?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;
    }
}
4

1 回答 1

0

许多解决方案之一是为此对话创建专用组。要重定向到该组,只需使用邀请链接(您可以从组描述中获取)并在机器人消息中提供它,如果使用了ReplyKeyboardMarkupInlineKeyboardMarkup按钮的url字段(我认为更喜欢)。

您也可以使用forwardMessage。当用户点击反馈按钮时,让您的机器人按照您所说的发送“为管理员写消息” ,并使用ForceReply选项。在此之后,您需要检查您收到的消息是否为reply_to_message字段不为空。如果是这样 - 然后获取此消息以转发到管理员组(或频道)。

于 2017-03-31T02:29:56.157 回答