-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathroutes-api.js
More file actions
96 lines (86 loc) · 2.91 KB
/
routes-api.js
File metadata and controls
96 lines (86 loc) · 2.91 KB
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
import express from 'express';
import { apiKeyAuth } from './middleware.js';
import { sendMessage, sendImageToUser, sendImagesToUser, getUserInfo, getUserID, getThreadInfo } from './fbService.js';
import { fbAccounts } from './api/facebook/fb.js';
const router = express.Router();
router.use(apiKeyAuth);
router.get('/accounts', (req, res) => {
const accounts = fbAccounts.map(acc => ({
ownId: acc.ownId,
userId: acc.userId || 'N/A'
}));
res.json({ success: true, data: accounts });
});
router.post('/sendMessage', async (req, res) => {
try {
const result = await sendMessage(req, res);
res.json(result);
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
router.post('/sendImageToUser', async (req, res) => {
try {
const result = await sendImageToUser(req, res);
res.json(result);
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
router.post('/sendImagesToUser', async (req, res) => {
try {
const result = await sendImagesToUser(req, res);
res.json(result);
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
router.get('/userInfo', async (req, res) => {
const { userId, ownId } = req.query;
if (!userId || !ownId) {
return res.status(400).json({ success: false, error: 'userId and ownId are required' });
}
const account = fbAccounts.find(acc => acc.ownId === ownId);
if (!account) {
return res.status(404).json({ success: false, error: 'Account not found' });
}
try {
const result = await getUserInfo(account.api, userId);
res.json({ success: true, data: result });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
router.get('/userId', async (req, res) => {
const { name, ownId } = req.query;
if (!name || !ownId) {
return res.status(400).json({ success: false, error: 'name and ownId are required' });
}
const account = fbAccounts.find(acc => acc.ownId === ownId);
if (!account) {
return res.status(404).json({ success: false, error: 'Account not found' });
}
try {
const result = await getUserID(account.api, name);
res.json({ success: true, data: result });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
router.get('/threadInfo', async (req, res) => {
const { threadId, ownId } = req.query;
if (!threadId || !ownId) {
return res.status(400).json({ success: false, error: 'threadId and ownId are required' });
}
const account = fbAccounts.find(acc => acc.ownId === ownId);
if (!account) {
return res.status(404).json({ success: false, error: 'Account not found' });
}
try {
const result = await getThreadInfo(account.api, threadId);
res.json({ success: true, data: result });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
export default router;