/*jshint esversion: 6 */ import { QuickReplyButton } from "./channel"; export interface CardAction{ title : String, text?: String, type?: String, value?: String } interface CardImage { url : String, tap: CardAction[] } export interface HeroCard { title?: String, subtitle?: String, text?: String, images?: any, buttons?: CardAction[] } export class BotFrameworkExtension { // return default Id constructor(private _uid){} normalizeButton(button){ const value = button.url || button.value || `${this._uid}-${button.payload}` || button.title; switch(button.type){ case 'web_url':{ return { title : button.title, type: 'openUrl', value: value } } case 'postback':{ return { title : button.title, type: 'postBack', value: value } } default : { return button; } } } text (text: String, quickReplies?: any){ if (quickReplies && quickReplies.length){ return { attachments : [ this.attachment({ text : text, buttons: (quickReplies || []).map(button=>{ return this.normalizeButton(button) }) }) ] } } return { text : text } } buttonTemplate(text: String, buttons: any) { return { attachments : [ this.attachment({ text : text, buttons: (buttons || []).map(button=>{ return this.normalizeButton(button) }) }) ] } } image(url: string, quickReplies?: QuickReplyButton[]) { return { attachments :[{ contentType : 'image/png', contentUrl: url, } ] } } genericTemplate(items) { return { attachmentLayout: "carousel", attachments: items } } genericItem(title, subtitle, imageUrl, defaultActionUrl, buttons, assetId) { return this.attachment({ title : title, subtitle: subtitle, images: [{ url : imageUrl }], buttons: (buttons || []).map(button=>{ switch(button.type){ case 'web_url':{ return this.urlButton(button.title, button.url) } default:{ return this.postbackButton(button.title, button.payload) } } }) }) } postbackButton(title: String, payload: any) { return { "type": "postBack", "title": title, "value": `${this._uid}-${payload}` }; } urlButton(title: String, url: String) { return { "type": "openUrl", "title": title, "value": url }; } private attachment(card: HeroCard) { const template = { "contentType": "application/vnd.microsoft.card.hero", "content" : card }; return template; } private appendQuickReplyTemplate(template, quickReplies?: QuickReplyButton[]) { if (!quickReplies || !quickReplies.length) { return; } template.content.buttons = quickReplies.map(button => { return { "title": button.title, "type": "postBack", "value": `${this._uid}-${button.payload}` } }); } }