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 #138 from blopa/feature/messenger_chatbox
Browse files Browse the repository at this point in the history
add default.xml and chatbox block
  • Loading branch information
blopa authored Dec 30, 2017
2 parents 52bcde2 + d84444c commit 03d04d1
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 37 deletions.
121 changes: 121 additions & 0 deletions Magento2/app/code/Werules/Chatbot/Block/Chatbox/Messenger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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\Chatbox;

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

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

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

public function getFacebookPageId()
{
$pageId = $this->getConfigValue('werules_chatbot_messenger/general/page_id');
if ($pageId)
return $pageId;

$messengerInstance = $this->getMessengerInstance();
$pageDetails = $messengerInstance->getPageDetails();
if (isset($pageDetails['id']))
{
$pageId = $pageDetails['id'];
$this->setConfigValue('werules_chatbot_messenger/general/page_id', $pageId);
return $pageId;
}

return '';
}

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

public function isDomainWhitelisted()
{
$enable = $this->getConfigValue('werules_chatbot_messenger/general/domain_whitelisted');
if ($enable)
return true;

$messengerInstance = $this->getMessengerInstance();
// $url = parse_url($_SERVER['SERVER_NAME'], PHP_URL_HOST);
$url = $_SERVER['SERVER_NAME'];
$domain = array($url);
$result = $messengerInstance->addDomainsToWhitelist($domain);
if (!isset($result['error']))
{
$this->setConfigValue('werules_chatbot_messenger/general/domain_whitelisted', $this->_define::WHITELABELED);
return true;
}

return false;
}

public function isChatboxEnabled()
{
$enable = $this->getConfigValue('werules_chatbot_messenger/general/enable_messenger_box');
$isWhitelisted = $this->isDomainWhitelisted();
if ($enable && $isWhitelisted)
return true;

return false;
}

private function getConfigValue($code)
{
return $this->_helper->getConfigValue($code);
}

private function setConfigValue($field, $value)
{
$this->_helper->setConfigValue($field, $value);
}

public function getLocaleCode()
{
return $this->_store->getLocaleCode();
}
}
1 change: 1 addition & 0 deletions Magento2/app/code/Werules/Chatbot/Helper/Define.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Define
const OUTGOING = 1;
const DISABLED = 0;
const ENABLED = 1;
const WHITELABELED = 1;
const NOT_LOGGED = 0;
const NOT_ADMIN = 0;
const ADMIN = 1;
Expand Down
22 changes: 22 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,28 @@ public function sendReceiptTemplate($chat_id, array $payload) {
);
}

// $whitelist = array(
// 'www.github.com',
// 'www.facebook.com'
// )
public function addDomainsToWhitelist(array $whitelist) {
return $this->endpoint("me/thread_settings",
array(
'setting_type' => 'domain_whitelisting',
'whitelisted_domains' => $whitelist,
'domain_action_type' => 'add'
)
);
}

public function getPageDetails() {
return $this->endpoint(
"me",
array(),
false
);
}

/// Get the text of the current message
public function Text() {
if (isset($this->data["entry"][0]["messaging"][0]["message"]["text"]))
Expand Down
Loading

0 comments on commit 03d04d1

Please sign in to comment.