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 #136 from blopa/feature/welcome_options
Browse files Browse the repository at this point in the history
add welcome message options
  • Loading branch information
blopa authored Dec 17, 2017
2 parents 955c787 + c1e0451 commit 6a801dd
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Magento Chatbot Integration
* Copyright (C) 2017
*
* This file is part of Werules/Chatbot.
*
* Werules/Chatbot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Werules\Chatbot\Block\Adminhtml\System\Config\Form\Field;

class WelcomeOptions extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{
/**
* @var \Magento\Framework\Data\Form\Element\Factory
*/
protected $_elementFactory;
protected $_itemRendererEnable;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Data\Form\Element\Factory $elementFactory,
array $data = array()
)
{
$this->_elementFactory = $elementFactory;
parent::__construct($context,$data);
}
protected function _construct()
{
$this->addColumn('enable_option', array(
'label' => __("Enable"),
'renderer' => $this->_getRendererEnable()
));
$this->addColumn('option_text', array(
'label' => __("Option Text"),
//'style' => 'width: 100%',
'class' => 'validate-no-html-tags'
));

$this->_addAfter = false;
$this->_addButtonLabel = __("Add");
parent::_construct();
}

protected function _getRendererYesNo()
{
return $this->getLayout()->createBlock(
'Werules\Chatbot\Block\Adminhtml\System\Config\Options\YesNo',
'',
array('data' => array('is_render_to_js_template' => true))
// array('is_render_to_js_template' => true)
); // ->setExtraParams('style="width: 100%;"');
}

protected function _getRendererEnable()
{
if (!$this->_itemRendererEnable)
{
$this->_itemRendererEnable = $this->_getRendererYesNo();
}
return $this->_itemRendererEnable;
}

protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
$optionExtraAttr = array();
$optionExtraAttr['option_' . $this->_getRendererEnable()->calcOptionHash($row->getData('enable_option'))] = 'selected="selected"';

$row->setData(
'option_extra_attrs', $optionExtraAttr
);
}
}
58 changes: 55 additions & 3 deletions Magento2/app/code/Werules/Chatbot/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ public function processIncomingMessage($message)
$chatbotAPI = $this->createChatbotAPI($chatbotAPI, $message);
$welcomeMessage = $this->getWelcomeMessage($message);
if ($welcomeMessage)
{
if ($message->getStatus() != $this->_define::PROCESSED)
$message->updateIncomingMessageStatus($this->_define::PROCESSED);

array_push($result, $welcomeMessage);
return $result;
}
}

$enabled = $this->getConfigValue($this->_configPrefix . '/general/enable');
Expand Down Expand Up @@ -1428,8 +1434,43 @@ private function getWelcomeMessage($message)
$text = $this->getConfigValue($this->_configPrefix . '/general/welcome_message');
if ($text != '')
{
$contentObj = $this->getTextMessageArray($text);
$outgoingMessage = $this->createOutgoingMessage($message, reset($contentObj)); // TODO reset -> gets first item of array
// $contentObj = $this->getTextMessageArray($text);
// $outgoingMessage = $this->createOutgoingMessage($message, reset($contentObj)); // TODO reset -> gets first item of array
$enableMessageOptions = $this->getConfigValue($this->_configPrefix . '/general/enable_message_options');
if ($enableMessageOptions == $this->_define::ENABLED)
{
$quickReplies = array();
$welcomeMessageOptions = $this->getWelcomeMessageOptionsData();
foreach ($welcomeMessageOptions as $optionId => $messageOption)
{
if (count($quickReplies) >= $this->_define::MAX_MESSAGE_ELEMENTS)
break;

$quickReply = array(
'content_type' => 'text', // TODO messenger pattern
'title' => $messageOption['option_text'],
'payload' => json_encode(array())
);
array_push($quickReplies, $quickReply);
}

$contentObject = new \stdClass();
$contentObject->message = $text;
$contentObject->quick_replies = $quickReplies;
$content = json_encode($contentObject);
$contentType = $this->_define::QUICK_REPLY;
}
else
{
$contentType = $this->_define::CONTENT_TEXT;
$content = $text;
}
$responseMessage = array(
'content_type' => $contentType,
'content' => $content,
'current_command_details' => json_encode(array())
);
$outgoingMessage = $this->createOutgoingMessage($message, $responseMessage);
// $this->processOutgoingMessage($outgoingMessage);
}

