Help integration Intercom on 1.8 Version

Hello Dan,
In version 1.7, I’m using integration with intercom using include in the header file theme.

I need your help.
Where do I need to change in version 1.8?

I noticed that base.twig does not accept the incluse after the head tag.

<? Php includes "http: //xxxxxx/intercom_test.js"; ?>

Twig is a templating engine and doesn’t use PHP syntax - you can read all about Twig here: https://twig.sensiolabs.org/

You were including a JS file with PHP before? Could you not just reference that file in <script> tags?

Dan, thanks for the quick support.

Basically I need to send the data from the CMS to the intercom. This is sent via a javascript, below an example:

<Script>
  Window.intercomSettings = {
    App_id: 'APP_ID',
    Name: "Jane Doe", // Full name
    Email: "customer@example.com", // Email address
    Created_at: 1312182000 // Signup date as a Unix timestamp
  };
</script>
<script>
(function () {var w = window; var ic = w.Intercom; if (typeof ic === "function") {ic ('reattach_activator'); ic ('update', intercomSettings);} else (Iqpush (args)}; w.Intercom = i; function l () {var () {var (); S.type = 'text / javascript'; s.async = true; s.src = 'https: //widget.intercom.io/widget/APP_ID'; var x = D.getElementsByTagName ('script') [0]; x.parentNode.insertBefore (s, x);} if (w.attachEvent) {w.attachEvent ('onload', l);} else {w.addEventListener (' Load ', l, false);}}}) () 
</script>

In CMS 1.7 I did the following:
I created a javascript that I added for include in the logo after the header
Within the javascript I used functions in PHP to get the userID, email, and other information from the CMS.

Due to the great change in Xibo architecture, obviously for the better, I’m having a hard time knowing how to integrate with Intercom.

Ah OK - i see what you mean.

The Twig templates - from authed.twig onward have currentUser which is an object containing all of the details relating to the currently logged in user.

You could replace authed.twig in your theme and add an include at the bottom inside the javascriptTemplates block (perhaps we should provide a customHead and customScripts block in a future release)

include('intercom.twig')

Then in your intercom.twig file you can have your script make use of the Twig engine to output the JavaScript you need.

Something like

<script>
  Window.intercomSettings = {
    App_id: 'APP_ID',
    Name: "{{ currentUser.firstName}} {{ currentUser.lastName }}", // Full name
    Email: "{{ currentUser.email }}", // Email address
    Created_at: 1312182000 // Signup date as a Unix timestamp
  };
</script>

This will then be output in the page just before the closing </body>

Dan, thanks, I’ll run the tests.

In full I need to get the information below,

