-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (93 loc) · 3.36 KB
/
app.js
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
//All packages
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const { TwitterApi } = require('twitter-api-v2');
const compression=require('compression');
//All models
const User = require('./models/user');
const Admin = require('./models/admin');
const Tweet = require('./models/tweet');
//All routes
const appRoutes = require('./routes/appRoutes');
const userRoutes = require('./routes/userRoutes');
const webhookRoutes = require('./routes/webhookRoutes');
const DM=require('./controllers/sendDMcontroller');
const CronJob = require('cron').CronJob;
const job = new CronJob('0 */1 * * * *', function () {
const d = new Date();
const now = Date.parse(d);
const remind = Tweet.find({ remindFlag: true }).then(tweets => {
console.log(tweets);
for (i = 0; i < tweets.length; i++) {
const comp = Date.parse(tweets[i].remindTime);
if (d > comp) {
DM.sendDM(tweets[i].tweetURL, tweets[i].userId);
DM.sendDM("Make sure to read this cause i have marked it as read!",tweets[i].userId);
Tweet.updateOne({userId:tweets[i].userId,remindTime:tweets[i].remindTime},{remindFlag:false}).then(result=>{
console.log(result);
})
}
}
});
console.log(".");
}
)
job.start();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(compression());
//Handle CRC request response from twitter
app.use('/webhook', webhookRoutes)
app.use('/app', appRoutes);
app.use('/user', userRoutes);
//Handle OAuth2 callback
app.get('/callback', async (req, res) => {
// Extract state and code from query string
const state = req.query.state;
const code = req.query.code;
console.log("state-" + state);
console.log("code-" + code);
//Take code verifier from first step an put it here
let codeVerifier = '6iKADiTsfA.R8g.49aYbBJzZG4T86L4YgQ5VqMqyBT_DTMChnR-LtPxp4c.D9rdJ~sv8NewkEWZL3GT97oHA.SDd6PXUFdXqYZ8EjeGxCXkX~zas9pR3JothdmDGVcBf';
console.log("verifier-" + codeVerifier);
const client = new TwitterApi({ clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET });
client.loginWithOAuth2({ code, codeVerifier, redirectUri: process.env.CALLBACK_URL })
.then(async ({ client: loggedClient, accessToken, refreshToken, expiresIn }) => {
const { dm_conversation_id, dm_event_id } = await loggedClient.v2.sendDmToParticipant(process.env.USER_ID, {
text: 'Testing!',
})
const newRefreshToken = refreshToken;
const newAccessToken = accessToken;
const newAdmin = new Admin({
user: 'All details',
oauth_acces_token: newAccessToken,
oauth_refresh_token: newRefreshToken,
oauth_codeVerfier: codeVerifier,
oauth_state: state,
oauth_code: code,
expiresIn: expiresIn
})
newAdmin.save();
console.log(newRefreshToken);
console.log(newAccessToken);
})
.catch(() => res.status(403).send('Invalid verifier or access tokens!'));
});
//Error handler
app.use((error, req, res, next) => {
console.log(error);
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({ message: message, data: data });
});
mongoose
.connect(
process.env.MONGODB_URL
)
.then(result => {
app.listen(3000);
console.log("Server is running on port 3000")
})
.catch(err => console.log(err));