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

Commit 03d04d1

Browse files
authored
Merge pull request #138 from blopa/feature/messenger_chatbox
add default.xml and chatbox block
2 parents 52bcde2 + d84444c commit 03d04d1

File tree

10 files changed

+314
-37
lines changed

10 files changed

+314
-37
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Magento Chatbot Integration
4+
* Copyright (C) 2017
5+
*
6+
* This file is part of Werules/Chatbot.
7+
*
8+
* Werules/Chatbot is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
namespace Werules\Chatbot\Block\Chatbox;
23+
24+
class Messenger extends \Magento\Framework\View\Element\Template
25+
{
26+
protected $_helper;
27+
protected $_define;
28+
protected $_chatbotAPI;
29+
protected $_store;
30+
31+
public function __construct(
32+
\Magento\Framework\View\Element\Template\Context $context,
33+
\Werules\Chatbot\Helper\Data $helperData,
34+
\Magento\Store\Api\Data\StoreInterface $store,
35+
\Werules\Chatbot\Model\ChatbotAPI $chatbotAPI,
36+
array $data = array()
37+
)
38+
{
39+
$this->_chatbotAPI = $chatbotAPI;
40+
$this->_helper = $helperData;
41+
$this->_store = $store;
42+
$this->_define = new \Werules\Chatbot\Helper\Define;
43+
parent::__construct($context, $data);
44+
}
45+
46+
private function getMessengerInstance()
47+
{
48+
$api_token = $this->_helper->getConfigValue('werules_chatbot_messenger/general/api_key');
49+
$messenger = $this->_chatbotAPI->initMessengerAPI($api_token);
50+
return $messenger;
51+
}
52+
53+
public function getFacebookPageId()
54+
{
55+
$pageId = $this->getConfigValue('werules_chatbot_messenger/general/page_id');
56+
if ($pageId)
57+
return $pageId;
58+
59+
$messengerInstance = $this->getMessengerInstance();
60+
$pageDetails = $messengerInstance->getPageDetails();
61+
if (isset($pageDetails['id']))
62+
{
63+
$pageId = $pageDetails['id'];
64+
$this->setConfigValue('werules_chatbot_messenger/general/page_id', $pageId);
65+
return $pageId;
66+
}
67+
68+
return '';
69+
}
70+
71+
public function getFacebookAppId()
72+
{
73+
$appId = $this->getConfigValue('werules_chatbot_messenger/general/app_id');
74+
return $appId;
75+
}
76+
77+
public function isDomainWhitelisted()
78+
{
79+
$enable = $this->getConfigValue('werules_chatbot_messenger/general/domain_whitelisted');
80+
if ($enable)
81+
return true;
82+
83+
$messengerInstance = $this->getMessengerInstance();
84+
// $url = parse_url($_SERVER['SERVER_NAME'], PHP_URL_HOST);
85+
$url = $_SERVER['SERVER_NAME'];
86+
$domain = array($url);
87+
$result = $messengerInstance->addDomainsToWhitelist($domain);
88+
if (!isset($result['error']))
89+
{
90+
$this->setConfigValue('werules_chatbot_messenger/general/domain_whitelisted', $this->_define::WHITELABELED);
91+
return true;
92+
}
93+
94+
return false;
95+
}
96+
97+
public function isChatboxEnabled()
98+
{
99+
$enable = $this->getConfigValue('werules_chatbot_messenger/general/enable_messenger_box');
100+
$isWhitelisted = $this->isDomainWhitelisted();
101+
if ($enable && $isWhitelisted)
102+
return true;
103+
104+
return false;
105+
}
106+
107+
private function getConfigValue($code)
108+
{
109+
return $this->_helper->getConfigValue($code);
110+
}
111+
112+
private function setConfigValue($field, $value)
113+
{
114+
$this->_helper->setConfigValue($field, $value);
115+
}
116+
117+
public function getLocaleCode()
118+
{
119+
return $this->_store->getLocaleCode();
120+
}
121+
}

Magento2/app/code/Werules/Chatbot/Helper/Define.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Define
3333
const OUTGOING = 1;
3434
const DISABLED = 0;
3535
const ENABLED = 1;
36+
const WHITELABELED = 1;
3637
const NOT_LOGGED = 0;
3738
const NOT_ADMIN = 0;
3839
const ADMIN = 1;

Magento2/app/code/Werules/Chatbot/Model/Api/Messenger.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,28 @@ public function sendReceiptTemplate($chat_id, array $payload) {
303303
);
304304
}
305305

306+
// $whitelist = array(
307+
// 'www.github.com',
308+
// 'www.facebook.com'
309+
// )
310+
public function addDomainsToWhitelist(array $whitelist) {
311+
return $this->endpoint("me/thread_settings",
312+
array(
313+
'setting_type' => 'domain_whitelisting',
314+
'whitelisted_domains' => $whitelist,
315+
'domain_action_type' => 'add'
316+
)
317+
);
318+
}
319+
320+
public function getPageDetails() {
321+
return $this->endpoint(
322+
"me",
323+
array(),
324+
false
325+
);
326+
}
327+
306328
/// Get the text of the current message
307329
public function Text() {
308330
if (isset($this->data["entry"][0]["messaging"][0]["message"]["text"]))

0 commit comments

Comments
 (0)