Skip to content

Commit

Permalink
Add support for OAuthCards to WebChat (#954)
Browse files Browse the repository at this point in the history
* Support for OAuthCard in WebChat

* Support for OAuthCard attachments

* CR feedback + tests
  • Loading branch information
Jeffders authored and compulim committed Apr 26, 2018
1 parent a35c8e5 commit 617de0a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Attachment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ export const AttachmentView = (props: {
<AdaptiveCardContainer className="signin" nativeCard={ CardBuilder.buildCommonCard(attachment.content) } onCardAction={ props.onCardAction } />
);

case "application/vnd.microsoft.card.oauth":
if (!attachment.content)
return null;
return (
<AdaptiveCardContainer className="signin" nativeCard={ CardBuilder.buildOAuthCard(attachment.content) } onCardAction={ props.onCardAction } />
);

case "application/vnd.microsoft.card.receipt":
if (!attachment.content)
return null;
Expand Down
31 changes: 27 additions & 4 deletions src/CardBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,32 @@ export class AdaptiveCardBuilder {
}
}

addButtons(cardActions: CardAction[]) {

addButtons(cardActions: CardAction[], includesOAuthButtons?: boolean) {
if (cardActions) {
cardActions.forEach(cardAction => {
this.card.addAction(AdaptiveCardBuilder.addCardAction(cardAction));
this.card.addAction(AdaptiveCardBuilder.addCardAction(cardAction, includesOAuthButtons));
});
}
}

private static addCardAction(cardAction: CardAction) {
private static addCardAction(cardAction: CardAction, includesOAuthButtons?: boolean) {
if (cardAction.type === 'imBack' || cardAction.type === 'postBack') {
const action = new SubmitAction();
const botFrameworkCardAction: BotFrameworkCardAction = { __isBotFrameworkCardAction: true, ...cardAction };

action.data = botFrameworkCardAction;
action.title = cardAction.title;

return action;
} else if (cardAction.type === 'signin' && includesOAuthButtons) {
// Create a button specific for OAuthCard 'signin' actions (cardAction.type == signin and button action is Action.Submit)
const action = new SubmitAction();
const botFrameworkCardAction: BotFrameworkCardAction = { __isBotFrameworkCardAction: true, ...cardAction };

action.data = botFrameworkCardAction;
action.title = cardAction.title;

return action;
} else {
const action = new OpenUrlAction();
Expand All @@ -71,10 +81,14 @@ export class AdaptiveCardBuilder {
}
}

addCommon(content: ICommonContent) {
addCommonHeaders(content: ICommonContent) {
this.addTextBlock(content.title, { size: TextSize.Medium, weight: TextWeight.Bolder });
this.addTextBlock(content.subtitle, { isSubtle: true, wrap: true });
this.addTextBlock(content.text, { wrap: true });
}

addCommon(content: ICommonContent) {
this.addCommonHeaders(content);
this.addButtons(content.buttons);
}

Expand Down Expand Up @@ -109,3 +123,12 @@ export const buildCommonCard = (content: ICommonContent): AdaptiveCard => {
cardBuilder.addCommon(content)
return cardBuilder.card;
};

export const buildOAuthCard = (content: ICommonContent): AdaptiveCard => {
if (!content) return null;

const cardBuilder = new AdaptiveCardBuilder();
cardBuilder.addCommonHeaders(content);
cardBuilder.addButtons(content.buttons, true);
return cardBuilder.card;
};
15 changes: 14 additions & 1 deletion src/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,22 @@ export const doCardAction = (
case "playVideo":
case "showImage":
case "downloadFile":
case "signin":
window.open(text);
break;
case "signin":
let loginWindow = window.open();
if (botConnection.getSessionId) {
botConnection.getSessionId().subscribe(sessionId => {
konsole.log("received sessionId: " + sessionId);
loginWindow.location.href = text + encodeURIComponent('&code_challenge=' + sessionId);
}, error => {
konsole.log("failed to get sessionId", error);
});
}
else {
loginWindow.location.href = text;
}
break;

default:
konsole.log("unknown button type", type);
Expand Down
8 changes: 8 additions & 0 deletions test/commands_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ var commands_map: CommandValuesMap = {
sendActivity(conversationId, server_content.si_card);
}
},
"oauth": {
client: function () {
return document.querySelectorAll('button')[0].textContent == "Signin";
},
server: function (conversationId, sendActivity) {
sendActivity(conversationId, server_content.oauth_card);
}
},
"suggested-actions": {
client: function () {
var ul_object = document.querySelectorAll('ul')[0];
Expand Down
4 changes: 4 additions & 0 deletions test/mock_dl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ const getMessages = (conversationId: string, watermark: number, res: express.Res
}
}

app.get('/mock/session/getsessionid', (req, res) => {
res.send({ sessionId: 'mock_session_id' });
});

const getCardJsonFromFs = (fsName: string): Promise<any> => {
return readFileAsync(path.join(__dirname, '../cards/', fsName + '.json'))
.then(function (res) {
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.2",
"botframework-directlinejs": "^0.9.13",
"botframework-directlinejs": "^0.9.15",
"chai": "^4.1.2",
"chai-subset": "^1.6.0",
"cors": "^2.8.4",
Expand Down
26 changes: 26 additions & 0 deletions test/server_content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,32 @@ export var si_card: dl.Message = {
]
}

/*
* Activity for OAuth SignIn
*
*/
export var oauth_card: dl.Message = {
type: "message",
from: bot,
timestamp: new Date().toUTCString(),
channelId: "webchat",
attachments: [
<dl.OAuth>{
contentType: "application/vnd.microsoft.card.oauth",
content: {
text: "Login to OAuth sample",
connectionname: "SampleConnection",
buttons: [
{
type: "signin",
title: "Signin"
}
]
}
}
]
}

/*
* Activity for SuggestedActions
*
Expand Down

0 comments on commit 617de0a

Please sign in to comment.