Expand Down Expand Up @@ -2038,6 +2079,16 @@ private function getDefaultRepliesData()
return $defaultReplies;
}

private function getWelcomeMessageOptionsData()
{
$welcomeMessageOptions = array();
$serializedWelcomeMessageOptions = $this->getConfigValue($this->_configPrefix . '/general/message_options');
if ($serializedWelcomeMessageOptions)
$welcomeMessageOptions = $this->_serializer->unserialize($serializedWelcomeMessageOptions);

return $welcomeMessageOptions;
}

private function getChatbotAPIModelBySenderId($senderId)
{
if (isset($this->_chatbotAPIModel))
Expand Down Expand Up @@ -2547,7 +2598,8 @@ private function processHelpCommand()
break;

$payload = array(
'command' => $commandId
'command' => $commandId,
// 'parameter' => ''
);
$quickReply = array(
'content_type' => 'text', // TODO messenger pattern
Expand Down
38 changes: 19 additions & 19 deletions Magento2/app/code/Werules/Chatbot/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,25 @@
<label>Welcome Message</label>
<comment>First message the bot will send to your client. please check the maximum size for messages on Facebook API, otherwise your message might not be sent. you can use "{customername}" to send the customer name.</comment>
</field>
<!--<field id="enable_message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="12" translate="label comment tooltip" type="select">-->
<!--&lt;!&ndash;TODO&ndash;&gt;-->
<!--&lt;!&ndash;<depends>&ndash;&gt;-->
<!--&lt;!&ndash;<field id="werules_chatbot_messenger/general/welcome_message">1</field>&ndash;&gt;-->
<!--&lt;!&ndash;</depends>&ndash;&gt;-->
<!--<label>Enable Welcome Message Options</label>-->
<!--<comment>Enable welcome message options.</comment>-->
<!--<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
<!--</field>-->
<!--<field id="message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="13" translate="label comment tooltip" type="text">-->
<!--&lt;!&ndash;TODO&ndash;&gt;-->
<!--&lt;!&ndash;<depends>&ndash;&gt;-->
<!--&lt;!&ndash;<field id="werules_chatbot_messenger/general/welcome_message">1</field>&ndash;&gt;-->
<!--&lt;!&ndash;</depends>&ndash;&gt;-->
<!--<label>Welcome Message Options</label>-->
<!--<comment>Welcome message options.</comment>-->
<!--<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>-->
<!--<frontend_model>Werules\Chatbot\Block\Adminhtml\System\Config\Form\Field\Commands</frontend_model>-->
<!--</field>-->
<field id="enable_message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="12" translate="label comment tooltip" type="select">
<!--TODO-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/welcome_message">1</field>-->
<!--</depends>-->
<label>Enable Welcome Message Options</label>
<comment>Enable welcome message options.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="13" translate="label comment tooltip" type="text">
<depends>
<!--<field id="werules_chatbot_messenger/general/welcome_message">1</field>--> <!--TODO-->
<field id="werules_chatbot_messenger/general/enable_message_options">1</field>
</depends>
<label>Welcome Message Options</label>
<comment>Welcome message options.</comment>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<frontend_model>Werules\Chatbot\Block\Adminhtml\System\Config\Form\Field\WelcomeOptions</frontend_model>
</field>
<field id="help_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="15" translate="label comment tooltip" type="text">
<label>Help Message</label>
<comment>Message will be sent when customer asks for help. Please check the maximum size for messages on Facebook API, otherwise your message might not be sent.</comment>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Go to **System > General Settings > Chatbot Settings**
- Add Support command
- Add option to list commands on Help Command
- Add Default Replies
- Add Welcome Message Options
- **v1.0.3:**
- Add Messages Queue Modes
- Add option to Clear Message Queue
Expand Down Expand Up @@ -271,7 +272,6 @@ A: Thank you! You can help by codding more features, creating pull requests, or
- Add `CDATA` and `<tooltip>` to configuration descriptions
- Add Promotional Messages
- Add Enable Facebook Messenger Box
- Add Welcome Message Options
- Add more options to filter clearing message queue
- Add handler for other posts from Messenger

Expand Down

0 comments on commit 6a801dd

Please sign in to comment.