-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (86 loc) · 3.44 KB
/
index.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
require('dotenv').config();
const { google } = require('googleapis')
// variable data
var fileUploadStatus = require('./modules/state')
// user defined modules
const createBuffer = require('./modules/createBuffer')
const getUserId = require('./modules/getUserId')
const authorizeImageHandler = require('./modules/authorizeImageHandler')
const createDriveFolder = require('./modules/createDriveFolder')
const imageHandler = require('./modules/imageHandler')
const msgHandler = require('./modules/msgHandler')
const handleMultipleRequestsError = require('./modules/handleMultipleRequestsError')
const bot = require('./modules/bot')
const printCurrentStatus = require('./modules/printCurrentStatus')
// Load environment variables
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REFRESH_TOKEN = process.env.REFRESH_TOKEN;
const REDIRECT_URI = 'https://developers.google.com/oauthplayground'
const GOOGLE_DRIVE_FOLDER_NAME = process.env.GOOGLE_DRIVE_FOLDER_NAME;
// Creating a local buffer Folder
createBuffer()
console.log("Created Buffer Folder ... ")
// Initialize Google OAuth2 client
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
// Initialize Google Drive API
const googleDriveAPI = google.drive({
version: 'v3',
auth: oauth2Client,
});
// Created a Folder for drive upload
const googleDriveFolderId = createDriveFolder(googleDriveAPI,GOOGLE_DRIVE_FOLDER_NAME)
console.log("Goolgle Drive Storage Folder Created ... ")
// API Setup Complete
var chatInstance = ''
function clearStatus(){
fileUploadStatus.queued = []
fileUploadStatus.completed = []
fileUploadStatus.failed = []
fileUploadStatus.lastUploadedCount = 0
}
bot.on('message', async (msg) => {
// get userId of sender
const userUid = getUserId(msg)
const authorizedUserUid = authorizeImageHandler(userUid)
chatInstance = msg
try {
// Check if the message contains a photo
if (msg.photo) {
if(authorizedUserUid===true){
await imageHandler(msg, userUid, googleDriveFolderId, googleDriveAPI)
} else {
await msgHandler(msg, `User ${userUid} not authorized to upload images to Google drive`,false)
}
} else if(msg.text === '/h'){
await msgHandler(msg, `PicSync Bot Commands include : \n/h : help\n/s : current status of file uploads\n/c : clear file uploads status`)
} else if(msg.text === '/s'){
const statusMessage = printCurrentStatus();
await msgHandler(msg, statusMessage, true)
} else if(msg.text === '/c'){
clearStatus()
await msgHandler(msg, `cleared file status`)
} else {
if(msg.text){
await msgHandler(msg, `${userUid} says ${msg.text}`)
} else {
await msgHandler(msg, `${userUid} sent a different type of message.`)
}
}
} catch (err) {
await handleMultipleRequestsError(err, msg);
}
});
const handleShutdown = async (signal) => {
console.log(`Received signal ${signal}. Shutting down the server`)
if(chatInstance!==''){
await msgHandler(chatInstance, "Bot Pic Sync will be down for sometime, Meet you soon.", false)
}
}
process.on('SIGINT', () => handleShutdown('SIGINT'));
process.on('SIGTERM', () => handleShutdown('SIGTERM'));