The final iteration of’s messaging service draws on much of the previous code that I had used for the Twilio messaging service in my Mark 1 and Mark 2 prototypes.  However, for my final iteration, it would need a few extra functionalities as well as an overhaul on how it analyses the text messages for emotional levels.


Tools used:

  • Twilio
  • PHP
  • IBM Watson API


The script runs every time a phone number texts the messaging service associated with – +447481343320. It takes the Twilio ‘message body’ and ‘from’ POST variables and stores them locally before processing them to check:

  • If the user exists
    • If no user with phone number exists, then create a new user and send them the introductory message.
  • If the message contains an edit password command
    • Edit the password in the database to the one which has been defined using the command
    • Give feedback that the password has been updated.
  • If the message contains an edit levels command
    • Edit the previous entry to match the command (eg A-2 : take 2 values from Anger). This is explained in the introduction text.
    • Give feedback that the levels have been updated.
  • Else
    • Analyse the message using IBM Watson’s Tone Analyzer API and log the text message to the database. Reply with feedback of these levels in a graph format.



Follows is a few key snippets of PHP code which were vital to the text messaging service.



I began by making sure that everything was logging to the database as expected. Once connection to the database was established, I had to retest my existing messaging service with the commands I had designed for implementation.  One of the characters used in the editing commands is a plus symbol. They are also contained in one of the POST values that the script received from Twilio. Plus symbols could potentially cause issues later in the script where I would use cURL and GET requests.  As a result, I reformatted the plus into the “%2B” character which is read in URLs as a plus symbol.  Knowing that this iteration code would be available for use by anyone with the URL, I also implemented a PHP function which protects against SQL injection.

Screen Shot 2016-04-27 at 09.28.09


Next, check if the user exists in the system and feed this into the $userExists variable.

Screen Shot 2016-04-27 at 09.31.55


If the user exists, check if they have an active week. If not, create a new one by sending a GET request to a script. This script creates a directory using Hashids, copies the required scripts for a week of graphs and adds the new week data to the weeks table.

Screen Shot 2016-04-27 at 09.47.49


If the string contains a password update command, update the password in the database.

Screen Shot 2016-04-27 at 09.52.46


Analyse the text for emotional levels using the IBM Watson Tone Analyzer. This works by sending a cURL request to a URL specified in the API documentation. The request returns JSON which can be parsed to reveal the interpreted anger, fear and sadness tone values.

Screen Shot 2016-04-27 at 09.54.25


The command for updating anger if a and an operator is present in the text message – this is the same for fear and sadness.

Screen Shot 2016-04-27 at 10.06.34


If no user with this phone number exists in the database, create a new user by sending a cURL request to a script which creates a user in the database and sends them an introductory text message.

Screen Shot 2016-04-27 at 09.57.25


Finally, once all the scripting is done, the script renders a Twiml (Twilio’s own XML format) document which Twilio uses to send a response back to the phone number. The $reply variable is set throughout the script and rendered as the Message body at the very end.

Screen Shot 2016-04-27 at 09.57.14



I constantly tested this messaging service and invited my friends to play with it.


There are no bugs as of 27/4/16.