Commit 560953fa by Manzar Hussain

facebook massenger module

parent e1cd44ba
# INTRODUCTION
Facebook Messenger Customer Chat Plugin For Drupal.
A simple module that provides the ability to add
Facebook Messenger Customer Chat Pluginto your Drupal site.
For More Information about the plugin:
https://developers.facebook.com/docs/messenger-platform/discovery/customer-chat-plugin
# REQUIREMENTS
1. Whitelist the domain of your website
For security reasons, the plugin will only render when loaded on a
domain that you have whitelisted.
As Facebook states, your domain must be served over HTTPS,
and Uses a fully qualified domain name,
such as https://www.messenger.com/.
IP addresses and localhost are not supported for whitelisting.
To whitelist your domain:
- Go to your Facebook page
- Click Settings at the top of your Page
- Click Messenger Platform on the left
- Edit whitelisted domains for your page in the
Whitelisted Domains section
2. Create Facebook App
You will need to create a Facebook app to be able to get an App ID.
This will serve as an API Key for Facebook so you can access their services.
Go to https://developers.facebook.com to get one.
# INSTALLATION
Open your terminal and download with composer.
composer require drupal/facebook_mcc
OR
1. Download the module to your DRUPAL_ROOT/modules directory,
or where ever you install contrib modules on your site.
2. Go to Admin > Extend and enable the module.
# CONFIGURATION
1. Go to admin/config/services/facebook_mcc
2. Set your Page ID & App ID and save the configurations
3. Place the Facebook MCC Block in your block layout
# AUTHOR
Yasser Samman
https://jo.linkedin.com/in/yasseralsamman
y.samman@codersme.com
facebook_mcc_page_id: ''
facebook_mcc_app_id: ''
facebook_mcc_sdk_version: ''
facebook_mcc_theme_color: ''
facebook_mcc_logged_in_greeting: ''
facebook_mcc_logged_out_greeting: ''
facebook_mcc_greeting_dialog_display: ''
facebook_mcc_greeting_dialog_delay: ''
facebook_mcc_language_mapping: ''
facebook_mcc.settings:
type: config_object
translateble: true
mapping:
facebook_mcc_page_id:
type: string
label: 'Facebook Page ID'
facebook_mcc_app_id:
type: string
label: 'Facebook App ID'
facebook_mcc_sdk_version:
type: string
label: 'Facebook SDK Version'
facebook_mcc_theme_color:
type: string
label: 'Theme Color'
facebook_mcc_logged_in_greeting:
type: text
label: 'Logged In Greeting'
facebook_mcc_logged_out_greeting:
type: text
label: 'Logged Out Greeting'
facebook_mcc_greeting_dialog_display:
type: string
label: 'Greeting Dialog Display'
facebook_mcc_greeting_dialog_delay:
type: string
label: 'Greeting Dialog Delay'
facebook_mcc_language_mapping:
type: sequence
label: 'Language Mapping'
facebook_mcc.settings:
title: 'Facebook MCC Settings'
base_route_name: facebook_mcc.config_form
names:
- facebook_mcc.settings
name: Facebook Messenger Customer Chat
description: Facebook Messenger Customer Chat Plugin For Drupal.
package: Web services
configure: facebook_mcc.config_form
type: module
core: 8.x
core_version_requirement: ^8 || ^9
# Information added by Drupal.org packaging script on 2020-03-16
version: '8.x-1.4'
project: 'facebook_mcc'
datestamp: 1584388771
facebook_mcc:
js:
js/facebook_mcc.js: {}
dependencies:
- core/drupal
- core/jquery
- core/drupalSettings
facebook_mcc.config_form:
title: 'Facebook MCC settings'
description: 'Configure settings for Facebook Messenger Customer Chat module.'
route_name: facebook_mcc.config_form
parent: 'system.admin_config_services'
facebook_mcc.settings:
route_name: facebook_mcc.config_form
title: Settings
base_route: facebook_mcc.config_form
<?php
/**
* @file
* Facebook_mcc module file.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*
* {@inheritdoc}
*/
function facebook_mcc_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.facebook_mcc':
$text = file_get_contents(dirname(__FILE__) . "/README.txt");
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}
facebook_mcc.config_form:
path: '/admin/config/services/facebook_mcc'
defaults:
_form: 'Drupal\facebook_mcc\Form\FacebookMCCForm'
_title: 'Facebook MCC settings'
requirements:
_permission: 'administer site configuration'
(function ($, Drupal) {
'use strict';
Drupal.behaviors.facebook_mcc = {
attach: function (context, settings) {
// SDK Source with localization.
var sdkSrc = 'https://connect.facebook.net/'+drupalSettings.facebook_mcc_locale+'/sdk/xfbml.customerchat.js';
// Block code to inject.
var code = '<div class="fb-customerchat" \
page_id="' + drupalSettings.facebook_mcc_page_id + '" \
theme_color="' + drupalSettings.facebook_mcc_theme_color + '" \
logged_in_greeting="' + drupalSettings.facebook_mcc_logged_in_greeting + '" \
logged_out_greeting="' + drupalSettings.facebook_mcc_logged_out_greeting + '" \
greeting_dialog_delay="' + drupalSettings.facebook_mcc_greeting_dialog_delay + '"';
// Check the mode of greeting display, if it's not default, then add it.
if(drupalSettings.facebook_mcc_greeting_dialog_display != 'default') {
code+= 'greeting_dialog_display="'+drupalSettings.facebook_mcc_greeting_dialog_display+'"';
}
code+='></div>';
// Script to initialize the SDK.
var script = '<script> \
window.fbAsyncInit=function(){ \
FB.init({ \
xfbml:!0, \
version:"v' + drupalSettings.facebook_mcc_sdk_version + '", \
appId:"' + drupalSettings.facebook_mcc_app_id + '" \
}) \
},function(e,t,n){ \
var c,o=e.getElementsByTagName(t)[0]; \
e.getElementById(n)||((c=e.createElement(t)).id=n,c.src="'+sdkSrc+'",o.parentNode.insertBefore(c,o)) \
}(document,"script","facebook-jssdk"); \
</script>';
// Inject code.
$(code).prependTo('body');
$(script).prependTo('body');
$('<div id="fb-root"></div>').prependTo('body');
}
};
})(jQuery, Drupal);
{
"en_US":"en_US",
"ca_ES":"ca_ES",
"cs_CZ":"cs_CZ",
"cx_PH":"cx_PH",
"cy_GB":"cy_GB",
"da_DK":"da_DK",
"de_DE":"de_DE",
"eu_ES":"eu_ES",
"en_UD":"en_UD",
"es_LA":"es_LA",
"es_ES":"es_ES",
"gn_PY":"gn_PY",
"fi_FI":"fi_FI",
"fr_FR":"fr_FR",
"gl_ES":"gl_ES",
"hu_HU":"hu_HU",
"it_IT":"it_IT",
"ja_JP":"ja_JP",
"ko_KR":"ko_KR",
"nb_NO":"nb_NO",
"nn_NO":"nn_NO",
"nl_NL":"nl_NL",
"fy_NL":"fy_NL",
"pl_PL":"pl_PL",
"pt_BR":"pt_BR",
"pt_PT":"pt_PT",
"ro_RO":"ro_RO",
"ru_RU":"ru_RU",
"sk_SK":"sk_SK",
"sl_SI":"sl_SI",
"sv_SE":"sv_SE",
"th_TH":"th_TH",
"tr_TR":"tr_TR",
"ku_TR":"ku_TR",
"zh_CN":"zh_CN",
"zh_HK":"zh_HK",
"zh_TW":"zh_TW",
"af_ZA":"af_ZA",
"sq_AL":"sq_AL",
"hy_AM":"hy_AM",
"az_AZ":"az_AZ",
"be_BY":"be_BY",
"bn_IN":"bn_IN",
"bs_BA":"bs_BA",
"bg_BG":"bg_BG",
"hr_HR":"hr_HR",
"nl_BE":"nl_BE",
"en_GB":"en_GB",
"et_EE":"et_EE",
"fo_FO":"fo_FO",
"fr_CA":"fr_CA",
"ka_GE":"ka_GE",
"el_GR":"el_GR",
"gu_IN":"gu_IN",
"hi_IN":"hi_IN",
"is_IS":"is_IS",
"id_ID":"id_ID",
"ga_IE":"ga_IE",
"jv_ID":"jv_ID",
"kn_IN":"kn_IN",
"kk_KZ":"kk_KZ",
"lv_LV":"lv_LV",
"lt_LT":"lt_LT",
"mk_MK":"mk_MK",
"mg_MG":"mg_MG",
"ms_MY":"ms_MY",
"mt_MT":"mt_MT",
"mr_IN":"mr_IN",
"mn_MN":"mn_MN",
"ne_NP":"ne_NP",
"pa_IN":"pa_IN",
"sr_RS":"sr_RS",
"so_SO":"so_SO",
"sw_KE":"sw_KE",
"tl_PH":"tl_PH",
"ta_IN":"ta_IN",
"te_IN":"te_IN",
"ml_IN":"ml_IN",
"uk_UA":"uk_UA",
"uz_UZ":"uz_UZ",
"vi_VN":"vi_VN",
"km_KH":"km_KH",
"tg_TJ":"tg_TJ",
"ar_AR":"ar_AR",
"he_IL":"he_IL",
"ur_PK":"ur_PK",
"fa_IR":"fa_IR",
"ps_AF":"ps_AF",
"my_MM":"my_MM",
"qz_MM":"qz_MM",
"or_IN":"or_IN",
"si_LK":"si_LK",
"rw_RW":"rw_RW",
"cb_IQ":"cb_IQ",
"ha_NG":"ha_NG",
"ja_KS":"ja_KS",
"br_FR":"br_FR",
"tz_MA":"tz_MA",
"co_FR":"co_FR",
"as_IN":"as_IN",
"ff_NG":"ff_NG",
"sc_IT":"sc_IT",
"sz_PL":"sz_PL"
}
<?php
namespace Drupal\facebook_mcc\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Component\Utility\Color;
use Drupal\Component\Serialization\Json;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Implement config form for Facebook mcc.
*/
class FacebookMCCForm extends ConfigFormBase {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Creates a FacebookMCCForm object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(LanguageManagerInterface $language_manager) {
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'facebook_mcc_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Form constructor.
$form = parent::buildForm($form, $form_state);
// Default settings.
$config = $this->config('facebook_mcc.settings');
// Load localization list from JSON.
$local_list = $this->loadLocalList();
// Form vertical tabs.
$form['plugin_config_tab'] = [
'#type' => 'vertical_tabs',
'#title' => $this->t('Facebook MCC Settings'),
];
// Form bases.
$form['plugin_config'] = [
'#type' => 'details',
'#title' => $this->t('Plugin Configurations'),
'#group' => 'plugin_config_tab',
];
$form['language_config'] = [
'#type' => 'details',
'#title' => $this->t('Language Configurations'),
'#group' => 'plugin_config_tab',
];
// Facebook Page ID Field.
$form['plugin_config']['facebook_mcc_page_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Facebook Page ID'),
'#default_value' => $config->get('facebook_mcc_page_id'),
'#description' => $this->t('Your Facebook page id'),
'#required' => TRUE,
];
// Facebook App ID Field.
$form['plugin_config']['facebook_mcc_app_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Facebook App ID'),
'#default_value' => $config->get('facebook_mcc_app_id'),
'#description' => $this->t('Your Facebook app id'),
'#required' => TRUE,
];
// Facebook SDK Version Field.
$form['plugin_config']['facebook_mcc_sdk_version'] = [
'#type' => 'textfield',
'#title' => $this->t('Facebook JS SDK Version'),
'#default_value' => $config->get('facebook_mcc_sdk_version'),
'#description' => $this->t('Ex: 6.0. You can find the version here: https://developers.facebook.com/docs/javascript/quickstart'),
'#required' => TRUE,
];
// Facebook MCC Theme Color Field.
$form['plugin_config']['facebook_mcc_theme_color'] = [
'#type' => 'textfield',
'#title' => $this->t('Theme Color'),
'#default_value' => $config->get('facebook_mcc_theme_color'),
'#description' => $this->t('The color to use as a theme for the plugin. Ex: #3B5998'),
];
// Facebook MCC Logged In Greeting Field.
$form['plugin_config']['facebook_mcc_logged_in_greeting'] = [
'#type' => 'textarea',
'#title' => $this->t('Logged In Greeting'),
'#rows' => 2,
'#resizable' => 'none',
'#maxlength' => 80,
'#default_value' => $config->get('facebook_mcc_logged_in_greeting'),
'#description' => $this->t('The greeting text that will be displayed if the user is currently logged in to Facebook. Maximum 80 characters.'),
];
// Facebook MCC Logged Out Greeting Field.
$form['plugin_config']['facebook_mcc_logged_out_greeting'] = [
'#type' => 'textarea',
'#title' => $this->t('Logged Out Greeting'),
'#rows' => 2,
'#resizable' => 'none',
'#maxlength' => 80,
'#default_value' => $config->get('facebook_mcc_logged_out_greeting'),
'#description' => $this->t('The greeting text that will be displayed if the user is currently not logged in to Facebook. Maximum 80 characters.'),
];
// Facebook MCC Greeting Dialog Display Field.
$form['plugin_config']['facebook_mcc_greeting_dialog_display'] = [
'#type' => 'select',
'#title' => $this->t('Greeting Dialog Display'),
'#options' => [
'default' => $this->t('Default'),
'show' => $this->t('Show'),
'hide' => $this->t('Hide'),
'fade' => $this->t('Fade'),
],
'#default_value' => $config->get('facebook_mcc_greeting_dialog_display'),
'#description' => $this->t('Sets how the greeting dialog will be displayed.
Default: show on desktop, hide on mobile.
Show: The greeting dialog will always be shown when the plugin loads.
Hide: The greeting dialog of the plugin will always be hidden until a user clicks on the plugin.
Fade: The greeting dialog of the plugin will be shown, then fade away and stay hidden afterwards.
'),
];
// Facebook MCC Logged Out Greeting Field.
$form['plugin_config']['facebook_mcc_greeting_dialog_delay'] = [
'#type' => 'number',
'#title' => $this->t('Greeting Dialog Delay'),
'#min' => 1,
'#default_value' => $config->get('facebook_mcc_greeting_dialog_delay'),
'#description' => $this->t('Sets the number of seconds of delay before the greeting dialog is shown after the plugin is loaded.'),
];
// Facebook MCC Language Localization Settings Table.
$form['language_config']['facebook_mcc_language_map'] = [
'#type' => 'table',
'#header' => [
$this->t('Site Language'),
$this->t('Plugin Display Language'),
],
'#empty' => $this->t('There are no enabled languages.'),
];
// Fill Languages table with enabled languages.
$language_list = $this->languageManager->getLanguages();
$language_map = $config->get('facebook_mcc_language_map');
foreach ($language_list as $language) {
$form['language_config']['facebook_mcc_language_map'][$language->getId()]['language'] = [
'#plain_text' => $language->getName(),
];
$form['language_config']['facebook_mcc_language_map'][$language->getId()]['localization'] = [
$form['language_config']['facebook_mcc_language_map']['local'] = [
'#type' => 'select',
'#options' => $local_list,
'#default_value' => (isset($language_map[$language->getId()])) ? $language_map[$language->getId()]['localization'] : 'en_US',
],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Validate Theme Color Field.
$color = $form_state->getValue('facebook_mcc_theme_color');
if (!Color::validateHex($color) && strlen($color) != 0) {
$form_state->setErrorByName('facebook_mcc_theme_color', $this->t('Color must be a 6-digit hexadecimal value.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('facebook_mcc.settings');
$config->set('facebook_mcc_page_id', $form_state->getValue('facebook_mcc_page_id'));
$config->set('facebook_mcc_app_id', $form_state->getValue('facebook_mcc_app_id'));
$config->set('facebook_mcc_sdk_version', $form_state->getValue('facebook_mcc_sdk_version'));
$config->set('facebook_mcc_theme_color', $form_state->getValue('facebook_mcc_theme_color'));
$config->set('facebook_mcc_logged_in_greeting', $form_state->getValue('facebook_mcc_logged_in_greeting'));
$config->set('facebook_mcc_logged_out_greeting', $form_state->getValue('facebook_mcc_logged_out_greeting'));
$config->set('facebook_mcc_greeting_dialog_display', $form_state->getValue('facebook_mcc_greeting_dialog_display'));
$config->set('facebook_mcc_greeting_dialog_delay', $form_state->getValue('facebook_mcc_greeting_dialog_delay'));
$config->set('facebook_mcc_language_map', $form_state->getValue('facebook_mcc_language_map'));
$config->save();
return parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'facebook_mcc.settings',
];
}
/**
* {@inheritdoc}
*/
private function loadLocalList() {
return Json::decode(file_get_contents(__DIR__ . '/../../locale.json'));
}
}
<?php
namespace Drupal\facebook_mcc\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides Facebook Messenger Customer Chat Block.
*
* @Block(
* id = "facebook_mcc_block",
* admin_label = @Translation("Facebook Messenger Customer Chat"),
* category = @Translation("Facebook Messenger Customer Chat"),
* )
*/
class FacebookMCCBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The configuration object.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $facebookMccConfig;
/**
* Creates a FacebookMCCBlock object.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key ‘context’ may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->facebookMccConfig = $config_factory->get('facebook_mcc.settings');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function build() {
// Check site language and set plugin language accordingly.
$current_language = $this->languageManager->getCurrentLanguage();
$language_map = $this->facebookMccConfig->get('facebook_mcc_language_map');
if ($language_map && count($language_map) > 0) {
$plugin_locale = $language_map[$current_language->getId()]['localization'];
}
else {
$plugin_locale = 'en_US';
}
// Build block.
$facebook_mcc_rendered_block = [
'#attributes' => [
'class' => ['fb-customerchat-block'],
],
'#attached' => [
'library' => [
'facebook_mcc/facebook_mcc',
],
'drupalSettings' => [
'facebook_mcc_sdk_version' => $this->facebookMccConfig->get('facebook_mcc_sdk_version'),
'facebook_mcc_app_id' => $this->facebookMccConfig->get('facebook_mcc_app_id'),
'facebook_mcc_page_id' => $this->facebookMccConfig->get('facebook_mcc_page_id'),
'facebook_mcc_theme_color' => $this->facebookMccConfig->get('facebook_mcc_theme_color'),
'facebook_mcc_logged_in_greeting' => $this->facebookMccConfig->get('facebook_mcc_logged_in_greeting'),
'facebook_mcc_logged_out_greeting' => $this->facebookMccConfig->get('facebook_mcc_logged_out_greeting'),
'facebook_mcc_greeting_dialog_delay' => $this->facebookMccConfig->get('facebook_mcc_greeting_dialog_delay'),
'facebook_mcc_greeting_dialog_display' => $this->facebookMccConfig->get('facebook_mcc_greeting_dialog_display'),
'facebook_mcc_locale' => $plugin_locale,
],
],
];
return $facebook_mcc_rendered_block;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment