Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Code for a very basic bot using response metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jcipriano committed Jul 24, 2017
1 parent ef124d5 commit bbb1e6a
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 127 deletions.
12 changes: 5 additions & 7 deletions index.js
Expand Up @@ -2,8 +2,8 @@ var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var security = require('./security')
var message_processor = require('./message-processor.js')
var twitter_config = require('./twitter-config.js')
var message_processor = require('./message-processor')
var twitter = require('./twitter')

app.set('port', (process.env.PORT || 5000));

Expand All @@ -15,7 +15,8 @@ app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')


console.log(twitter_config)
console.log(twitter)


/**
* Serves the home page
Expand All @@ -24,9 +25,6 @@ app.get('/', function(request, response) {
response.render('pages/index')
})

app.get('/foo', function(request, response) {
response.send(twitter_config)
})

/**
* Receives challenge response check (CRC)
Expand All @@ -36,7 +34,7 @@ app.get('/webhooks/twitter', function(request, response) {
var crc_token = request.query.crc_token

if (crc_token) {
var hash = security.get_challenge_response(crc_token, twitter_config.oauth.consumer_secret)
var hash = security.get_challenge_response(crc_token, twitter.oauth.consumer_secret)

response.status(200);
response.send({
Expand Down
40 changes: 27 additions & 13 deletions message-processor.js
@@ -1,5 +1,5 @@
var _ = require('lodash');
var twitter_config = require('./twitter-config.js')
var twitter = require('./twitter')
var messages = require('./messages')

var mp = {}
Expand All @@ -9,16 +9,19 @@ var mp = {}
* Processes incomming events
* @param payload the incomming webhook json payload
*/
mp.process = function(payload) {
mp.process = function (payload) {

// check for direct message events
if(payload.direct_message_events) {

// loop through each event
_.forEach(payload.direct_message_events, function(message_event) {
// check if event is a message_create event and if it is incomming
if(message_event.type == 'message_create' && message_event.message_create.sender_id !== twitter_config.user_id) {

// check if event is a message_create event and if it is incomming by validating sender ID
if(message_event.type == 'message_create' && message_event.message_create.sender_id !== twitter.user_id) {

// process each event individually
mp.process_message_event(message_event)
process_message_event(message_event)
}
});
}
Expand All @@ -29,26 +32,35 @@ mp.process = function(payload) {
* Processes a single message event
* @param message_event a valid Twitter DM message_event
*/
function process_message_event(message_event) {
function process_message_event (message_event) {

console.log('Message recieved from:', message_event.message_create.sender_id)
console.log(message_event.message_create.message_data)

var metadata
var message_to_send
var sender_id

// check for quick reply response
if(message_event.message_create.message_data.quick_reply_response) {
metadata = message_event.message_create.message_data.quick_reply_response.metadata
var message_to_send = messages.get(metadata)
if(message_to_send) {

return;
}
// access the metadata of the quick reply response
metadata = message_event.message_create.message_data.quick_reply_response.metadata
}
// user submitted free form messsage
else {

var message_text = message_event.message_create.message_data.text
metadata = 'default_message'
}


// access sender of the message to reply to
sender_id = message_event.message_create.sender_id

// retrieve response for provided metadata
message_to_send = messages.get(metadata, sender_id)

mp.send_message(message_to_send)
}


Expand All @@ -57,7 +69,9 @@ function process_message_event(message_event) {
* @param msg a valid message event to sent using POST direct_messages/events/new
*/
mp.send_message = function (msg) {
console.log('sending message:', msg)
twitter.send_direct_message(msg, function (error, response, body) {
console.log(body)
})
}


Expand Down
4 changes: 1 addition & 3 deletions messages/default_welcome_message.js
@@ -1,10 +1,8 @@
var demo_features_quick_reply = require('./demo_features_quick_reply')

module.exports = {
"welcome_message": {
"message_data": {
"text": "Select a feature to demo.",
"quick_reply": demo_features_quick_reply
"quick_reply": require('./fragment_demo_features_options')
}
}
}
49 changes: 0 additions & 49 deletions messages/demo_features_menu.js

This file was deleted.

34 changes: 34 additions & 0 deletions messages/feature_buttons.js
@@ -0,0 +1,34 @@
module.exports = {
metadata_trigger: 'feature_buttons',
message_event: {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": undefined
},
"message_data": {
"text": "You can see buttons attached to this message! Some customer experiences require actions outside of Direct Messages. Create interactions that open webviews, Tweet compose windows and other external links. \n\nNotice that you can combine Buttons with a Quick Reply. Select another feature when you are ready.",
"quick_reply": require('./fragment_demo_features_options'),
"ctas": [
{
"type": "web_url",
"label": "Follow @TwitterDev",
"url": "https://twitter.com/intent/follow?screen_name=twitterdev"
},
{
"type": "web_url",
"label": "Visit dev.twitter.com",
"url": "https://dev.twitter.com"
},
{
"type": "web_url",
"label": "Learn more about Buttons",
"url": "https://dev.twitter.com/rest/direct-messages/buttons"
}
]
}
}
}
}
}
22 changes: 22 additions & 0 deletions messages/feature_location_sharing.js
@@ -0,0 +1,22 @@
module.exports = {
metadata_trigger: 'feature_location_sharing',
message_event: {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": undefined
},
"message_data": {
"text": "You can see a request to share your location attached to this message! Location sharing allows business to get essential context for delivering many customer experiences -- examples include an automated location finder bot, helping a human agent interpret complaints about mobile phone service, or personalizing news content.",
"quick_reply": {
"type": "location",
"location": {
"metadata": "feature_location_sharing_response"
}
}
}
}
}
}
}
24 changes: 24 additions & 0 deletions messages/feature_location_sharing_response.js
@@ -0,0 +1,24 @@
module.exports = {
metadata_trigger: 'feature_location_sharing_response',
message_event: {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": undefined
},
"message_data": {
"text": "Select another feature or learn more about Location Sharing.",
"quick_reply": require('./fragment_demo_features_options'),
"ctas": [
{
"type": "web_url",
"label": "Learn more about Location Sharing",
"url": "https://dev.twitter.com/rest/direct-messages/quick-replies/location"
}
]
}
}
}
}
}
4 changes: 2 additions & 2 deletions messages/feature_quick_reply_input.js
Expand Up @@ -8,13 +8,13 @@ module.exports = {
"recipient_id": undefined
},
"message_data": {
"text:": "Normally the text field shows “Send a message” but with the text input version of quick replies you can modify this to give better instructions for what people should enter (ie Email address or Zip code). Enter anything to proceed.",
"text": "Normally the text field shows “Send a message” but with the text input version of quick replies you can modify this to give better instructions for what people should enter (ie Email address or Zip code). Enter anything to proceed.",
"quick_reply": {
"type": "text_input",
"text_input": {
"keyboard": "number",
"label": "Enter any number you want.",
"metadata": "external_id_1"
"metadata": "feature_quick_reply_input_response"
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions messages/feature_quick_reply_input_response.js
@@ -0,0 +1,24 @@
module.exports = {
metadata_trigger: 'feature_quick_reply_input_response',
message_event: {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": undefined
},
"message_data": {
"text": "Select another feature or learn more about Quick Reply Text Input.",
"quick_reply": require('./fragment_demo_features_options'),
"ctas": [
{
"type": "web_url",
"label": "Learn more about Text Input",
"url": "https://dev.twitter.com/rest/direct-messages/quick-replies/text-input"
}
]
}
}
}
}
}
6 changes: 3 additions & 3 deletions messages/feature_quick_reply_options.js
Expand Up @@ -15,17 +15,17 @@ module.exports = {
{
"label": "Option 1",
"description": "A short description for option 1",
"metadata": ""
"metadata": "feature_quick_reply_options_response"
},
{
"label": "Option 2",
"description": "A short description for option 2",
"metadata": ""
"metadata": "feature_quick_reply_options_response"
},
{
"label": "Option 3",
"description": "A short description for option 3",
"metadata": ""
"metadata": "feature_quick_reply_options_response"
}
]
}
Expand Down
24 changes: 24 additions & 0 deletions messages/feature_quick_reply_options_response.js
@@ -0,0 +1,24 @@
module.exports = {
metadata_trigger: 'feature_quick_reply_options_response',
message_event: {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": undefined
},
"message_data": {
"text": "Select another feature or learn more about Quick Reply Options.",
"quick_reply": require('./fragment_demo_features_options'),
"ctas": [
{
"type": "web_url",
"label": "Learn more about Options",
"url": "https://dev.twitter.com/rest/direct-messages/quick-replies/options"
}
]
}
}
}
}
}
25 changes: 25 additions & 0 deletions messages/fragment_demo_features_options.js
@@ -0,0 +1,25 @@
module.exports = {
"type": "options",
"options": [
{
"label": "Quick Reply: Options",
"description": "Prompt a user to select from list of predefined options.",
"metadata": "feature_quick_reply_options"
},
{
"label": "Quick Reply: Text Input",
"description": "Prompt a user with hint text and a restricted keyboard.",
"metadata": "feature_quick_reply_input"
},
{
"label": "Location Sharing",
"description": "Prompt a user to share their location with an interactive map.",
"metadata": "feature_location_sharing"
},
{
"label": "Buttons",
"description": "Prompt a user with buttons linked to URLs.",
"metadata": "feature_buttons"
}
]
}

0 comments on commit bbb1e6a

Please sign in to comment.