Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from blopa/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
blopa authored Mar 11, 2017
2 parents 2511fa7 + 4695a2d commit f9694aa
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ A big thanks to [Eleirbag89](https://github.com/Eleirbag89/) who wrote [this](ht
- Force exit customer from support mode
- Block a customer for using support mode
- Use Telegram to receive and reply messages from Facebook
- Set custom reply messages for predetermined phrases

**Currently not working with Configurable Products and products with custom options**

Expand Down Expand Up @@ -119,7 +120,7 @@ A: Thank you! You can help by codding more features, creating pull requests, or
- Add compatibility with configurable products
- Add compatibility with products with custom options
- Add better usage of command alias
- Fix categories listing limits
- Fix categories buttons listing for Telegram
- Integrate checkout to Facebook Payment API
- Add inline search for Telegram
- Add natural language configuration (wit.ai?)
Expand Down
22 changes: 22 additions & 0 deletions app/code/community/Werules/Chatbot/Block/Enable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
class Werules_Chatbot_Block_Enable extends Mage_Core_Block_Html_Select
{
public function _toHtml()
{
$options = Mage::getSingleton('chatbot/enable')->toOptionArray();
if (!$this->getOptions())
{
foreach ($options as $option)
{
$this->addOption($option['value'], $option['label']);
}
}

return parent::_toHtml();
}

public function setInputName($value)
{
return $this->setName($value);
}
}
52 changes: 52 additions & 0 deletions app/code/community/Werules/Chatbot/Block/Replies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
class Werules_Chatbot_Block_Replies extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
{
protected $_itemRenderer;

public function _prepareToRender()
{
$this->addColumn('catch_phrase', array(
'label' => Mage::helper('core')->__('Phrase'),
'style' => 'width: 250px'
));
$this->addColumn('reply_phrase', array(
'label' => Mage::helper('core')->__('Reply'),
'style' => 'width: 250px'
));
$this->addColumn('similarity', array(
'label' => Mage::helper('core')->__('Similarity (%)'),
'style' => 'width: 50px',
//'type' => 'number',
//'maxlength' => '3',
'class' => 'input-number validate-number validate-number-range number-range-1-100'
));
$this->addColumn('match_case', array(
'label' => Mage::helper('core')->__('Match Case'),
'renderer' => $this->_getRenderer()
));

$this->_addAfter = false;
$this->_addButtonLabel = Mage::helper('core')->__('Add');
}

protected function _getRenderer()
{
if (!$this->_itemRenderer)
{
$this->_itemRenderer = $this->getLayout()->createBlock(
'werules_chatbot/enable',
'',
array('is_render_to_js_template' => true)
)->setExtraParams("style='width: auto;'");
}
return $this->_itemRenderer;
}

protected function _prepareArrayRow(Varien_Object $row)
{
$row->setData(
'option_extra_attr_' . $this->_getRenderer()->calcOptionHash($row->getData('match_case')),
'selected="selected"'
);
}
}
63 changes: 61 additions & 2 deletions app/code/community/Werules/Chatbot/Model/Api/Facebook/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function facebookHandler($apiKey)

// configs
//$enable_witai = Mage::getStoreConfig('chatbot_enable/witai_config/enable_witai');
$enabledBot = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_bot');
$enableReplies = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_default_replies');
$enablePredict = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_predict_commands');
$enableLog = Mage::getStoreConfig('chatbot_enable/general_config/enable_post_log');
$enableEmptyCategoriesListing = Mage::getStoreConfig('chatbot_enable/general_config/list_empty_categories');
Expand Down Expand Up @@ -109,9 +111,57 @@ public function facebookHandler($apiKey)
else if ($chatdata->getFacebookChatId())
$chatdata->updateChatdata('facebook_message_id', $messageId); // if this fails, it may send the same message twice

// bot enabled/disabled
if ($enabledBot != "1")
{
$disabledMessage = Mage::getStoreConfig('chatbot_enable/facebook_config/disabled_message');
if (!empty($disabledMessage))
$facebook->sendMessage($chatId, $disabledMessage);
return $facebook->respondSuccess();
}

// send feedback to user
$facebook->sendChatAction($chatId, "typing_on");

// handle default replies
if ($enableReplies == "1")
{
$defaultReplies = Mage::getStoreConfig('chatbot_enable/facebook_config/default_replies');
if ($defaultReplies)
{
$replies = unserialize($defaultReplies);
if (is_array($replies))
{
foreach($replies as $reply)
{
$match = $reply["catch_phrase"];
$similarity = $reply["similarity"];
if (is_numeric($similarity))
{
if (!($similarity >= 1 && $similarity <= 100))
$similarity = 100;
}
else
$similarity = 100;

if ($reply["match_case"] == "0")
{
$match = strtolower($match);
$text = strtolower($text);
}

similar_text($text, $match, $percent);
if ($percent >= $similarity)
{
$facebook->sendMessage($chatId, $reply["reply_phrase"]);
return $facebook->respondSuccess();
break; // probably useless
}
}
}
}
}

// payload handler, may change the conversation state
if ($chatdata->getFacebookConvState() == $chatdata->_listProductsState || $chatdata->getFacebookConvState() == $chatdata->_listOrdersState) // listing products
{
Expand Down Expand Up @@ -418,8 +468,8 @@ public function facebookHandler($apiKey)
if ($chatdata->checkCommand($text, $chatdata->_aboutCmd))
{
$message = Mage::getStoreConfig('chatbot_enable/facebook_config/facebook_about_msg'); // TODO
$cmdlisting = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_command_list');
if ($cmdlisting == 1)
$cmdListing = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_command_list');
if ($cmdListing == 1)
{
$message .= "\n\n" . $magehelper->__("Command list") . ":\n";
$replies = array(); // quick replies limit is 10 options
Expand Down Expand Up @@ -587,8 +637,11 @@ public function facebookHandler($apiKey)
break;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$facebook->sendMessage($chatId, $magehelper->__("And that was the last one."));
if (!$chatdata->updateChatdata('facebook_conv_state', $chatdata->_startState))
$facebook->sendMessage($chatId, $chatdata->_errorMessage);
}
}
$i++;
}
Expand Down Expand Up @@ -710,8 +763,11 @@ public function facebookHandler($apiKey)
break;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$facebook->sendMessage($chatId, $magehelper->__("And that was the last one."));
if (!$chatdata->updateChatdata('facebook_conv_state', $chatdata->_startState))
$facebook->sendMessage($chatId, $chatdata->_errorMessage);
}
}
$i++;
}
Expand Down Expand Up @@ -1078,8 +1134,11 @@ public function facebookHandler($apiKey)
$flagBreak = true;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$facebook->sendMessage($chatId, $magehelper->__("And that was the last one."));
if (!$chatdata->updateChatdata('facebook_conv_state', $chatdata->_startState))
$facebook->sendMessage($chatId, $chatdata->_errorMessage);
}

