Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed old school function to fat arrow #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# FavoritableMenu
# FavoritableMenu

<iframe src='https://webchat.botframework.com/embed/dynamicMenu?s=X8mMOM9UpAE.cwA.3es.XVk4eRGKDScILT6j9Zamo9wvSjs4y61VHAxY-bMobwM
'></iframe>
78 changes: 37 additions & 41 deletions bot.js → app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var builder = require('botbuilder');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url);
});

Expand All @@ -25,16 +25,16 @@ server.post('/api/messages', connector.listen());
//=========================================================

bot.dialog('/',
function (session) {
session => {
session.beginDialog("/topLevelMenu");
session.beginDialog("/favoriteMenu");
}
);

bot.dialog('/topLevelMenu',
function (session, args) {
(session, args) => {
var buttons = [];
var card = createHeroCard(session, "Top Level", ["A", "B", "C"]);
var card = createHeroCard(session, "Top Level Dialog", ["A", "B", "C"]);

var msg = new builder.Message(session)
.textFormat(builder.TextFormat.xml)
Expand All @@ -44,12 +44,12 @@ bot.dialog('/topLevelMenu',
);

bot.dialog('/favoriteMenu',
function (session, args){
(session, args) => {
var orderedMenu = getMenuOrderedByScoreDesc(session, 3);

if(orderedMenu && orderedMenu.length > 0){
if (orderedMenu && orderedMenu.length > 0) {
var buttons = [];
orderedMenu.forEach(function(menu) {
orderedMenu.forEach(menu => {
buttons.push(menu.actionname);
});
var card = createHeroCard(session, "Favorite actions", buttons);
Expand All @@ -59,15 +59,14 @@ bot.dialog('/favoriteMenu',
.attachments([card]);
session.endDialog(msg);
}
else{
else {
session.endDialog();
}
}
)

bot.dialog('/A',
function (session, args) {
session.send("In Dialog A");
(session, args) => {
setMenuScore(session, "A");
var buttons = [];
var card = createHeroCard(session, "Dialog A", ["A1", "A2", "A3"]);
Expand All @@ -79,8 +78,7 @@ bot.dialog('/A',
);

bot.dialog('/B',
function (session, args) {
session.send("In Dialog A");
(session, args) => {
setMenuScore(session, "B");
var buttons = [];
var card = createHeroCard(session, "Dialog B", ["B1", "B2", "B3"]);
Expand All @@ -92,8 +90,7 @@ bot.dialog('/B',
);

bot.dialog('/C',
function (session, args) {
session.send("In Dialog A");
(session, args) => {
setMenuScore(session, "C");
var buttons = [];
var card = createHeroCard(session, "Dialog C", ["C1", "C2", "C3"]);
Expand All @@ -105,55 +102,55 @@ bot.dialog('/C',
);

bot.dialog('/A1',
function (session) {
session => {
session.endDialog("In dialog A1");
setMenuScore(session, "A1");
});

bot.dialog('/A2',
function (session) {
session => {
session.endDialog("In dialog A2");
setMenuScore(session, "A2");
});

bot.dialog('/A3',
function (session) {
session => {
session.endDialog("In dialog A3");
setMenuScore(session, "A3");
});

bot.dialog('/B1',
function (session) {
session => {
session.endDialog("In dialog B1");
setMenuScore(session, "B1");
});

bot.dialog('/B2',
function (session) {
session => {
session.endDialog("In dialog B2");
setMenuScore(session, "B2");
});

bot.dialog('/B3',
function (session) {
session => {
session.endDialog("In dialog B3");
setMenuScore(session, "B3");
});

bot.dialog('/C1',
function (session) {
session => {
session.endDialog("In dialog C1");
setMenuScore(session, "C1");
});

bot.dialog('/C2',
function (session) {
session => {
session.endDialog("In dialog C2");
setMenuScore(session, "C2");
});

bot.dialog('/C3',
function (session) {
session => {
session.endDialog("In dialog C3");
setMenuScore(session, "C3");
});
Expand All @@ -178,9 +175,9 @@ bot.beginDialogAction('C3', '/C3');
// Helpers
// ==========================================

function createHeroCard(session, title, buttonNames) {
var createHeroCard = (session, title, buttonNames) => {
var buttons = [];
buttonNames.forEach(function (buttonName) {
buttonNames.forEach(buttonName => {
buttons.push(
builder.CardAction
.dialogAction(session, buttonName, null, "Intent " + buttonName)
Expand All @@ -196,20 +193,19 @@ const MENU_SCORING_NAME = "menuScoring";
var setMenuScore = (session, actionname, actionlabel) => {
let userData = session.userData;

if(!userData[MENU_SCORING_NAME]){
if (!userData[MENU_SCORING_NAME]) {
userData[MENU_SCORING_NAME] = [];
}

let found = false;
for(let menuid in userData[MENU_SCORING_NAME])
{
if(userData[MENU_SCORING_NAME][menuid].actionname === actionname){
userData[MENU_SCORING_NAME][menuid].count ++;
for (let menuid in userData[MENU_SCORING_NAME]) {
if (userData[MENU_SCORING_NAME][menuid].actionname === actionname) {
userData[MENU_SCORING_NAME][menuid].count++;
found = true;
}
}

if(!found){
if (!found) {
userData[MENU_SCORING_NAME].push(
{
actionname: actionname,
Expand All @@ -223,43 +219,43 @@ var setMenuScore = (session, actionname, actionlabel) => {
var getMenuScore = (session, actionname) => {
let userData = session.userData;

if(userData[MENU_SCORING_NAME] && userData[MENU_SCORING_NAME][actionname]){
if (userData[MENU_SCORING_NAME] && userData[MENU_SCORING_NAME][actionname]) {
return userData[MENU_SCORING_NAME][actionname];
}

return 0;
};

var getMenuOrderedByScoreDesc = (session, trigger) => {
if(trigger == undefined)
if (trigger == undefined)
trigger = 0;

let menuScore = session.userData[MENU_SCORING_NAME];
let sortedMenuList = [];

for(let originalid in menuScore){
for (let originalid in menuScore) {
menuButton = menuScore[originalid];

if(menuButton.count < trigger) continue;
if (menuButton.count < trigger) continue;

if(sortedMenuList.length === 0) {
if (sortedMenuList.length === 0) {
sortedMenuList.push(menuButton);
}
else {
var insertAt = -1;
for(let sortedid in sortedMenuList){
for (let sortedid in sortedMenuList) {
sortedMenuButton = sortedMenuList[sortedid];
if(menuButton.count >= sortedMenuButton.count) {
if (menuButton.count >= sortedMenuButton.count) {
insertAt = sortedid;
break;
}
}
if(insertAt === -1)
if (insertAt === -1)
sortedMenuList.push(menuButton);
else
sortedMenuList.splice(insertAt, 0, menuButton);
}
}
}

return sortedMenuList;
return sortedMenuList;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "FavoritableMenu",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down