diff --git a/action/ActionProcessor.js b/action/ActionProcessor.js index f981167..833825c 100644 --- a/action/ActionProcessor.js +++ b/action/ActionProcessor.js @@ -47,26 +47,44 @@ class ActionProcessor{ // You should also return the CARD-ACTION-STATUS header in the response. // The value of the header will be displayed to the user. - var card = { - "type": "AdaptiveCard", - "version": "1.0", - "body": [ - { - "size": "large", - "text": "Hello " + result.actionPerformer + ". Your action email was from " + result.sender, - "wrap": true, - "type": "TextBlock" - } - ] - }; + var action = body.action; - cb({ - status: 200, - headers: {'CARD-UPDATE-IN-BODY': 'true'}, - body: card - }) - }); + var comment = body.comment; + + // User's input need to be json encoded to prevent breaking the response json on text replacement + comment = JSON.stringify(comment); + comment = comment.substr(1, comment.length - 2); + // Usually these should be constant from your app config. + var originator = body.originator; + var actionUrl = body.actionUrl; + + var template = "expense_approved.json"; + + if (action === "decline"){ + template = "expense_declined.json"; + } + + var fs = require('fs'); + fs.readFile(__dirname +'/' + template, 'utf8', function (err, data) { + if (err) { + context.log(err); + cb({ + status: 500, + body: err + }); + } + + var cardRaw = data.replace(/\{actionUrl\}/g, actionUrl).replace(/\{originator\}/g, originator).replace(/\{user\}/g, result.actionPerformer).replace(/\{comment\}/g, comment); + + cb({ + status: 200, + headers: {'CARD-UPDATE-IN-BODY': 'true'}, + body: JSON.parse(cardRaw) + }); + }); + } + ); } } diff --git a/action/expense_approved.json b/action/expense_approved.json new file mode 100644 index 0000000..00dee61 --- /dev/null +++ b/action/expense_approved.json @@ -0,0 +1,159 @@ +{ + "$schema":"http://adaptivecards.io/schemas/adaptive-card.json", + "type":"AdaptiveCard", + "originator":"{originator}", + "version":"1.0", + "body":[ + { + "type":"ColumnSet", + "columns":[ + { + "width":6, + "items":[ + { + "type":"Container", + "padding":{ + "left":"small" + }, + "items":[ + { + "type":"Image", + "url":"https://filedepot.blob.core.windows.net/build/logo.png" + } + ] + }, + { + "type":"Container", + "backgroundImage":"https://filedepot.blob.core.windows.net/build/header.png", + "spacing":"small", + "items":[ + { + "type":"TextBlock", + "text":"Expense Report (ER-321377) for 1179 USD is available", + "size":"medium", + "color":"light", + "horizontalAlignment":"left", + "wrap":true + } + ] + }, + { + "type":"Container", + "padding":"small", + "items":[ + { + "type":"ColumnSet", + "columns":[ + { + "type":"Column", + "width":"auto", + "items": [ + { + "type":"Image", + "id":"action_image", + "url":"https://filedepot.blob.core.windows.net/build/approved.png" + } + ] + }, + { + "type":"Column", + "width":"auto", + "items": [ + { + "type":"TextBlock", + "text":"by {user}" + } + ] + } + ] + }, + { + "type":"ColumnSet", + "columns":[ + { + "type":"Column", + "width":"auto", + "items":[ + { + "type":"Image", + "url":"https://filedepot.blob.core.windows.net/profile/9.png", + "style":"person" + } + ] + }, + { + "type":"Column", + "width":"stretch", + "items":[ + { + "type":"TextBlock", + "text":"**Miguel Garcia** submitted on 8th May" + }, + { + "type":"TextBlock", + "text":"Travel & stay expenses for customer visit", + "spacing":"none", + "size":"small" + } + ] + }, + { + "type":"Column", + "width":"auto", + "items":[ + { + "type":"TextBlock", + "text":"$1179", + "size":"medium", + "weight":"bolder" + } + ] + } + ] + }, + { + "type":"ActionSet", + "id":"expenseActions", + "horizontalAlignment":"left", + "actions":[ + { + "type":"Action.Http", + "method":"post", + "url":"{actionUrl}", + "title":"Approve", + "body":"{\"action\": \"approve\", \"id\": \"ER-321377\", \"comment\":\"{{comment.value}}\", \"actionUrl\": \"{actionUrl}\", \"originator\": \"{originator}\"}" + }, + { + "type":"Action.ShowCard", + "title":"Decline", + "card":{ + "type":"AdaptiveCard", + "body":[ + { + "type":"Input.Text", + "id":"comment", + "isMultiline":true, + "placeholder":"Comment (optional)" + } + ], + "actions":[ + { + "type":"Action.Http", + "method":"post", + "url":"{actionUrl}", + "title":"OK", + "body":"{\"action\": \"decline\", \"id\": \"ER-321377\", \"comment\":\"{{comment.value}}\", \"actionUrl\": \"{actionUrl}\", \"originator\": \"{originator}\"}" + } + ] + } + } + ] + } + ] + } + ] + } + ] + } + ] + } \ No newline at end of file diff --git a/action/expense_declined.json b/action/expense_declined.json new file mode 100644 index 0000000..c5fd98e --- /dev/null +++ b/action/expense_declined.json @@ -0,0 +1,159 @@ +{ + "$schema":"http://adaptivecards.io/schemas/adaptive-card.json", + "type":"AdaptiveCard", + "originator":"{originator}", + "version":"1.0", + "body":[ + { + "type":"ColumnSet", + "columns":[ + { + "width":6, + "items":[ + { + "type":"Container", + "padding":{ + "left":"small" + }, + "items":[ + { + "type":"Image", + "url":"https://filedepot.blob.core.windows.net/build/logo.png" + } + ] + }, + { + "type":"Container", + "backgroundImage":"https://filedepot.blob.core.windows.net/build/header.png", + "spacing":"small", + "items":[ + { + "type":"TextBlock", + "text":"Expense Report (ER-321377) for 1179 USD is available", + "size":"medium", + "color":"light", + "horizontalAlignment":"left", + "wrap":true + } + ] + }, + { + "type":"Container", + "padding":"small", + "items":[ + { + "type":"ColumnSet", + "columns":[ + { + "type":"Column", + "width":"auto", + "items": [ + { + "type":"Image", + "id":"action_image", + "url":"https://filedepot.blob.core.windows.net/build/declined.png" + } + ] + }, + { + "type":"Column", + "width":"auto", + "items": [ + { + "type":"TextBlock", + "text":"by {user} with comment: {comment}" + } + ] + } + ] + }, + { + "type":"ColumnSet", + "columns":[ + { + "type":"Column", + "width":"auto", + "items":[ + { + "type":"Image", + "url":"https://filedepot.blob.core.windows.net/profile/9.png", + "style":"person" + } + ] + }, + { + "type":"Column", + "width":"stretch", + "items":[ + { + "type":"TextBlock", + "text":"**Miguel Garcia** submitted on 8th May" + }, + { + "type":"TextBlock", + "text":"Travel & stay expenses for customer visit", + "spacing":"none", + "size":"small" + } + ] + }, + { + "type":"Column", + "width":"auto", + "items":[ + { + "type":"TextBlock", + "text":"$1179", + "size":"medium", + "weight":"bolder" + } + ] + } + ] + }, + { + "type":"ActionSet", + "id":"expenseActions", + "horizontalAlignment":"left", + "actions":[ + { + "type":"Action.Http", + "method":"post", + "url":"{actionUrl}", + "title":"Approve", + "body":"{\"action\": \"approve\", \"id\": \"ER-321377\", \"comment\":\"{{comment.value}}\", \"actionUrl\": \"{actionUrl}\", \"originator\": \"{originator}\"}" + }, + { + "type":"Action.ShowCard", + "title":"Decline", + "card":{ + "type":"AdaptiveCard", + "body":[ + { + "type":"Input.Text", + "id":"comment", + "isMultiline":true, + "placeholder":"Comment (optional)" + } + ], + "actions":[ + { + "type":"Action.Http", + "method":"post", + "url":"{actionUrl}", + "title":"OK", + "body":"{\"action\": \"decline\", \"id\": \"ER-321377\", \"comment\":\"{{comment.value}}\", \"actionUrl\": \"{actionUrl}\", \"originator\": \"{originator}\"}" + } + ] + } + } + ] + } + ] + } + ] + } + ] + } + ] + } \ No newline at end of file diff --git a/action/index.js b/action/index.js index 050974a..914c47a 100644 --- a/action/index.js +++ b/action/index.js @@ -12,7 +12,7 @@ module.exports = function (context, req) { } else { - fs = require('fs'); + var fs = require('fs'); fs.readFile(__dirname +'/instruction.html', 'utf8', function (err, data) { if (err) { context.log(err); diff --git a/action/instruction.html b/action/instruction.html index 6f5db06..da7e71c 100644 --- a/action/instruction.html +++ b/action/instruction.html @@ -14,194 +14,144 @@ Please copy the following template to the playground and send it to yourself by clicking "Send via email" button
+ }@@ -259,7 +209,7 @@ var cardTemplate = $("#cardTemplate").html(); - var card = cardTemplate.replace("{actionUrl}", actionUrl).replace("{originator}", originator); + var card = cardTemplate.replace(/\{actionUrl\}/g, actionUrl).replace(/\{originator\}/g, originator); $("#cardContent").val(card);