Skip to content
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
33 changes: 16 additions & 17 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
const {
registerAction,
getAction,
run,
prompt,
} = require('@bottender/proposal-conversation');

// Register the action before handling events
registerAction('AskLikeCheeseOrNot', async function AskLikeCheeseOrNot(
context,
props
) {
if (!props.result) {
await context.sendText('Do you like cheese? (yes/no)');
return prompt('result');
}
const AskLikeCheeseOrNot = registerAction(
'AskLikeCheeseOrNot',
async function AskLikeCheeseOrNot(context, props) {
if (!props.result) {
await context.sendText('Do you like cheese? (yes/no)');
return prompt('result');
}

if (props.result === 'yes') {
await context.sendText('You said yes! How wonderful.');
} else if (props.result === 'no') {
await context.sendText('You said no, that is too bad.');
} else {
await context.sendText('Sorry I did not understand.');
if (props.result === 'yes') {
await context.sendText('You said yes! How wonderful.');
} else if (props.result === 'no') {
await context.sendText('You said no, that is too bad.');
} else {
await context.sendText('Sorry I did not understand.');
}
}
});
);

module.exports = run(function App() {
return getAction('AskLikeCheeseOrNot');
return AskLikeCheeseOrNot;
});
90 changes: 46 additions & 44 deletions examples/line-confirm-template/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { router, text } = require('bottender/router');
const {
registerAction,
getAction,
run,
prompt,
} = require('@bottender/proposal-conversation');

registerAction(
const AskLikeCheeseOrNotByTextActions = registerAction(
'AskLikeCheeseOrNotByTextActions',
async function AskLikeCheeseOrNotByTextActions(context, props) {
if (!props.result) {
Expand Down Expand Up @@ -38,54 +37,57 @@ registerAction(
}
);

registerAction('AskLikeCheeseOrNotByPayloadActions', {
getProps: ({ key, context, prevProps }) => {
if (key === 'result' && context.event.isPayload) {
const AskLikeCheeseOrNotByPayloadActions = registerAction(
'AskLikeCheeseOrNotByPayloadActions',
{
getProps: ({ key, context, prevProps }) => {
if (key === 'result' && context.event.isPayload) {
return {
...prevProps,
result: context.event.payload === 'YES' ? 'yes' : 'no',
};
}
return {
...prevProps,
result: context.event.payload === 'YES' ? 'yes' : 'no',
[key]: context.event.text,
};
}
return {
...prevProps,
[key]: context.event.text,
};
},
action: async function AskLikeCheeseOrNotByPayloadActions(context, props) {
if (!props.result) {
await context.sendConfirmTemplate('Do you like cheese?', {
text: 'Do you like cheese?',
actions: [
{
type: 'postback',
label: 'Yes',
data: 'YES',
displayText: 'yes',
},
{
type: 'postback',
label: 'No',
data: 'NO',
displayText: 'no',
},
],
});
return prompt('result');
}
},
action: async function AskLikeCheeseOrNotByPayloadActions(context, props) {
if (!props.result) {
await context.sendConfirmTemplate('Do you like cheese?', {
text: 'Do you like cheese?',
actions: [
{
type: 'postback',
label: 'Yes',
data: 'YES',
displayText: 'yes',
},
{
type: 'postback',
label: 'No',
data: 'NO',
displayText: 'no',
},
],
});
return prompt('result');
}

if (props.result === 'yes') {
await context.sendText('You said yes! How wonderful.');
} else if (props.result === 'no') {
await context.sendText('You said no, that is too bad.');
} else {
await context.sendText('Sorry I did not understand.');
}
},
});
if (props.result === 'yes') {
await context.sendText('You said yes! How wonderful.');
} else if (props.result === 'no') {
await context.sendText('You said no, that is too bad.');
} else {
await context.sendText('Sorry I did not understand.');
}
},
}
);

module.exports = run(function App() {
return router([
text('payload', getAction('AskLikeCheeseOrNotByPayloadActions')),
text('*', getAction('AskLikeCheeseOrNotByTextActions')),
text('payload', AskLikeCheeseOrNotByPayloadActions),
text('*', AskLikeCheeseOrNotByTextActions),
]);
});
13 changes: 6 additions & 7 deletions examples/line-datetime-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { router, text } = require('bottender/router');
const {
registerAction,
getAction,
run,
prompt,
} = require('@bottender/proposal-conversation');

registerAction('AskDate', {
const AskDate = registerAction('AskDate', {
getProps: ({ key, context, prevProps }) => {
if (
key === 'date' &&
Expand Down Expand Up @@ -47,7 +46,7 @@ registerAction('AskDate', {
},
});

registerAction('AskTime', {
const AskTime = registerAction('AskTime', {
getProps: ({ key, context, prevProps }) => {
if (
key === 'time' &&
Expand Down Expand Up @@ -88,7 +87,7 @@ registerAction('AskTime', {
},
});

registerAction('AskDatetime', {
const AskDatetime = registerAction('AskDatetime', {
getProps: ({ key, context, prevProps }) => {
if (
key === 'datetime' &&
Expand Down Expand Up @@ -131,8 +130,8 @@ registerAction('AskDatetime', {

module.exports = run(function App() {
return router([
text('date', getAction('AskDate')),
text('time', getAction('AskTime')),
text('*', getAction('AskDatetime')),
text('date', AskDate),
text('time', AskTime),
text('*', AskDatetime),
]);
});
5 changes: 2 additions & 3 deletions examples/line-quick-replies/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const {
registerAction,
getAction,
run,
prompt,
} = require('@bottender/proposal-conversation');

registerAction(
const AskLikeCheeseOrNotByTextQuickReplies = registerAction(
'AskLikeCheeseOrNotByTextQuickReplies',
async function AskLikeCheeseOrNotByTextQuickReplies(context, props) {
if (!props.result) {
Expand Down Expand Up @@ -45,5 +44,5 @@ registerAction(
);

module.exports = run(function App() {
return getAction('AskLikeCheeseOrNotByTextQuickReplies');
return AskLikeCheeseOrNotByTextQuickReplies;
});
5 changes: 2 additions & 3 deletions examples/messenger-quick-replies/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const {
registerAction,
getAction,
run,
prompt,
} = require('@bottender/proposal-conversation');

registerAction(
const AskLikeCheeseOrNotByTextQuickReplies = registerAction(
'AskLikeCheeseOrNotByTextQuickReplies',
async function AskLikeCheeseOrNotByTextQuickReplies(context, props) {
if (!props.result) {
Expand Down Expand Up @@ -37,5 +36,5 @@ registerAction(
);

module.exports = run(function App() {
return getAction('AskLikeCheeseOrNotByTextQuickReplies');
return AskLikeCheeseOrNotByTextQuickReplies;
});
97 changes: 48 additions & 49 deletions examples/slot-filling-confirmation/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
const {
registerAction,
getAction,
run,
prompt,
setField,
deleteField,
} = require('@bottender/proposal-conversation');

registerAction('AskNameAndPhone', async function AskNameAndPhone(
context,
props
) {
if (!props.name) {
await context.sendText('Please type your name:');
return prompt('name');
}
const AskNameAndPhone = registerAction(
'AskNameAndPhone',
async function AskNameAndPhone(context, props) {
if (!props.name) {
await context.sendText('Please type your name:');
return prompt('name');
}

if (props.name.length > 30) {
const name = `${props.name.slice(0, 27)}...`;
setField(context, 'name', name);
await context.sendText(
`Name can't be more than 30 characters. Set your name to ${name}`
);
}
if (props.name.length > 30) {
const name = `${props.name.slice(0, 27)}...`;
setField(context, 'name', name);
await context.sendText(
`Name can't be more than 30 characters. Set your name to ${name}`
);
}

if (!props.phone) {
await context.sendText('Please type your phone number (10 digits):');
return prompt('phone');
}
if (!props.phone) {
await context.sendText('Please type your phone number (10 digits):');
return prompt('phone');
}

if (props.phone && props.phone.length !== 10) {
await context.sendText(
`Your input ${props.phone} is invalid. Please retype your phone again (10 digits):`
);
return prompt('phone');
}
if (props.phone && props.phone.length !== 10) {
await context.sendText(
`Your input ${props.phone} is invalid. Please retype your phone again (10 digits):`
);
return prompt('phone');
}

if (!props.confirm) {
await context.sendText(
`${props.name}, your phone number is ${props.phone}, right? If not, which part you want to refill? (yes|name|phone|both)`
);
return prompt('confirm');
}
if (!props.confirm) {
await context.sendText(
`${props.name}, your phone number is ${props.phone}, right? If not, which part you want to refill? (yes|name|phone|both)`
);
return prompt('confirm');
}

if (props.confirm === 'yes') {
await context.sendText(
`Thank you for your help, your personal data was stored correctly.`
);
} else if (props.confirm === 'name') {
deleteField(context, ['name', 'confirm']);
await context.sendText('Please retype your name:');
return prompt('name');
} else if (props.confirm === 'phone') {
deleteField(context, ['phone', 'confirm']);
await context.sendText('Please retype your phone again (10 digits):');
return prompt('phone');
} else if (props.confirm === 'both') {
deleteField(context, ['confirm', 'name', 'phone']);
return getAction('AskNameAndPhone');
if (props.confirm === 'yes') {
await context.sendText(
`Thank you for your help, your personal data was stored correctly.`
);
} else if (props.confirm === 'name') {
deleteField(context, ['name', 'confirm']);
await context.sendText('Please retype your name:');
return prompt('name');
} else if (props.confirm === 'phone') {
deleteField(context, ['phone', 'confirm']);
await context.sendText('Please retype your phone again (10 digits):');
return prompt('phone');
} else if (props.confirm === 'both') {
deleteField(context, ['confirm', 'name', 'phone']);
return AskNameAndPhone;
}
}
});
);

module.exports = run(function App() {
return getAction('AskNameAndPhone');
return AskNameAndPhone;
});
Loading