在当前运行新对话

时间:2017-03-27 15:40:18

标签: php telegram telegram-bot php-telegram-bot

我使用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;
    }
}

1 个答案:

答案 0 :(得分:0)

许多解决方案之一是为此对话创建专用组。要重定向到此群组,只需使用邀请链接(您可以从群组说明中获取)并在机密的消息中提供,如果 ReplyKeyboardMarkup 使用或 url 字段的按钮对于 InlineKeyboardMarkup (在我看来更加优先)。

您也可以使用forwardMessage。当用户点击反馈按钮时,让您的机器人发送&#34;按照您的说法为管理员&#34; 写下您的消息并让它与ForceReply选项一起使用。在此之后,您需要检查收到的消息 reply_to_message 字段不为空。如果是 - 那么请将此消息转发给管理员组(或频道)。