“User_id”: “”, // user email
"Email": “”, // user email
"Name": “”, // username
"Company": {
“Name”: “xxx”, // parameter - row within the setting table
"Plan": “”, // parameter - line within table setting
"Monthly_spend": “0.00”, // parameter - row within table setting
"Licenses": “0”, // amount of CMS licenses on setting parameter
"Displays": “0”, // number of displays in the CMS
"CMS": “”, // parameter - line within the setting table
"Created_at": “1388541600”, // parameter - line within the settingtable

To return the parameters that are in setting, how should I proceed? Before I did for php direct, returning the result of a query into javascript.

Sorry if it is not ideal solution that I had given in the past for CMS 1.7. But it follows the script I used before by include.

My question is I do not know where to start to make this work properly in CMS 1.8. Thank you for all the help you can give me. I need to make this customization to migrate to cms 1.8

> <script id="IntercomSettingsScriptTag"> 
> 	window.intercomSettings = { 
> 	//Users Parameters
> 	"app_id": "XXXXXXX", 
> 	"user_id": "<?php echo Kit::GetParam('username', _SESSION, _USERNAME);?>",
> 	<?php $UserID=Kit::GetParam('userid', _SESSION, _INT, 0);?><?php $Email = mysql_query("SELECT email FROM user where userID=$UserID");$row = mysql_fetch_assoc($Email);$TxtEmail = $row['email'];?>"email": "<?php echo $TxtEmail ;?>", 
> 	"user_hash": "<?php echo hash_hmac('sha256', Kit::GetParam('username', _SESSION, _USERNAME), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); ?>",
> 	"name":null,
> 	"company":{
> 		<?php global $dbname;$GCustomID = mysql_query("SELECT VALUE FROM `setting` WHERE setting='Custom_ID'");$row = mysql_fetch_assoc($GCustomID);$VCustomID = $row['VALUE'];?>"id":"<?php echo $VCustomID;?>",
> 		<?php global $dbname;$GCustomCOMPANY = mysql_query("SELECT VALUE FROM `setting` WHERE setting='Custom_COMPANY'");$row = mysql_fetch_assoc($GCustomCOMPANY);$VCustomCOMPANY = $row['VALUE'];?>"name":"<?php echo $VCustomCOMPANY;?>",
> 		<?php global $dbname;$GCustomPlan = mysql_query("SELECT VALUE FROM `setting` WHERE setting='Custom_Plan'");$row = mysql_fetch_assoc($GCustomPlan);$VCustomPlan = $row['VALUE'];?>"plan":"<?php echo $VCustomPlan;?>",
> 		<?php global $dbname;$GCustomMONTHLYSPEND = mysql_query("SELECT VALUE FROM `setting` WHERE setting='Custom_SPEND'");$row = mysql_fetch_assoc($GCustomMONTHLYSPEND);$VCustomMONTHLYSPEND = $row['VALUE'];?>"monthly_spend":"<?php echo $VCustomMONTHLYSPEND;?>",
> 		<?php global $dbname;$GCustomLicenses = mysql_query("SELECT VALUE FROM `setting` WHERE setting='MAX_LICENSED_DISPLAYS'");$row = mysql_fetch_assoc($GCustomLicenses);$VCustomLicenses = $row['VALUE'];?>"Licenses":"<?php echo $VCustomLicenses;?>",
> 		<?php global $dbname;$GDISPLAYS = mysql_query("SELECT count(*) as DISPLAYS FROM display");$row = mysql_fetch_assoc($GDISPLAYS);$DISPLAYS = $row['DISPLAYS'];?>"Displays":"<?php echo $DISPLAYS;?>",
> 		"CMS":"<?php echo "http://$_SERVER[HTTP_HOST]";?>",
> 		<?php global $dbname;$GCustomID = mysql_query("SELECT VALUE FROM `setting` WHERE setting='Custom_CreatedAT'");$row = mysql_fetch_assoc($GCustomID);$VCustomID = $row['VALUE'];?>"created_at":"<?php echo strtotime($VCustomID);?>",
> 		}
> };
> </script>
> <script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/zuntlo9h';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>

Your settings will already be available in twig using the format:

{{ settings.SETTING_NAME }}

hash_hmac isnt a twig filter, so that makes things more complicated - as does your count of displays, server host, etc.

You will need to append these to the data context that Twig has knowledge of, you can do this using custom middleware. In your middleware create a hook that executes before dispatch:

$app->hook('slim.before.dispatch', function() use($app) {
    // Append your new data into the view
    $app->view()->appendData([
        'gdisplays' => count($this->displayFactory->query(null, ['disableUserCheck' => 1])),
        'host' => $_SERVER['HTTP_HOST']
    ]);
});

You can then use those in your Twig template:

{{ gdisplays }}
{{ host }}

I hope this makes sense

Hello Dan, thank you very much for the directions.

I was able to move forward …

I just could not do the fields below:

Displays":“0”,

CMS:“http://my_cms_url”,

User_Hash // <?php $UserID=Kit::GetParam('userid', _SESSION, _INT, 0);?><?php $Email = mysql_query("SELECT email FROM user where userID=$UserID");$row = mysql_fetch_assoc($Email);$TxtEmail = $row['email'];?>“email”: “<?php echo $TxtEmail ;?>”,

Sorry for my limitation on not knowing how to use twig architecture well.
Thanks if you can guide me with more details of with moving on

Dan, I was able to get the amount of players using
"Displays": “{{displays | length}}”,

Now there are only two items left … we are making progress …
User_hash is the most important, can you guide me in how to achieve without doing something out of the system standards?

Hi Dan, I did it.

I did the following:

I created the intercom.twing file and left it inside the views directory
I put an intercom.twig include in views / authed.twig
I created a public method in in Lib / Entity / user.php called user_hash which returns the hash I needed.

I would like to know if you could guide me on how to make this more professional, as a module for other Xibo users to enjoy.

Changing system default files is always bad when you perform an update and have to merge code.

In your Middleware you would do the following:

$app->view()->appendData([
    'hashed' => hash_hmac('sha256', $app->user->userName, , 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX')
]);

And then in your twig file you can use {{ hashed }}

Sounds like you’ve made some really good progress though!!