forked from Tuxedo21/cmuNodeFbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.ts
More file actions
104 lines (96 loc) · 3.21 KB
/
interactive.ts
File metadata and controls
104 lines (96 loc) · 3.21 KB
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
import readline = require("readline");
import handlers = require("./handlers");
import open = require("open");
let instance: Interactive = null;
class Interactive {
currentVolunteer: number;
interface: readline.ReadLine;
buttons: Array<any>; // TODO: create type definitions for messenger-bot
constructor() {
if (!instance) {
instance = this;
}
this.currentVolunteer = (process.env.FBID || 1);
this.interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
this.setPrompt();
this.buttons = [];
return instance;
}
sendMessage(id, message) {
let text = message.text;
if (!text && message.attachment && message.attachment.payload) {
text = message.attachment.payload.text;
if (message.attachment.payload.template_type === "button") {
const buttons = message.attachment.payload.buttons;
let i = this.buttons.length;
buttons.forEach((b) => text = text + `\n\t${i++}. ${b.title}`);
this.buttons.push(...buttons);
text = text + "\n\tYou can choose a button with the command '/but <button number>'.";
}
}
console.log(`to: (${id}) < ${text}`);
this.interface.prompt();
}
setPrompt() {
this.interface.setPrompt(`(${this.currentVolunteer}) > `);
}
startListening() {
console.log(`Welcome to the interactive test mode.
You are currently impersonating volunteer ${this.currentVolunteer}.
You can switch volunteer ids with the command '/vol <id>'.`);
this.interface.on("line", (line) => {
const values = line.trim().split(" ");
const reply = this.sendMessage.bind(this, this.currentVolunteer);
if (values.length === 2 && values[0] === "/vol") {
this.currentVolunteer = parseInt(values[1], 10);
this.setPrompt();
this.interface.prompt();
} else if (values.length === 2 && values[0] === "/but") {
const buttonIndex = parseInt(values[1], 10);
if (!(buttonIndex in this.buttons)) {
console.log(`Button ${buttonIndex} not found.`);
return this.interface.prompt();
}
if (this.buttons[buttonIndex].type === "postback") {
const payload = {
sender: {
id: this.currentVolunteer,
profile: {
first_name: "John",
last_name: "Smith"
}
},
postback: {payload: this.buttons[buttonIndex].payload}
};
handlers.dispatchPostback(payload, reply);
} else if (this.buttons[buttonIndex].type === "web_url") {
open(this.buttons[buttonIndex].url);
console.log(`\topening url: ${this.buttons[buttonIndex].url}`);
this.interface.prompt();
}
} else {
const payload = {
sender: {
id: this.currentVolunteer,
profile: {
first_name: "John",
last_name: "Smith"
}
},
message: {
text: line.trim()
}
};
handlers.dispatchMessage(payload, reply);
}
}).on("close", () => {
console.log("Bot shutdown.");
process.exit(0);
});
this.interface.prompt();
}
}
module.exports.instance = new Interactive();