diff --git a/app/code/community/Werules/Chatbot/Model/Chatdata.php b/app/code/community/Werules/Chatbot/Model/Chatdata.php index ac5882a..5e31659 100644 --- a/app/code/community/Werules/Chatbot/Model/Chatdata.php +++ b/app/code/community/Werules/Chatbot/Model/Chatdata.php @@ -3,8 +3,10 @@ class Werules_Chatbot_Model_Chatdata extends Mage_Core_Model_Abstract { + // APIs public $tg_bot = "telegram"; public $fb_bot = "facebook"; + public $wapp_bot = "whatsapp"; // CONVERSATION STATES public $start_state = 0; @@ -21,6 +23,9 @@ class Werules_Chatbot_Model_Chatdata extends Mage_Core_Model_Abstract public $support_state = 11; public $send_email_state = 12; + // COMMANDS + public $add2card_cmd = "/add2cart"; + public function _construct() { //parent::_construct(); @@ -77,6 +82,30 @@ public function getState($chat_id, $apiType) } } + public function addProd2Cart($prodId) // TODO add try / except + { + // TODO get user session if exists + $product = Mage::getModel('catalog/product')->load($prodId); + $cart = Mage::getModel('checkout/cart'); + $cart->init(); + $cart->addProduct($product, array('qty' => '1')); + $cart->save(); + Mage::getSingleton('checkout/session')->setCartWasUpdated(true); + + $session = Mage::getSingleton('core/session'); + return $session->getEncryptedSessionId(); + } + + public function getCommandValue($text, $cmd) + { + return substr($text, strlen($cmd), strlen($text)); + } + + public function checkCommand($text, $cmd) + { + return substr($text, 0, strlen($cmd)) == $cmd; + } + public function setState($chat_id, $apiType, $state) { if ($apiType == $this->tg_bot) // telegram api @@ -100,15 +129,44 @@ public function setState($chat_id, $apiType, $state) public function handleTelegram($api) { - // Instances the class + // Instances the Telegram class $telegram = new Telegram($api); // Take text and chat_id from the message $text = $telegram->Text(); $chat_id = $telegram->ChatID(); + // Instances the model class + $chatdata = Mage::getModel('chatbot/chatdata')->load($chat_id, 'telegram_chat_id'); + if (!is_null($text) && !is_null($chat_id)) { + // states + if ($this->getState($chat_id, $this->tg_bot) == $this->list_cat_state) + { + $_category = Mage::getModel('catalog/category')->loadByAttribute('name', $text); + //$telegram->sendMessage(array('chat_id' => $chat_id, 'text' => var_export($_category, true))); // TODO debug + $keyb = $telegram->buildKeyBoardHide(true); // hide keyboard built on listing categories + + $productIDs = $_category->getProductCollection()->getAllIds(); + foreach ($productIDs as $productID) + { + $_product = Mage::getModel('catalog/product')->load($productID); + $message = $_product->getName() . "\n/add2cart" . $_product->getId(); // TODO + $telegram->sendMessage(array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => $message)); + } + $this->setState($chat_id, $this->tg_bot, $this->list_prod_state); + } + else if ($this->getState($chat_id, $this->tg_bot) == $this->list_prod_state && $this->checkCommand($text, $this->add2card_cmd)) // TODO + { + $sessionId = $this->addProd2Cart($this->getCommandValue($text, $this->add2card_cmd)); + $sessionUrl = Mage::getBaseUrl(); + if (!isset(parse_url($sessionUrl)['SID'])) + $sessionUrl .= "?SID=" . $sessionId; // add session id to url + + $telegram->sendMessage(array('chat_id' => $chat_id, 'text' => $sessionUrl)); + } + // commands if ($text == "/start") { // started the bot for the first time @@ -150,12 +208,15 @@ public function handleTelegram($api) $this->setState($chat_id, $this->tg_bot, $this->list_cat_state); $helper = Mage::helper('catalog/category'); $categories = $helper->getStoreCategories(); - foreach ($categories as $_category) + $option = array(); + foreach ($categories as $_category) // TODO fix max size { - $message = $_category->getName(); - $content = array('chat_id' => $chat_id, 'text' => $message); - $telegram->sendMessage($content); + array_push($option, $_category->getName()); } + + $keyb = $telegram->buildKeyBoard(array($option)); + $content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "Pick a Category"); + $telegram->sendMessage($content); } else if ($text == "/list_prod") { @@ -209,4 +270,9 @@ public function handleFacebook() { } + + public function handleWhatsapp() + { + + } } \ No newline at end of file diff --git a/app/code/community/Werules/Chatbot/sql/chatbot_setup/mysql4-install-0.0.1.php b/app/code/community/Werules/Chatbot/sql/chatbot_setup/mysql4-install-0.0.1.php index a6492a5..3bb1ac9 100644 --- a/app/code/community/Werules/Chatbot/sql/chatbot_setup/mysql4-install-0.0.1.php +++ b/app/code/community/Werules/Chatbot/sql/chatbot_setup/mysql4-install-0.0.1.php @@ -8,6 +8,7 @@ CREATE TABLE {$this->getTable('wr_chatbot')} ( `entity_id` int(11) unsigned NOT NULL auto_increment, `customer_id` int(11) NULL, + `session_id` varchar(150) NULL, `telegram_chat_id` varchar(50) NULL, `telegram_conv_state` int(10) NOT NULL default '0', `facebook_chat_id` varchar(50) NULL,