How to Become a Chatbot Ninja in 7 Days

Module 3: Lesson 3.2. Advanced Calculating with PHP

Lesson 3.2. Advanced Calculating with PHP

As you might have noticed in the sample in the previous chapter, the default calculation options have bad formatting. There’s a lot of digits behind the decimal separator and this is not the best way to display results to your user.

There are a few ways to make this nicer. First, you can use Zapier to make the calculation in Google Sheets and format the result in a nice way. Or, and as you might expect this has our preference, you can use a PHP script to make the calculation with a Dynamic response message.

The Zapier and Dynamic Content variants will be discussed later in the course where we show you how to connect Zapier to your ManyChat flows and utilize PHP/MySQL.

The formula we will use in our PHP file later is as follows and will display the BMI with a single digit decimal point later in the Dynamic content:

number_format(($weight / (($height/100)*($height)/100)),1) * 1;

If you want to setup your PHP script with ManyChat, you can do it with the following method:

  1. create your PHP file
  2. create a dynamic response node
  3. send the header request to your PHP script with the variables

That's it! Let's look at these things in more detail.

Creating the PHP file

<?php
/**
 * 
 * A lot of time went into the creation of my course and you made a payment to get access
 * Please be kind and do not share this with people who are not a member of my course
 * This is a personal file, provided to YOU, not non paying members
 *
 * 
 * DO NOT RESELL THIS FILE
 * 1 LICENSE IS VALID FOR ONE (1) STUDENT
 *
 * Copyright to ChatbotDojo.com (a Social Source initiative)
 *
 * Lesson 3.2
 * 
 *
 */

header("Content-type: application/json; charset=utf-8");

////----- GET POSTED VARIABLES FROM DYNAMC LINK
$p_id                  = (isset($_GET['p_id']))?$_GET['p_id']:"";    // GET FACEBOOK PAGE ID TO KEEP OTHER PAGES FROM ACCESSING YOUR SCRIPT 
$allowed_pages         = array("123654789","987456321"); // YOUR PAGE NUMBER(S) HERE
if (!in_array($p_id, $allowed_pages))exit;                           // CHECK YOUR PAGE, IF IT S NOT IN THIS LIST EXIT SCRIPT

$height                = (isset($_GET['height']))? $_GET['height']:"";
$weight                = (isset($_GET['weight']))? $_GET['weight']:"";

$bmi                   = number_format(($weight / (($height/100)*($height)/100)),1) * 1;

////----- TEST JSON

$myArray               = array("version" => "v2");
$content               = array();

$actionsArray          = array("action" => "set_field_value", "field_name" => "mb_BMICalc", "value" => $bmi); // field name has to pre-exist
$content['actions'][]  = $actionsArray;

$myArray['content']    = $content;

////----- CONVERT TO JSON
$json                  = json_encode($myArray, JSON_PRETTY_PRINT);
echo $json;
?>

A dynamic response node

To set up the dynamic content, we are going to expand our previous BMI Calculator. We have copied the version from lesson 3.1 and are now adding an extra option to calculate with PHP.

When the user selects the PHP button, a series of events occur.

We set a TAG, called use_php, we will reference that later with a condition to check if we are going to use the default method to calculate, or call the PHP script.

Sending the header request to your PHP script with the variables

The last step is to create a simple URL to your file location.

What this script does, is calculate your BMI (or any other calculation that you can come up with for any variable you need) and write that to your user_field.

Note: your user_field MUST exist before setting up your request, or you will get errors.

Downloads: You need at least a Silver Membership to Access/Download this Content