2017-03-27 36 views
1

我使用PHP Telegram SDK:https://github.com/akalongman/php-telegram-bot在當前運行新會話

如何在當前對話中運行新的對話?我有一個鍵盤按鈕,當用戶點擊「反饋」按鈕時,機器人啓動新的對話框(對話),就像「爲管理員寫信息」一樣。我有一個帶有代碼的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; 
    } 
} 

回答

0

許多解決方案之一是爲此對話創建專用組。對於重定向到該組只使用邀請鏈接(你可以從組描述得到它),並在機器人的信息提供,如果使用ReplyKeyboardMarkup或按鈕爲InlineKeyboardMarkup(更在我看來較受歡迎)網址場。你

也可以使用forwardMessage。當用戶打反饋按鈕,讓你的機器人發送「寫你的消息管理員」如你所說,讓它與ForceReply選項。在此之後,您需要檢查您收到的郵件是否爲reply_to_message字段不爲空。如果是這樣 - 然後獲取此消息轉發給管理員組(或頻道)。