$facebook->sendButtonTemplate($chatId, $message, $buttons);
if ($flagBreak)
Expand Down
63 changes: 61 additions & 2 deletions app/code/community/Werules/Chatbot/Model/Api/Telegram/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function telegramHandler($apiKey)

// configs
//$enable_witai = Mage::getStoreConfig('chatbot_enable/witai_config/enable_witai');
$enabledBot = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_bot');
$enableReplies = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_default_replies');
$enableLog = Mage::getStoreConfig('chatbot_enable/general_config/enable_post_log');
$enableEmptyCategoriesListing = Mage::getStoreConfig('chatbot_enable/general_config/list_empty_categories');
$enableFinalMessage2Support = Mage::getStoreConfig('chatbot_enable/general_config/enable_support_final_message');
Expand All @@ -98,9 +100,57 @@ public function telegramHandler($apiKey)
else if ($chatdata->getTelegramChatId())
$chatdata->updateChatdata('telegram_message_id', $messageId); // if this fails, it may send the same message twice

// bot enabled/disabled
if ($enabledBot != "1")
{
$disabledMessage = Mage::getStoreConfig('chatbot_enable/telegram_config/disabled_message');
if (!empty($disabledMessage))
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $disabledMessage));
return $telegram->respondSuccess();
}

// send feedback to user
$telegram->sendChatAction(array('chat_id' => $chatId, 'action' => 'typing'));

// handle default replies
if ($enableReplies == "1")
{
$defaultReplies = Mage::getStoreConfig('chatbot_enable/telegram_config/default_replies');
if ($defaultReplies)
{
$replies = unserialize($defaultReplies);
if (is_array($replies))
{
foreach($replies as $reply)
{
$match = $reply["catch_phrase"];
$similarity = $reply["similarity"];
if (is_numeric($similarity))
{
if (!($similarity >= 1 && $similarity <= 100))
$similarity = 100;
}
else
$similarity = 100;

if ($reply["match_case"] == "0")
{
$match = strtolower($match);
$text = strtolower($text);
}

similar_text($text, $match, $percent);
if ($percent >= $similarity)
{
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $reply["reply_phrase"]));
return $telegram->respondSuccess();
break; // probably useless
}
}
}
}
}

