forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook-channeldata.js
120 lines (103 loc) · 3.86 KB
/
facebook-channeldata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var util = require('util');
// AirlineCheckin
function AirlineCheckin(intro_message, locale, pnr_number, checkin_url, flight_info) {
checkParam(intro_message, 'intro_message');
checkParam(locale, 'locale');
checkParam(pnr_number, 'pnr_number');
checkParam(checkin_url, 'checkin_url');
checkParam(flight_info, 'flight_info');
checkType(flight_info, Array, 'flight_info');
flight_info.forEach(function (info, ix) { checkType(info, FlightInfo, 'flight_info[' + ix + ']'); });
this.type = 'template';
this.payload = {
template_type: 'airline_checkin',
intro_message: intro_message,
locale: locale,
pnr_number: pnr_number,
checkin_url: checkin_url,
flight_info: flight_info
};
}
AirlineCheckin.prototype.toString = function () {
return util.format(
'%s. Confirmation Number: %s. %s. Check in @ %s',
this.payload.intro_message,
this.payload.pnr_number,
this.payload.flight_info.map(function (info) { return info.toString(); }).join('; '),
this.payload.checkin_url);
};
// FlightInfo
function FlightInfo(flight_number, departure_airport, arrival_airport, flight_schedule) {
checkParam(flight_number, 'flight_number');
checkParam(departure_airport, 'departure_airport');
checkParam(arrival_airport, 'arrival_airport');
checkParam(flight_schedule, 'flight_schedule');
checkType(departure_airport, Airport, 'departure_airport');
checkType(arrival_airport, Airport, 'arrival_airport');
checkType(flight_schedule, FlightSchedule, 'flight_schedule');
this.flight_number = flight_number;
this.departure_airport = departure_airport;
this.arrival_airport = arrival_airport;
this.flight_schedule = flight_schedule;
}
FlightInfo.prototype.toString = function () {
return util.format(
'Flight %s from %s (%s) to %s (%s) departing at %s from gate %s at terminal %s and arriving at %s to gate %s at terminal %s. Boarding time is %s',
this.flight_number,
this.departure_airport.city,
this.departure_airport.airport_code,
this.arrival_airport.city,
this.arrival_airport.airport_code,
this.flight_schedule.departure_time,
this.departure_airport.gate,
this.departure_airport.terminal,
this.flight_schedule.arrival_time,
this.arrival_airport.gate,
this.arrival_airport.terminal,
this.flight_schedule.boarding_time
);
};
// Airport
function Airport(airport_code, city, terminal, gate) {
checkParam(airport_code, 'airport_code');
checkParam(city, 'city');
checkParam(terminal, 'terminal');
checkParam(gate, 'gate');
this.airport_code = airport_code;
this.city = city;
this.terminal = terminal;
this.gate = gate;
}
// FlightSchedule
function FlightSchedule(boarding_time, departure_time, arrival_time) {
checkParam(boarding_time, 'boarding_time');
checkParam(departure_time, 'departure_time');
checkParam(arrival_time, 'arrival_time');
checkType(boarding_time, Date, 'boarding_time');
checkType(departure_time, Date, 'departure_time');
checkType(arrival_time, Date, 'arrival_time');
this.boarding_time = formatDate(boarding_time);
this.departure_time = formatDate(departure_time);
this.arrival_time = formatDate(arrival_time);
}
// helpers
function checkParam(param, name) {
if (param === undefined) {
throw new Error('Mising Parameter: \'' + name + '\'');
}
}
function checkType(param, expectedType, name) {
if (!(param instanceof expectedType)) {
throw new Error('Expected type \'' + expectedType.name + '\' for parameter \'' + name + '\'');
}
}
function formatDate(date) {
return date.toISOString().split('.')[0];
}
// exports
module.exports = {
AirlineCheckin: AirlineCheckin,
FlightInfo: FlightInfo,
FlightSchedule: FlightSchedule,
Airport: Airport
};