deadCL chat-bot using emails
(CHAD)
View tutorials overview
Introduction
This tutorial focuses on using deadCL to send & receive emails. Here we've chosen to use the same Library for object processing as our Parcel chat-bot however our inputs & outputs are performed using CHAD instead of Arnold.The Library & Conversation used are linked below, for both comparison and ease of execution.
Everything shown in blue must be substituted with your own values.
This tutorial will not deal with the initial establishment of DeadLetter or your web server/web presence.
NOTE: before proceeding you'll need to read our Arnold chat-bot tutorial as well.
What you'll learn
- | How to use CHAD (Parcel) to 'send' & 'receive' emails |
---|---|
- | How to use CHAD (Parcel) for inputs & outputs |
- | To parse different object data depending on origin. |
What you'll need
- | A CloudMailin: you'll need a method to convert inbound emails to JSON. Luckily for us cloudmailin.com has done this for us. You can of-course substitute any other service. |
---|---|
- | html POST endpoint Somewhere to receive output from Parcel, this needs to be on a web-server with PHP |
- | Author Key: it goes without saying you'll need an Author Key - setup with a Parcel endpoint |
Step One - Setting up parameters
The first thing we need to do is configure our Parcel endpoint (in/out-bound).The Library below shows how we're structuring our endpoints.
RECEIVE-WEBHOOK; PARCEL- C jSCW3t SEND-WEBHOOK; PARCEL- X-WWW api.domain.com/chat-bot MESSAGE-FROM; PARCEL- B JSON RV_to MESSAGE; PARCEL- B JSON RV_subject B JSON RV_message SEND; X-WWW XW_to X-WWW XW_subject X-WWW XW_message XW_to; PARCEL- to VAL_ $_user XW_subject; PARCEL- subject VAL_ $_subject XW_message; PARCEL- message VAL_ $_reply RV_to; PARCEL- to VAL_ $_user RV_subject; PARCEL- subject VAL_ $_subject {REMOVE, 'RE:,Re:,re:' if found} RV_message; PARCEL- html VAL_ $_message
Receiving Messages
The first step is setting our input (you'll need to an exisiting Parcel endpoint configured for CHAD)In this example we've set our input as lSCW3t you'll need to ensure your Arnold endpoint is configured with this value.
You can learn how here.
RECEIVE-WEBHOOK; PARCEL- A jSCW3t
Sending Messages
Next we need to define our remote endpoint (the URI to send responses to).As a placeholder we've used api.domain.com/chat-bot you'll need to change this to where deadCL is POST responses, or format your Author Key to handle the domain redirect.
You can learn how here.
SEND-WEBHOOK; PARCEL- X-WWW api.domain.com/chat-bot
Handling parameters (in)
To receive emails as JSON from cloudmailin we'll need to configure our Parcel endpoint to capture the relevant data.Here we've set our parameters to be actionable with two KC using Parcel to handle the three values we care about;
RV_to | Who sent the 'email' |
---|---|
RV_subject | What's the subject |
RV_message | The contents of the email body |
MESSAGE-FROM; PARCEL- B JSON RV_to MESSAGE; PARCEL- B JSON RV_subject B JSON RV_message
Handling parameters (out)
Now lets take a look at 'sending' messages back. For the method used in this tutorial we won't be using JSON.Here we've set our parameters to be actionable with one KC using Parcel to handle the three values we care about;
RV_to | Who send the 'email' to |
---|---|
RV_subject | The subject mirrored back |
RV_message | The contents of the email body |
SEND; X-WWW XW_to X-WWW XW_subject X-WWW XW_message
Handling parameters Values (in)
Setting up the parameter values is much easier than one would think. Here we only need to define the body parameter and how we'd like Parcel to treat it.For example html VAL_ $_message tells Parcel we'd like to 'take' the value html & assign it to $_message.
We've also inserted a rule to remove 'reply' references from the subject; this prevents Parcel from inserting "Re:" with each reply.
RV_to; PARCEL- from VAL_ $_user RV_subject; PARCEL- subject VAL_ $_subject {REMOVE, 'RE:,Re:,re:' if found} RV_message; PARCEL- html VAL_ $_message
Handling parameters Values (out)
Creating the payload for the remote endpoint is done in much the same way. with one slightly different substitution values.Here we're using x-www-form-urlencoded instead of JSON a requirement for our 'simple' email send method.
XW_to; PARCEL- to VAL_ $_user XW_subject; PARCEL- subject VAL_ $_subject XW_message; PARCEL- message VAL_ $_reply
Step Two - The Conversation
Lets move onto the Conversation. Our Conversation performs three functions1. | Performing an 'does it make sense' test |
---|---|
2. | Structuring a fluid response to 'X' |
3. | Posting a x-www-form-urlencoded request to your remote endpoint |
dld.keyset('NAME_YOURNAME') *Catch-all - libs REACT; {reply to, respond} 'X', INTERPRET = 'M' RESPOND; { calculate, RESPONSE 'M' = $reply } ^PAUSE POST; ON, REACT PARCEL- C 'SEND' -- SEND-WEBHOOK *END
Adding Libraries
If you don't want to use a local Libraries then the best option (or the very least, safest for testing) is to use the Offical DeadLetter Libraries by adding them all.Don't forget to check your Domain is set correctly in your Author Key.
*Catch-all - libs
Checking interpretation
The first step we'll take is a basic check, that 'X' makes sense before rewriting the object value as; 'M'The inclusion of {reply to, respond} aids our MBS command INTERPRET to correctly 'check' the value.
REACT;
{reply to, respond}
'X', INTERPRET = 'M'
Forming a response
To create a fluid response we're the following MBSCALCULATE | Here calculate is being employed to initiate the creation of our response |
---|---|
RESPONSE | Is informing upon the relationship to 'M' |
If you're wondering how parcel knows the subject & send-to address these are automatically determined as both the input(s) and output(s) use the same substitution value.
RESPOND;
{ calculate, RESPONSE 'M' = $_message }
^PAUSE
Making the Post request
Here we're just posting the values assigned to $_user, $_subject &$_reply as defined in our Library to the endpoint with the value api.domain.com/chat-bot
POST; ON, REACT PARCEL- A 'MESSAGE' -- SEND-WEBHOOK
Step Three - Upload some PHP
Okay so now you've got a way of sending emails into deadCL using Parcel, but no method to send a reply (as an email)To solve this you'll need to upload a small PHP script, designed to send the payload from Parcel as an email.
It's dirty code - designed as a proof of concept and not a final revision as every email sent will be likely be sent straight to spam.
PHPMailer would be more suitable for post-development applications than PHP mail ( )
For the sake of testing we've chosen to use PHP mail ( )
$to = $_POST['recipient']; $subject = $_POST['subject']; $message = $_POST['message']; $header = "From:[email protected] \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; }
You're Done!
By now you should have a basic understanding of using CHAD to create a chat-bot with DeadLetter (deadCL) that communicates using traditional emails.This code can easily be modified to automate customer support or organise personal emails to name a few possibilities.