I'm building a chatbot which shows a school meal menu for students. I've already received a permission from facebook, so my bot is allowed to send a message to users at anytime. But the problem is how to make a subscription feature.
I use Node.js and MongoDB. My current method of enabling subscription is really inefficient. Here's some parts of my codes:
var express = require("express");
var request = require("request");
var bodyParser = require("body-parser");
var isTimer = false;
var schedule = require('node-schedule');
var morningTime;
var lunchTime;
var dinnerTime;
var snackTime;
var mongo = require("mongodb");
var MongoClient = require("mongodb").MongoClient;
var url = 'mongodb://greenblatt:kinesis1717!@hafscluster-shard-00-00-cyacf.mongodb.net:27017,hafscluster-shard-00-01-cyacf.mongodb.net:27017,hafscluster-shard-00-02-cyacf.mongodb.net:27017/mbot?ssl=true&replicaSet=hafsCluster-shard-0&authSource=admin'
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.listen((process.env.PORT || 5000));
app.get("/", function (req, res) {
res.send("Deployed!");
});
app.get("/webhook", function (req, res) {
if (req.query["hub.verify_token"] === process.env.VERIFICATION_TOKEN) {
console.log("Verified webhook");
res.status(200).send(req.query["hub.challenge"]);
} else {
console.error("Verification failed. The tokens do not match.");
res.sendStatus(403);
}
});
app.post('/webhook', function (req, res) {
var data = req.body;
if (data.object === 'page') {
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
entry.messaging.forEach(function(event) {
if (event.message) {
receivedMessage(event);
} else if(event.postback){
receivedPostback(event);
}
else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
});
function setSubscribe(senderID){
if(isTimer){
} else{
MongoClient.connect(url, function(err, db){
if (err) throw err;
db.collection('users', function(err, collection) {
collection.find({"psid": senderID}).toArray(function(err, result) {
if(!result[0]){
db.collection("users").insertOne({psid: senderID,isSub:false}, function(err, res) {
if (err) throw err;
});
}
else if(result[0].isSub === true){
morningTime = schedule.scheduleJob('00 08 * * *', function(){
sendGenericMessage(senderID, "pmorning");
});
lunchTime = schedule.scheduleJob('00 13 * * *', function(){
sendGenericMessage(senderID, "plunch");
});
dinnerTime = schedule.scheduleJob('00 18 * * *', function(){
sendGenericMessage(senderID, "pdinner");
});
snackTime = schedule.scheduleJob('20 20 * * *', function(){
sendSnack(senderID);
});
}
});
db.close();
});
})
isTimer = true;
}
}
function receivedMessage(event) {
var senderID = event.sender.id;
psid = senderID;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
var messageText = message.text;
var messageAttachments = message.attachments;
setSubscribe(senderID);
if (messageText) {
switch (messageText) {
case 'Check':
sendTextMessage(senderID, "psid: " + psid);
sendTextMessage(senderID, "isTimer: " + isTimer);
break;
But whenever I revise my app.js(the code above) and push it, node-schedule stops and subscription doesn't work. 1. So how should I solve it? 2. Who should I check if the user subscribed? (there's a field 'isSub' in db)
via brain_team
No comments:
Post a Comment