wjc0930

Tutorial code for making Facebook chatbot.

1 +node_modules
1 ++2019/5/25
2 ++mother project : LINEBOT
3 ++new project : 페이스북 메신저 기반 컴공/소융 강의평가 봇
4 ++
1 +var express = require("express");
2 +var request = require("request");
3 +var bodyParser = require("body-parser");
4 +
5 +var app = express();
6 +app.use(bodyParser.urlencoded({extended: false}));
7 +app.use(bodyParser.json());
8 +app.listen((process.env.PORT || 5000));
9 +
10 +// Server index page
11 +app.get("/", function (req, res) {
12 + res.send("Deployed!");
13 +});
14 +
15 +// Facebook Webhook
16 +// Used for verification
17 +app.get("/webhook", function (req, res) {
18 + if (req.query["hub.verify_token"] === "process.env.VERIFICATION_TOKEN") {
19 + console.log("Verified webhook");
20 + res.status(200).send(req.query["hub.challenge"]);
21 + } else {
22 + console.error("Verification failed. The tokens do not match.");
23 + res.sendStatus(403);
24 + }
25 +});
26 +
27 +// All callbacks for Messenger will be POST-ed here
28 +app.post("/webhook", function (req, res) {
29 + // Make sure this is a page subscription
30 + if (req.body.object == "page") {
31 + // Iterate over each entry
32 + // There may be multiple entries if batched
33 + req.body.entry.forEach(function(entry) {
34 + // Iterate over each messaging event
35 + entry.messaging.forEach(function(event) {
36 + if (event.postback) {
37 + processPostback(event);
38 + }
39 + });
40 + });
41 +
42 + res.sendStatus(200);
43 + }
44 +});
45 +
46 +function processPostback(event) {
47 + var senderId = event.sender.id;
48 + var payload = event.postback.payload;
49 +
50 + if (payload === "Greeting") {
51 + // Get user's first name from the User Profile API
52 + // and include it in the greeting
53 + request({
54 + url: "https://graph.facebook.com/v2.6/" + senderId,
55 + qs: {
56 + access_token: process.env.PAGE_ACCESS_TOKEN,
57 + fields: "first_name"
58 + },
59 + method: "GET"
60 + }, function(error, response, body) {
61 + var greeting = "";
62 + if (error) {
63 + console.log("Error getting user's name: " + error);
64 + } else {
65 + var bodyObj = JSON.parse(body);
66 + name = bodyObj.first_name;
67 + greeting = "Hi " + name + ". ";
68 + }
69 + var message = greeting + "My name is SP Movie Bot. I can tell you various details regarding movies. What movie would you like to know about?";
70 + sendMessage(senderId, {text: message});
71 + });
72 + }
73 +}
74 +
75 +// sends message to user
76 +function sendMessage(recipientId, message) {
77 + request({
78 + url: "https://graph.facebook.com/v2.6/me/messages",
79 + qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
80 + method: "POST",
81 + json: {
82 + recipient: {id: recipientId},
83 + message: message,
84 + }
85 + }, function(error, response, body) {
86 + if (error) {
87 + console.log("Error sending message: " + response.error);
88 + }
89 + });
90 +}
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "spbot",
3 + "version": "1.0.0",
4 + "description": "SPBot Server",
5 + "main": "app.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1",
8 + "start": "node app.js"
9 + },
10 + "author": "WonJun Choi",
11 + "license": "ISC",
12 + "dependencies": {
13 + "body-parser": "^1.19.0",
14 + "express": "^4.17.0",
15 + "mongoose": "^5.5.11",
16 + "request": "^2.88.0"
17 + }
18 +}