本文整理匯總了TypeScript中machina.BehavioralFsm.extend方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript BehavioralFsm.extend方法的具體用法?TypeScript BehavioralFsm.extend怎麽用?TypeScript BehavioralFsm.extend使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類machina.BehavioralFsm
的用法示例。
在下文中一共展示了BehavioralFsm.extend方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: function
export const ReplaceBeaconTaskFsm = machina.BehavioralFsm.extend({
namespace: "place_beacons",
initialState: "supply",
states: {
supply: {
_onEnter: function(task: Task) {
task.context = {slot: task.instructionParams.slot};
task.deployment().fetch()
.then((deployment: Deployment) => {
let text = `One of our beacons needs to be replaced because it isn't working. Please go to the Supply Station (${deployment.supplyStation}). Tell me when you are 'there'.`;
return bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["there"])
);
});
},
"msg:there": "pickup",
},
pickup: {
_onEnter: function(task) {
let text = "Great! To open the lockbox, type the code 020217, then #, then turn the switch. Please take a replacement beacon. Please close and lock the box. Let me know when you are 'ready'.";
bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["ready"])
);
},
"msg:ready": "go",
},
go: {
_onEnter: function(task: Task) {
const url = `${config.BASE_URL}/map/?advanced&hidden&beacon=${task.instructionParams.slot}`;
const text = "Please go to the location marked on the map below. Tell me when you are 'there'.";
const buttons = [{
type: "web_url",
title: "Open Map",
url: url,
webview_height_ratio: "tall",
messenger_extensions: true,
}] as Array<FBTypes.MessengerButton>;
bot.FBPlatform.sendButtonMessage(task.volunteerFbid.toString(), text, buttons);
},
"msg:there": "old_beacon",
},
old_beacon: {
_onEnter: function(task) {
let text = "Is there an existing beacon at this location?";
bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["yes", "no"])
);
},
"msg:yes": function(task) {
task.context.return = true;
this.transition(this, "which");
},
"msg:no": function(task) {
task.context.return = false;
this.transition(this, "which");
}
},
which: {
_onEnter: function(task) {
bot.sendMessage(
task.volunteerFbid,
{text: `What is the number on the back of the replacement beacon?`}
);
},
number: function(task, id) {
// TODO: double check if it seems like that beacon doesn't exist or is already placed.
task.context.currentBeacon = id;
task.context.return ? this.transition(task, "replace_return") : this.transition(task, "replace");
}
},
replace_return: {
_onEnter: function(task) {
// record beacon status as broken and in possession of volunteer
let text = "Please take down the old beacon and put the new one in it's place. Then return the old beacon to the pickup location. Tell me when you are 'done'.";
bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["done"])
);
},
"msg:done": function(task) {
task.saveScore(40).then(() => this.handle(task, "complete"));
},
},
replace: {
_onEnter: function(task) {
let text = "Please put the beacon on the wall (you can double check the location using the map above). Tell me when you are 'done'.";
bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["done"])
);
// record beacon's status as MIA, look for it.
},
"msg:done": function(task: Task) {
const updateSlot = new BeaconSlot({id: task.context.slot})
.save({beacon_id: task.context.currentBeacon}, {patch: true});
const updateScore = task.saveScore(40);
//.........這裏部分代碼省略.........
示例2: Task
export const FingerprintTaskFsm = machina.BehavioralFsm.extend({
namespace: "fingerprint",
initialState: "ios_check",
states: {
ios_check: {
_onEnter: function(task: Task) {
return task.assignedVolunteer().fetch()
.then(vol => {
if (vol.hasIOS !== null) {
return this.transition(task, "download_app");
}
const text = "Some of our tasks require the use of a helper app to collect Bluetooth data. Do you have an iOS device?";
return vol.sendMessage(msgUtil.quickReplyMessage(text, ["yes", "no"]));
});
},
"msg:yes": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({"has_ios": true}))
.then(() => this.transition(task, "download_app"));
},
"msg:no": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({"has_ios": false}))
.then((vol: Volunteer) => {
const text = "Unforuntately, we don't have the helper app available for other platforms yet. We will contact you when we do!";
return vol.sendMessage({text})
.then(() => vol.unassignTask());
}).then(([vol, oldTask]) => getAndAssignVolTask(vol));
},
},
download_app: {
_onEnter: function(task: Task) {
return task.assignedVolunteer().fetch()
.then(vol => {
if (vol.appState === "installed") {
return this.transition(task, "how_many");
}
const text = "You will need to download the app 'LuzDeploy Data Sampler'. Press the link below to open the App Store.";
const url = "http://appstore.com/luzdeploydatasampler";
const buttons = [{
"type": "web_url",
"title": "Download App",
"url": url,
"webview_height_ratio": "compact",
}] as Array<FBTypes.MessengerButton>;
return bot.FBPlatform.sendButtonMessage(
vol.fbid.toString(),
text,
buttons
).then(() => {
const text = "Then come back to this conversation and let me know when you are 'done'!";
return vol.sendMessage(msgUtil.quickReplyMessage(text, ["done"]));
});
});
},
"msg:done": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({app_state: "installed"}))
.then(() => this.transition(task, "how_many"));
}
},
how_many: {
_onEnter: function(task: Task) {
let text = "We need you to help us sample beacon data in the building. How many samples would you like to collect (one sample takes about 30 seconds on average)?";
bot.sendMessage(task.volunteerFbid, msgUtil.quickReplyMessage(text, ['5', '10', '15']));
},
number: function(task: Task, n) {
task.context = {numSamples: n};
this.transition(task, "load_points");
}
},
load_points: {
_onEnter: function(task: Task) {
let self = this;
FingerprintPoint.getPointsForSampling(
task.deploymentId,
task.context.numSamples
).then(function(points) {
task.context.points = points.map((p: FingerprintPoint) => ({
floor: p.floor,
lat: p.latitude,
long: p.longitude
}));
}).then(function() {
self.transition(task, "goto");
});
}
},
goto: {
_onEnter: function(task: Task) {
const text = "Please open the LuzDeploy app below and follow the instructions. Let me know when you are 'done'!";
const locations = task.context.points.map(
p => `${p.floor},${p.lat},${p.long}`
).join(";");
const url = `${config.BASE_URL}/redirect.html?type=datasampler&major=65535&locations=${locations}&wid=${task.volunteerFbid}&next=${config.THREAD_URI}&base=${config.BASE_URL}`
const buttons = [
{
"type": "web_url",
"title": "Open LuzDeploy",
"url": url,
//.........這裏部分代碼省略.........
示例3: function
export const SetUsernameFsm = machina.BehavioralFsm.extend({
namespace: "set_username",
initialState: "get_username",
states: {
get_username: {
_onEnter: function(user) {
let self = this;
bot.FBPlatform.createButtonMessage(user.fbid)
.text(`Hi, ${user.firstName}! In the spirit of friendly competition, we have a leaderboard that shows a score based on how many tasks you have done. Your nickname been randomly selected as "${user.username}". Would you like to change it?`)
.postbackButton("Change Nickname", "")
.postbackButton("Leave As Is", "")
.send();
},
"change": "get_input",
"leave": "completed"
},
get_input: {
_onEnter: function(user) {
const text = "Ok, what would you like it to be?";
bot.FBPlatform.sendTextMessage
},
"msg:done": function(task) {
this.handle(task, "complete");
},
"webhook:done": function(task) {
this.handle(task, "complete");
}
},
}
});
示例4: Task
export const SweepTaskFsm = machina.BehavioralFsm.extend({
namespace: "sweep_edge",
initialState: "ios_check",
states: {
ios_check: {
_onEnter: function(task: Task) {
return task.assignedVolunteer().fetch()
.then(vol => {
if (vol.hasIOS !== null) {
return this.transition(task, "download_app");
}
const text = "Some of our tasks require the use of a helper app to collect Bluetooth data. Do you have an iOS device?";
return vol.sendMessage(msgUtil.quickReplyMessage(text, ["yes", "no"]));
});
},
"msg:yes": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({"has_ios": true}))
.then(() => this.transition(task, "download_app"));
},
"msg:no": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({"has_ios": false}))
.then((vol: Volunteer) => {
const text = "Unforuntately, we don't have the helper app available for other platforms yet. We will contact you when we do!";
return vol.sendMessage({text})
.then(() => vol.unassignTask());
}).then(([vol, oldTask]) => getAndAssignVolTask(vol));
},
},
download_app: {
_onEnter: function(task: Task) {
return task.assignedVolunteer().fetch()
.then(vol => {
if (vol.appState === "installed") {
return this.transition(task, "goto");
}
const text = "You will need to download the app 'LuzDeploy Data Sampler'. Press the link below to open the App Store.";
const url = "http://appstore.com/luzdeploydatasampler";
const buttons = [{
"type": "web_url",
"title": "Download App",
"url": url,
"webview_height_ratio": "compact",
}] as Array<FBTypes.MessengerButton>;
return bot.FBPlatform.sendButtonMessage(
vol.fbid.toString(),
text,
buttons
).then(() => {
const text = "Then come back to this conversation and let me know when you are 'done'!";
return vol.sendMessage(msgUtil.quickReplyMessage(text, ["done"]));
});
});
},
"msg:done": function(task: Task) {
return task.assignedVolunteer().fetch()
.then((vol: Volunteer) => vol.save({app_state: "installed"}))
.then(() => this.transition(task, "goto"));
}
},
goto: {
_onEnter: function(task: Task) {
const text = "We need you to help us check which beacons are not working in the building. Please open the LuzDeploy app below and follow the instructions. Let me know when you are 'done'!";
const params = task.instructionParams;
const url = `${config.BASE_URL}/redirect.html?type=beaconsweeper&major=65535&edge=${params.edge}&beaconlist=${params.beacons}&wid=${task.volunteerFbid}&start=${params.start}&end=${params.end}&next=${config.THREAD_URI}&base=${config.BASE_URL}`;
const buttons = [{
type: "web_url",
title: "Open LuzDeploy",
url: url,
webview_height_ratio: "compact",
messenger_extensions: true,
}] as Array<FBTypes.MessengerButton>;
bot.FBPlatform.sendButtonMessage(task.volunteerFbid.toString(), text, buttons);
},
"msg:done": function(task: Task) {
task.saveScore(40, 0.4).then(() => this.handle(task, "complete"));
},
"webhook:done": function(task) {
task.saveScore(40, 0.4).then(() => this.handle(task, "complete"));
}
},
},
getNewTask: function(vol: Volunteer): Promise<Task> {
if (vol.hasIOS === false) {
return null;
}
return Beacon.collection<Beacon>()
.query(qb => {
qb.where({deployment_id: vol.deploymentId})
.whereNotNull("slot");
}).fetch({withRelated: "slot"})
// Temporary TODO(cgleason): remove this later
.then(beacons => beacons.filter(
(b: Beacon) => (b.related<BeaconSlot>("slot") as BeaconSlot).building === "GHC"
)).then(beacons => _.groupBy(beacons,
(b: Beacon) => (b.related<BeaconSlot>("slot") as BeaconSlot).edge)
).then(edges => {
const minDate = (bs: Beacon[]) => _.min(bs.map(b => b.lastSwept)) || null;
const edge = _(edges).keys().sortBy([
//.........這裏部分代碼省略.........
示例5: BeaconSlot
export const PlaceBeaconsTaskFsm = machina.BehavioralFsm.extend({
namespace: "place_beacons",
initialState: "supply",
beaconToReturn: function(task) {
task.context.toReturn.push(task.context.currentBeaconNumber);
task.context.currentBeacon = null;
task.context.numBeacons--;
task.context.slots.pop(1);
if (task.context.numBeacons === 0) {
this.transition(task, "return");
return;
} else {
return bot.sendMessage(
task.volunteerFbid,
{text: "Later, please return that beacon to the supply station. For now we will plave another beacon."}
).then(() => this.transition(task, "which"));
}
},
states: {
supply: {
_onEnter: function(task: Task) {
return task.deployment().fetch()
.then((deployment: Deployment) => {
let text = `In this task you will place beacons in the environment that will be used by people with visual impairments to navigate. Please go to the Supply Station (${deployment.supplyStation}). Tell me when you are 'there'.`;
return bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["there"])
);
});
},
"msg:there": "pickup",
},
pickup: {
_onEnter: function(task) {
return task.deployment().fetch()
.then((deployment: Deployment) => {
let text = `Great! ${deployment.supplyStationInstructions} Tell me how many you took (you can press a button or type a number).`;
return bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage(text, ["1", "3", "5", "10"])
);
});
},
number: function(task: Task, n) {
task.context = {
initialBeacons: n,
numBeacons: n,
currentSlot: null,
currentBeacon: null,
toReturn: [],
score: 30 + n * 10
};
let self = this;
BeaconSlot.getNSlots(n, task.deploymentId)
.then((slots) => {
if (slots.length === 0) {
task.context.numBeacons = 0;
let text = `I could not find any place that needs beacons. Please return all beacons.`;
return bot.sendMessage(task.volunteerFbid, {text: text})
.then(() => this.handle(task , "reject"))
} else if (slots.length !== n) {
// TODO: handle case when slots.length == 0
task.context.numBeacons = slots.length;
let text = `I could only find ${slots.length} places needing beacons. Please return any excess beacons.`;
return bot.sendMessage(task.volunteerFbid, {text: text})
.then(() => self.transition(task, "go"));
}
task.context.slots = slots.map(s => s.id);
return slots.invokeThen("save", {in_progress: true})
.then(() => self.transition(task, "go"));
});
},
},
go: {
_onEnter: function(task) {
task.context.currentSlot = task.context.slots.pop(1);
return task.deployment().fetch()
.then((deployment: Deployment) => {
const url = `${config.BASE_URL}/map/?advanced&hidden&map=${deployment.mapFilename}&beacon=${task.context.currentSlot}`
const text = `You have ${task.context.numBeacons} beacons to place. Please go to the location marked on the map below.`;
const buttons = [
{
"type": "web_url",
"title": "Open Map",
"url": url,
"webview_height_ratio": "tall",
"messenger_extensions": true,
}
] as Array<FBTypes.MessengerButton>;
return bot.FBPlatform.sendButtonMessage(task.volunteerFbid, text, buttons)
}).then(() => {
return bot.sendMessage(
task.volunteerFbid,
msgUtil.quickReplyMessage("Tell me when you are 'there'!", ["there"])
);
});
},
"msg:there": "confirm_empty",
},
confirm_empty: {
//.........這裏部分代碼省略.........