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

Commit

Permalink
getting page details from api
Browse files Browse the repository at this point in the history
  • Loading branch information
blopa committed Dec 26, 2017
1 parent e0e7ee3 commit 0d8532b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 118 deletions.
35 changes: 35 additions & 0 deletions Magento2/app/code/Werules/Chatbot/Block/Chatbox/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,46 @@

class Messenger extends \Magento\Framework\View\Element\Template
{
protected $_helper;
protected $_define;
protected $_chatbotAPI;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Werules\Chatbot\Helper\Data $helperData,
\Werules\Chatbot\Model\ChatbotAPI $chatbotAPI,
array $data = array()
)
{
$this->_chatbotAPI = $chatbotAPI;
$this->_helper = $helperData;
$this->_define = new \Werules\Chatbot\Helper\Define;
parent::__construct($context, $data);
}

protected function getMessengerInstance()
{
$api_token = $this->_helper->getConfigValue('werules_chatbot_messenger/general/api_key');
$messenger = $this->_chatbotAPI->initMessengerAPI($api_token);
return $messenger;
}

public function getFacebookPageId()
{
$messengerInstance = $this->getMessengerInstance();
$pageDetails = $messengerInstance->getPageDetails();
$this->_helper->logger($pageDetails);
die;
}

public function getFacebookAppId()
{
$appId = $this->getConfigValue('werules_chatbot_general/general/app_id');
return $appId;
}

public function getConfigValue($code)
{
return $this->_helper->getConfigValue($code);
}
}
8 changes: 8 additions & 0 deletions Magento2/app/code/Werules/Chatbot/Model/Api/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ public function sendReceiptTemplate($chat_id, array $payload) {
);
}

public function getPageDetails() {
return $this->endpoint("me",
array(
'fields' => array('id', 'name', 'fan_count', 'picture', 'is_verified')
)
);
}

