diff --git a/controller/message.js b/controller/message.js index 8424d7b..003fa1a 100644 --- a/controller/message.js +++ b/controller/message.js @@ -1,22 +1,46 @@ -function getAllMessages(request,response){ +const {Message,User} = require('../database/mongodb/index').Models; +/* + Gets all the messages that sent along the bot +*/ +async function getAllMessages(request,response){ + const messages = await new Message().getAll(); + return response.status(200).json({ + data:messages, + errors:[] + }); } -function getMessage(request,response) { +/* + Gets specific Message +*/ +async function getMessage(request,response) { - + const id = request.params.id; + const message = await Message.getById(id); + return response.status(200).json({ + data:message, + errors:[] + }); } -function deleteMessage(request,response) { +/* + Deletes specific Message +*/ +async function deleteMessage(request,response) { - + const id = request.params.id; + const message = await Message.softDeleteById(id); + return response.status(200).json({ + data:message, + errors:[] + }); } - module.exports = { getAllMessages, getMessage, diff --git a/database/index.js b/database/index.js index 9bb9dd0..e3ae9d2 100644 --- a/database/index.js +++ b/database/index.js @@ -1,15 +1,17 @@ const {Redis} = require('./redis/index.js'); let redisController = null; -//Router Controller -let start = (config)=>{ +let redis = async ()=>{ - redisController = Redis; + if(redisController) { + return redisController; + } + redisController = new Redis(); + await redisController.init(); + return redisController; } module.exports = { - start, - redis:redisController, - + Cacher:redis, } \ No newline at end of file diff --git a/database/mongodb/models/message.model.js b/database/mongodb/models/message.model.js index 7e9b9aa..19a4e82 100644 --- a/database/mongodb/models/message.model.js +++ b/database/mongodb/models/message.model.js @@ -4,7 +4,7 @@ const Schema = mongoose.Schema const Types = Schema.Types const modelName = 'Message' -const collectionName = 'messages'; +const collection = 'messages'; const schema = new Schema({ @@ -16,10 +16,6 @@ const schema = new Schema({ type:String, default:null }, - timestamp:{ - type:Date, - default:null - }, recipientId: { type:String, default:null @@ -121,6 +117,27 @@ schema.methods.getAll = function(){ }; +schema.methods.getById = function(id){ + + const model = this.model(modelName); + + let query = { + _id:id + }; + + const fields = { + __v:0, + _data_lifecycle:0, + }; + + return model + .find(query) + .populate('User','-__v -_data_lifecycle') + .select(fields) + .exec(); + +}; + const model = mongoose.model(modelName, schema, collection) module.exports = { diff --git a/database/mongodb/models/user.model.js b/database/mongodb/models/user.model.js index df02d88..7cdb85e 100644 --- a/database/mongodb/models/user.model.js +++ b/database/mongodb/models/user.model.js @@ -55,7 +55,7 @@ const schema = new Schema({ }); -schema.methods.getUserByObjectId = function(id){ +schema.methods.getByObjectId = function(id){ return this.model(modelName).findOne({ _id:id @@ -63,7 +63,7 @@ schema.methods.getUserByObjectId = function(id){ } -schema.methods.getUserById = function(id){ +schema.methods.getById = function(id){ return this.model(modelName).findOne({ id diff --git a/database/redis/index.js b/database/redis/index.js index 55f13f9..73b9c36 100644 --- a/database/redis/index.js +++ b/database/redis/index.js @@ -19,8 +19,8 @@ class Redis { try{ - const status = await this.createClient(0),data = await this.createClient(1),message = await this.createClient(2); - this.addModel('user',new models.user(status,data,message)); + const status = await this.createClient(0),data = await this.createClient(1); + this._models['user'] = new models.user(status,data); }catch(error){ @@ -72,9 +72,6 @@ class Redis { } -const _redis = new Redis(); -_redis.init(); - module.exports = { - Redis:_redis + Redis }; diff --git a/database/redis/models/user.model.js b/database/redis/models/user.model.js index d596e31..8f3c705 100644 --- a/database/redis/models/user.model.js +++ b/database/redis/models/user.model.js @@ -1,13 +1,13 @@ class Model { - constructor(statusClient,dataClient,messageClient){ - - this.status = null; - this.user = null; + constructor(statusClient,dataClient){ this.statusClient = statusClient; this.dataClient = dataClient; + this.status = null; + this.user = null; + this.expireTime = 60*60*2;//2h } @@ -26,27 +26,21 @@ class Model { this.user.birthday = value; } - get user(user){ - - const userString = JSON.stringify(user); - return this.dataClient.setAsync(user.id,userString,'EX',this.expireTime); + async setId(value){ + if(value){ + this.user = await this.dataClient.getAsync(value); + this.status = await this.statusClient.getAsync(value); + } + } - set status(user){ - - const userString = JSON.stringify(user); - return this.dataClient.setAsync(user.id,userString,'EX',this.expireTime); - // await this.emailVerificationClient.delAsync(key); - - } - - get status(){ - return this.messages; - } - set status(value){ - this.status = value; - } + // get status(){ + // return this.messages; + // } + // set status(value){ + // this.status = value; + // } } diff --git a/index.js b/index.js index c287c41..6a68e77 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict'; +require('dotenv').config(); const router = require('./router'); try{ diff --git a/middlewares/cache.js b/middlewares/cache.js new file mode 100644 index 0000000..f088c47 --- /dev/null +++ b/middlewares/cache.js @@ -0,0 +1,7 @@ +function updateCache(request,response){ + +} + +module.exports = { + updateCache +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 40d7366..48db2b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3672,6 +3672,35 @@ "util-deprecate": "~1.0.1" } }, + "redis": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-3.1.0.tgz", + "integrity": "sha512-//lAOcEtNIKk2ekZibes5oyWKYUVWMvMB71lyD/hS9KRePNkB7AU3nXGkArX6uDKEb2N23EyJBthAv6pagD0uw==", + "requires": { + "denque": "^1.5.0", + "redis-commands": "^1.7.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" + } + }, + "redis-commands": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", + "integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, "regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", diff --git a/package.json b/package.json index 4f9d3d7..3202c03 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A Simple Messenger Bot", "main": "index.js", "scripts": { + "start": "node index.js", "test": "", "dev": "nodemon index.js" }, @@ -22,6 +23,7 @@ "express": "^4.17.1", "jest": "^26.6.3", "mongoose": "^5.12.3", + "redis": "^3.1.0", "sequelize": "^6.6.2" } } diff --git a/requests/config.js b/requests/config.js new file mode 100644 index 0000000..ad49419 --- /dev/null +++ b/requests/config.js @@ -0,0 +1,10 @@ +module.exports = { + config:{ + baseURL: "https://graph.facebook.com/v7.0", + headers: { + "Content-Type": "application/json", + }, + responseType: "json", + }, + access_token:process.env.MESSENGER_BOT_ACCESS_TOKEN +}; \ No newline at end of file diff --git a/requests/index.js b/requests/index.js new file mode 100644 index 0000000..30c8470 --- /dev/null +++ b/requests/index.js @@ -0,0 +1,62 @@ +const axios = require("axios"); +const { config, access_token } = require("./config"); + +const client = axios.create(config); + +function markSeen(recipient) { + const path = "/me/messages", + data = { + recipient: { id: recipient }, + sender_action: "mark_seen", + }, + options = { + params: { access_token }, + }; + + return client.post(path, data, options); +} + +function setTyping(recipient, state) { + const path = "/me/messages", + data = { + recipient: { id: recipient }, + sender_action: state ? "typing_on" : "typing_off", + }, + options = { + params: { access_token }, + }; + + return client.post(path, data, options); +} + +function sendMessage(recipient, message, quckReplies) { + const path = "/me/messages", + data = { + recipient: { id: recipient }, + message: { + text:message, + quick_replies:quckReplies?quckReplies:undefined, + }, + }, + options = { + params: { access_token }, + }; + + console.log(data) + + return client.post(path, data, options); +} + +async function sendMessageWithSeenAndTyping(recipient, message, quckReplies) { + await markSeen(recipient); + await setTyping(recipient,true); + await sendMessage(recipient,message,quckReplies); + await setTyping(recipient,false); +} + +module.exports = { + markSeen, + setTyping, + sendMessage, + sendMessageWithSeenAndTyping +}; diff --git a/responser/index.js b/responser/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/responser/message.js b/responser/message.js deleted file mode 100644 index e69de29..0000000 diff --git a/responser/user.js b/responser/user.js deleted file mode 100644 index e69de29..0000000 diff --git a/services/index.js b/services/index.js index e69de29..c23cfeb 100644 --- a/services/index.js +++ b/services/index.js @@ -0,0 +1,37 @@ +const + user = require('./user'), + weather = require('./weather'); + +function router(route){ + + const actions = [ + ...user, + ...weather + ] + + return actions.find((action)=>action.route==route); + +} + +function mainService(user){ + + //Register before any Actions + if(!user.firstname || !user.birthday){ + + const action = router('/user/register'); + action.service(user,action.previous,action.next) + //Cache route + //send to router + + } + + console.log(user) + + //Send to Main Menu + //send main menu + +} + + + +module.exports = mainService; \ No newline at end of file