Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add2cart #1

Merged
merged 2 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 71 additions & 5 deletions app/code/community/Werules/Chatbot/Model/Chatdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
{
Expand Down Expand Up @@ -209,4 +270,9 @@ public function handleFacebook()
{

}

public function handleWhatsapp()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down