From 613ee5484165f5e1816a7458ad1b5d618294f4b4 Mon Sep 17 00:00:00 2001 From: Victor Algaze Date: Wed, 13 Oct 2021 04:27:16 -0700 Subject: [PATCH] add addData to SpeedyCard --- README.md | 1 + docs/util.md | 2 +- src/cards.ts | 97 ++++++++++++++++++++++------------------------- test/card.test.ts | 89 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 test/card.test.ts diff --git a/README.md b/README.md index ff8e7b9..8ea19f5 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ export default [{ .setInput(`What's on your mind?`) .setUrl('https://www.youtube.com/watch?v=3GwjfUFyY6M', 'Take a moment to celebrate') .setTable([[`Bot's Date`, new Date().toDateString()], ["Bot's Uptime", `${String(process.uptime())}s`]]) + .setData({mySpecialData: {a:1, b:2}}) bot.sendCard(myCard.render(), 'Your client does not currently support Adaptive Cards') }, helpText: 'Sends an Adaptive Card with an input field to the user' diff --git a/docs/util.md b/docs/util.md index 4440757..2832470 100644 --- a/docs/util.md +++ b/docs/util.md @@ -66,9 +66,9 @@ const cardJson = new SpeedyCard().setTitle('System is 👍', {st}) .setImage('https://i.imgur.com/SW78JRd.jpg') .setInput(`What's on your mind?`) .setUrl('https://www.youtube.com/watch?v=3GwjfUFyY6M', 'Take a moment to celebrate') + .setData({mySpecialData: {a:1, b:2}}) bot.sendCard(cardJson.render(), 'Your client does not currently support Adaptive Cards') - ``` diff --git a/src/cards.ts b/src/cards.ts index d701d45..9a58106 100644 --- a/src/cards.ts +++ b/src/cards.ts @@ -1,4 +1,4 @@ - +import {bad} from './index' export interface BaseConfig { title?: string; titleConfig?: Partial; @@ -52,6 +52,17 @@ export interface inputConfig { placeholder?: string; } +export interface Fact { + title: string; + value: string +} +export interface FactSet { + type: 'FactSet', + facts: Fact[] +} +export interface AttachmentData { + [key: string]: any +} /** * SpeedyCard @@ -91,6 +102,8 @@ export class SpeedyCard { public url = '' public urlLabel = 'Go' public tableData: string[][] = [] + public attachedData: AttachmentData = {} + public needsSubmit = false public json:EasyCardSpec = { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", @@ -166,8 +179,15 @@ export class SpeedyCard { return this } + setData(payload:AttachmentData) { + if (payload) { + this.attachedData = payload + this.needsSubmit = true + } + return this + } + render() { - let needsSubmit = false if (this.title) { const payload:TextBlock = { type: 'TextBlock', @@ -194,52 +214,18 @@ export class SpeedyCard { } if (this.tableData && this.tableData.length) { - const payload = { - "type": "ColumnSet", - "columns": [], - "spacing": "Padding", - "horizontalAlignment": "Center" - } - - const columnsData: { type: string, width: number, items: any[] }[] = [ - { - "type": "Column", - "width": 35, - "items": [] - }, - { - "type": "Column", - "width": 65, - "items": [] - } - ] - - const buildLabel = (label: string) => { - return { - "type": "TextBlock", - "text": label, - "weight": "Bolder", - "color": "Light", - "spacing": "Small" - } - } - const buildValue = (value: string) => { - return { - "type": "TextBlock", - "text": value, - "color": "Light", - "weight": "Lighter", - "spacing": "Small" - } + const payload: FactSet = { + "type": "FactSet", + "facts": [] } this.tableData.forEach(([label, value], i) => { - columnsData[0].items.push(buildLabel(label)) - columnsData[1].items.push(buildValue(value)) + const fact:Fact = { + title: label, + value + } + payload.facts.push(fact) }) - - // @ts-ignore - payload.columns = columnsData this.json.body.push(payload) } @@ -256,7 +242,7 @@ export class SpeedyCard { } if (this.choices.length) { - needsSubmit = true + this.needsSubmit = true const payload: ChoiceBlock = { type: 'Input.ChoiceSet', id: 'choiceSelect', @@ -270,7 +256,7 @@ export class SpeedyCard { } if (this.inputPlaceholder) { - needsSubmit = true + this.needsSubmit = true const payload = { "type": "Input.Text", placeholder: this.inputPlaceholder, @@ -280,15 +266,24 @@ export class SpeedyCard { } - if (needsSubmit) { - const payload = { + if (this.needsSubmit) { + interface SubmitPayload { + type: string; + title: string; + data?: unknown + } + const payload:SubmitPayload = { type: "Action.Submit", title: this.buttonLabel, - "data": { - "cardType": "inputForm" - } + } + if (this.attachedData) { + payload.data = this.attachedData } this.json.actions = [payload] + } else { + if (this.attachedData && Object.keys(this.attachedData).length) { + bad(`attachedData ignore, you must call at least either .setInput() or .setChoices to pass through data with an adaptive card`) + } } if (this.url) { diff --git a/test/card.test.ts b/test/card.test.ts new file mode 100644 index 0000000..ccc7e27 --- /dev/null +++ b/test/card.test.ts @@ -0,0 +1,89 @@ +import test from "tape"; +import {SpeedyCard} from './../src' + +test("setup", function (t) { + t.end(); +}); + +test("Sanity test", (t) => { + const expected = { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.0", + "body": [{ + "type": "TextBlock", + "text": "System is 👍", + "weight": "Bolder", + "size": "Large", + "wrap": true + }] + } + const cardPayload = new SpeedyCard().setTitle('System is 👍') + + + const actual = cardPayload.render() + t.deepEqual(actual, expected); + t.end(); +}); + +test("Kitchen sink", (t) => { + const expected = { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.0", + "body": [{ + "type": "TextBlock", + "text": "System is 👍", + "weight": "Bolder", + "size": "Large", + "wrap": true + }, { + "type": "TextBlock", + "text": "If you see this card, everything is working", + "size": "Small", + "isSubtle": true, + "wrap": true, + "weight": "Lighter" + }, { + "type": "FactSet", + "facts": [{ + "title": "Bot's Uptime", + "value": "12.492006583s" + }] + }, { + "type": "Image", + "url": "https://i.imgur.com/SW78JRd.jpg", + "horizontalAlignment": "Center", + "size": "Large" + }, { + "type": "Input.Text", + "placeholder": "What's on your mind?", + "id": "inputData" + }], + "actions": [{ + "type": "Action.Submit", + "title": "Submit", + "data": { + "mySpecialData": { + "a": 1, + "b": 2 + } + } + }] + } + + const cardPayload = new SpeedyCard().setTitle('System is 👍') + .setSubtitle('If you see this card, everything is working') + .setImage('https://i.imgur.com/SW78JRd.jpg') + .setInput(`What's on your mind?`) + .setTable([["Bot's Uptime", `12.492006583s`]]) + .setData({mySpecialData: { a:1,b:2}}) + + const actual = cardPayload.render() + t.deepEqual(actual, expected); + t.end(); +}); + +test("teardown", function (t) { + t.end(); +}); \ No newline at end of file