// show more handler, may change the conversation state
if ($chatdata->getTelegramConvState() == $chatdata->_listProductsState || $chatdata->getTelegramConvState() == $chatdata->_listOrdersState) // listing products
{
Expand Down Expand Up @@ -377,8 +427,8 @@ public function telegramHandler($apiKey)
if ($chatdata->checkCommand($text, $chatdata->_aboutCmd))
{
$message = Mage::getStoreConfig('chatbot_enable/telegram_config/telegram_about_msg'); // TODO
$cmdlisting = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_command_list');
if ($cmdlisting == 1)
$cmdListing = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_command_list');
if ($cmdListing == 1)
{
$message .= "\n\n" . $magehelper->__("Command list") . ":\n";
if ($chatdata->_listCategoriesCmd['command']) $message .= $chatdata->_listCategoriesCmd['command'] . " - " . $magehelper->__("List store categories.") . "\n";
Expand Down Expand Up @@ -532,8 +582,11 @@ public function telegramHandler($apiKey)
break;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $magehelper->__("And that was the last one.")));
if (!$chatdata->updateChatdata('telegram_conv_state', $chatdata->_startState))
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $chatdata->_errorMessage));
}
}
$i++;
}
Expand Down Expand Up @@ -617,8 +670,11 @@ public function telegramHandler($apiKey)
break;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $magehelper->__("And that was the last one.")));
if (!$chatdata->updateChatdata('telegram_conv_state', $chatdata->_startState))
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $chatdata->_errorMessage));
}
}
$i++;
}
Expand Down Expand Up @@ -915,8 +971,11 @@ public function telegramHandler($apiKey)
break;
}
else if (($i + 1) == $total) // if it's the last one, back to _startState
{
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $magehelper->__("And that was the last one.")));
if (!$chatdata->updateChatdata('telegram_conv_state', $chatdata->_startState))
$telegram->sendMessage(array('chat_id' => $chatId, 'text' => $chatdata->_errorMessage));
}
}
$i++;
}
Expand Down
16 changes: 9 additions & 7 deletions app/code/community/Werules/Chatbot/Model/Chatdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,18 @@ protected function getApikey($apiType) // check if bot integration is enabled
{
if ($apiType == $this->_tgBot) // telegram api
{
$enabled = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_bot');
//$enabled = Mage::getStoreConfig('chatbot_enable/telegram_config/enable_bot');
$apikey = Mage::getStoreConfig('chatbot_enable/telegram_config/telegram_api_key');
if ($enabled == 1 && $apikey) // is enabled and has API
//if ($enabled == 1 && $apikey) // is enabled and has API
if ($apikey) // has API
return $apikey;
}
else if ($apiType == $this->_fbBot)
{
$enabled = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_bot');
//$enabled = Mage::getStoreConfig('chatbot_enable/facebook_config/enable_bot');
$apikey = Mage::getStoreConfig('chatbot_enable/facebook_config/facebook_api_key');
if ($enabled == 1 && $apikey) // is enabled and has API
//if ($enabled == 1 && $apikey) // is enabled and has API
if ($apikey) // has API
return $apikey;
}
return null;
Expand Down Expand Up @@ -320,13 +322,13 @@ protected function getCommandString($cmdId)
}
}
if (empty($cmdCode)) // if no command found, return the default
$cmdCode = $defaultCmds[$cmdId - 1];
$cmdCode = $defaultCmds[$cmdId];
}
else // if no command found, return the default
$cmdCode = $defaultCmds[$cmdId - 1];
$cmdCode = $defaultCmds[$cmdId];
}
else // if no command found, return the default
$cmdCode = $defaultCmds[$cmdId - 1];
$cmdCode = $defaultCmds[$cmdId];

$cmdCode = preg_replace( // remove all non-alphanumerics
$this->_unallowedCharacters,
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Werules/Chatbot/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Werules_Chatbot>
<version>0.0.4</version>
<version>0.0.5</version>
</Werules_Chatbot>
</modules>
<frontend>
Expand Down
Loading

0 comments on commit f9694aa

Please sign in to comment.