forked from Tuxedo21/cmuNodeFbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.ts
More file actions
114 lines (99 loc) · 4.05 KB
/
bot.ts
File metadata and controls
114 lines (99 loc) · 4.05 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
105
106
107
108
109
110
111
112
113
114
import * as handlers from "./handlers";
import * as FBTypes from "facebook-sendapi-types";
import fb from "facebook-send-api";
import * as moment from "moment";
import * as rpErrors from "request-promise/errors";
import * as logger from "winston";
import {DATE_FORMAT} from "./config";
import {Admin} from "./models/admin";
import {Volunteer} from "./models/volunteer";
export interface WebhookPayloadFields extends FBTypes.WebhookPayloadFields {
sender: {
id: string;
profile?: FBTypes.FacebookUser;
admin?: Admin,
volunteer?: Volunteer
};
}
class Bot {
FBPlatform: fb;
constructor(token: string) {
this.FBPlatform = new fb(token);
}
sendMessage(fbid: string | number, message: FBTypes.MessengerMessage) {
if (typeof fbid === "number") {
fbid = String(fbid);
}
logger.info("sending message", {fbid, message});
return this.FBPlatform.sendMessageToFB(fbid, message)
.catch((error) => {
logger.error(
"Error while trying to send Facebook message via Send API.",
error.response.body.error
);
});
}
getProfile(fbid: string) {
return this.FBPlatform.getUserProfile(fbid)
.catch((err) => {
logger.error(err);
});
}
handleEvent(payload: FBTypes.WebhookPayload) {
new Volunteer({fbid: payload.sender.id}).fetch()
.then((vol: Volunteer) => {
if (vol && vol.ignoring) {
logger.info(`ignoring message from ${payload.sender.id}`);
return;
} else {
return this.getProfile((payload.message && payload.message.is_echo) ? payload.recipient.id : payload.sender.id)
.then((profile): any => {
if (profile) {
(payload as WebhookPayloadFields).sender.profile = profile;
} else {
(payload as WebhookPayloadFields).sender.profile = null;
}
if (payload.message && payload.message.is_echo) {
return this.handleEcho(payload);
} else if (payload.message && !payload.message.text) {
return this.sendMessage(
payload.sender.id,
{text: "Sorry, I can only handle text messages right now"}
);
} else if (payload.postback) {
return this.handlePostback(payload);
} else {
return this.handleMessage(payload);
}
});
}
});
}
handleEcho(payload: FBTypes.MessengerPayload) {
logger.info("echo received:", payload);
// keep track of last time we sent anything to this user
return new Volunteer({ fbid: payload.recipient.id })
.save(
{ last_messaged: moment().format(DATE_FORMAT) },
{ patch: true, require: false }
);
}
handleMessage(payload: FBTypes.WebhookPayloadFields) {
logger.info("message received:", payload);
// Keep track of last time we received anything from this user
new Volunteer({ fbid: payload.sender.id })
.save(
{ last_response: moment().format(DATE_FORMAT) },
{ patch: true, require: false }
).then(() => {
const reply = this.sendMessage.bind(this, payload.sender.id);
handlers.dispatchMessage(payload, reply);
});
}
handlePostback(payload: WebhookPayloadFields) {
const reply = this.sendMessage.bind(this, payload.sender.id);
handlers.dispatchPostback(payload, reply);
}
}
export type ReplyFunc = (message: FBTypes.MessengerMessage) => Promise<FBTypes.MessengerResponse>;
export const bot = new Bot(process.env.PAGE_ACCESS_TOKEN);