/// Get the text of the current message
public function Text() {
if (isset($this->data["entry"][0]["messaging"][0]["message"]["text"]))
Expand Down
62 changes: 33 additions & 29 deletions Magento2/app/code/Werules/Chatbot/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,58 +75,67 @@
<comment>Enable Messenger bot.</comment>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="enable_hub_challenge" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="2" translate="label comment tooltip" type="select">
<label>Enable Hub Challenge Verification</label>
<comment>Enable Hub Challenge verification.</comment>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="disabled_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="3" translate="label comment tooltip" type="text">
<depends>
<field id="werules_chatbot_messenger/general/enable">0</field>
</depends>
<label>Unavailability Message</label>
<comment>Message to send when bot is disabled. Leave it empty to send no message. Please check the maximum size for messages on Messenger API, otherwise your message might not be sent.</comment>
</field>
<field id="api_key" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="4" translate="label comment tooltip" type="text">
<field id="app_id" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="4" translate="label comment tooltip" type="text">
<label>Facebook Application ID</label>
<comment>Your Facebook Application ID.</comment>
</field>
<field id="api_key" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="5" translate="label comment tooltip" type="text">
<label>Page Access Token</label>
<comment>Your page access token.</comment>
</field>
<field id="bot_username" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="5" translate="label comment tooltip" type="text">
<field id="bot_username" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="6" translate="label comment tooltip" type="text">
<label>Facebook Page Username</label>
<comment>Facebook page username.</comment>
</field>
<!--<field id="enable_messenger_box" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="6" translate="label comment tooltip" type="select">-->
<!--<field id="enable_messenger_box" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="7" translate="label comment tooltip" type="select">-->
<!--<label>Enable Facebook Messenger Box</label>-->
<!--<comment>Enable Facebook Messenger box</comment>-->
<!--<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
<!--</field>-->
<!--<field id="messenger_box_width" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="7" translate="label comment tooltip" type="text">-->
<!--<field id="messenger_box_width" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="8" translate="label comment tooltip" type="text">-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/enable_messenger_box">1</field>-->
<!--</depends>-->
<!--<label>Facebook Messenger Box Size</label>-->
<!--<validate>input-number validate-number</validate>-->
<!--<comment>Facebook Messenger box size.</comment>-->
<!--</field>-->
<!--<field id="enable_referral" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="8" translate="label comment tooltip" type="select">-->
<!--<field id="enable_referral" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="9" translate="label comment tooltip" type="select">-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/enable_messenger_box">1</field>-->
<!--</depends>-->
<!--<label>Enable Referral</label>-->
<!--<comment>Enable referral.</comment>-->
<!--<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
<!--</field>-->
<!--<field id="referral_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="9" translate="label comment tooltip" type="text">-->
<!--<field id="referral_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label comment tooltip" type="text">-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/enable_referral">1</field>-->
<!--</depends>-->
<!--<label>Referral Message</label>-->
<!--<comment>Referral message.</comment>-->
<!--</field>-->
<!--<field id="support_group" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label comment tooltip" type="text">-->
<!--<field id="support_group" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="11" translate="label comment tooltip" type="text">-->
<!--<label>Messenger Support Chat ID</label>-->
<!--<comment>The chat ID of the support admin. This feature isn't very useful since you can simply log into your page and directly reply your customers from there.</comment>-->
<!--</field>-->
<field id="welcome_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="11" translate="label comment tooltip" type="text">
<field id="welcome_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="12" translate="label comment tooltip" type="text">
<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">
<field id="enable_message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="13" translate="label comment tooltip" type="select">
<!--TODO-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/welcome_message">1</field>-->
Expand All @@ -135,7 +144,7 @@
<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">
<field id="message_options" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="14" 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>
Expand All @@ -145,27 +154,31 @@
<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">
<field id="about_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="15" translate="label comment tooltip" type="text">
<label>About Message</label>
<comment>Message will be sent when customer asks informations about the store. Please check the maximum size for messages on Facebook API, otherwise your message might not be sent.</comment>
</field>
<field id="help_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="16" 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>
</field>
<field id="enable_help_command_list" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="16" translate="label comment tooltip" type="select">
<field id="enable_help_command_list" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="17" translate="label comment tooltip" type="select">
<label>Enable Command Listing on Help Command</label>
<comment>Enable command listing when customer ask for help.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="commands_list" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="17" translate="label comment tooltip" type="text">
<field id="commands_list" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="18" translate="label comment tooltip" type="text">
<label>Commands List</label>
<comment>Code of the commands and it's alias. Alias must be split by comma. eg.: "support,chat,talk,sac".</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_default_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="18" translate="label comment tooltip" type="select">
<field id="enable_default_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="19" translate="label comment tooltip" type="select">
<label>Enable Default Replies</label>
<comment>Enable default replies.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="default_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="19" translate="label comment tooltip" type="text">
<field id="default_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label comment tooltip" type="text">
<depends>
<field id="werules_chatbot_messenger/general/enable_default_replies">1</field>
</depends>
Expand All @@ -174,20 +187,20 @@
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<frontend_model>Werules\Chatbot\Block\Adminhtml\System\Config\Form\Field\DefaultReplies</frontend_model>
</field>
<field id="enable_nlp_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label comment tooltip" type="select">
<field id="enable_nlp_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="21" translate="label comment tooltip" type="select">
<label>Enable Natural Language Processor Replies (wit.ai)</label>
<comment>Enable Natural Language Processor replies.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="nlp_entity_prefix" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="21" translate="label comment tooltip" type="text">
<field id="nlp_entity_prefix" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="22" translate="label comment tooltip" type="text">
<depends>
<field id="werules_chatbot_messenger/general/enable_nlp_replies">1</field>
</depends>
<label>Natural Language Processor Entity Prefix</label>
<validate>validate-no-html-tags</validate>
<comment>Natural Language Processor entity prefix is a prefix name to flag that the request is coming from Messenger.</comment>
</field>
<field id="nlp_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="22" translate="label comment tooltip" type="text">
<field id="nlp_replies" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="23" translate="label comment tooltip" type="text">
<depends>
<field id="werules_chatbot_messenger/general/enable_nlp_replies">1</field>
</depends>
Expand All @@ -196,11 +209,11 @@
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<frontend_model>Werules\Chatbot\Block\Adminhtml\System\Config\Form\Field\NaturalLanguageProcessorReplies</frontend_model>
</field>
<field id="fallback_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="23" translate="label comment tooltip" type="text">
<field id="fallback_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="24" translate="label comment tooltip" type="text">
<label>Fallback Message</label>
<comment>Message will be sent when the bot don't understand customer messages for the Nst time.</comment>
</field>
<field id="fallback_message_quantity" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="24" translate="label comment tooltip" type="text">
<field id="fallback_message_quantity" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="25" translate="label comment tooltip" type="text">
<!--TODO-->
<!--<depends>-->
<!--<field id="werules_chatbot_messenger/general/fallback_message">1</field>-->
Expand All @@ -209,15 +222,6 @@
<validate>input-number validate-number</validate>
<comment>Number of fallbacks before sending the fallback message.</comment>
</field>
<field id="enable_hub_challenge" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="2" translate="label comment tooltip" type="select">
<label>Enable Hub Challenge Verification</label>
<comment>Enable Hub Challenge verification.</comment>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="about_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="14" translate="label comment tooltip" type="text">
<label>About Message</label>
<comment>Message will be sent when customer asks informations about the store. Please check the maximum size for messages on Facebook API, otherwise your message might not be sent.</comment>
</field>
</group>
</section>
<section id="werules_chatbot_danger" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="3" translate="label comment tooltip">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<script>
// Facebook SDK for JavaScript
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
autoLogAppEvents : true,
xfbml : true,
version : 'v2.11'
});
};
// Facebook SDK for JavaScript
window.fbAsyncInit = function() {
FB.init({
appId : <?php echo $this->getFacebookAppId() ?>,
autoLogAppEvents : true,
xfbml : true,
version : 'v2.11'
});
};

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Original file line number Diff line number Diff line change
@@ -1,73 +1,5 @@
<style>
#werules-messenger-box {
position: fixed;
display: block;
z-index: 999;
bottom: 20px;
right: 30px;
cursor: pointer;
}

#werules-messenger-box:hover .werules-messenger-icon img{
transform:scale(1.1, 1.1);
-webkit-transform:scale(1.1, 1.1);
-moz-transform:scale(1.1, 1.1);
}

.werules-messenger-icon img {
width: 60px;
filter: drop-shadow(2px 6px 4px rgba(0,0,0,.3));
-webkit-filter: drop-shadow(2px 6px 4px rgba(0,0,0,.3));
}

.werules-tiptext{
width: 120px;
background-color: white;
color: #2c2c2c; ;
text-align: center;
padding: 5px 0;
border: 1px solid #eeeeee;
border-radius: 6px;
position: fixed;
bottom: 30px;
right: 100px;
text-transform: uppercase;
filter: drop-shadow(2px 6px 4px rgba(0,0,0,.3));
-webkit-filter: drop-shadow(2px 6px 4px rgba(0,0,0,.3));
}

.werules-tiptext:after{
content: "";
position: absolute;
top: 50%;
left: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent #eeeeee;
}

@media (max-width: 480px)
{
.werules-mobilehidden{
display: none;
}
}
</style>
<?php $fbChatUrl = "http://messenger.com"; ?>
<div id="werules-messenger-box">
<!-- code from https://gist.github.com/tomcritchlow/493774b09b02e31d1184d7cda926c659 -->
<a href="<?php echo $fbChatUrl; ?>" target="_blank" onclick="OpenChatInNewTab();">
<div class="werules-messenger-icon">
<img src="<?php echo $this->getViewFileUrl('Werules_Chatbot::image/messenger-icon.png'); ?>"/>
<span class="werules-tiptext werules-mobilehidden"><?php echo __("Message us"); ?></span>
</div>
</a>
</div>
<script>
function OpenChatInNewTab() {
var screenwidth = screen.width;
var screenheight = window.innerHeight;
window.open('<?php echo $fbChatUrl; ?>', '_blank', 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=' + screenheight + ',left=' + (screenwidth - 500));
}
</script>
<?php $this->getFacebookPageId(); ?>
<div class="fb-customerchat"
page_id="<PAGE_ID>"
ref="<OPTIONAL_WEBHOOK_PARAM>"
</div>

0 comments on commit 0d8532b

Please sign in to comment.