-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
211 lines (190 loc) · 7.36 KB
/
app.js
File metadata and controls
211 lines (190 loc) · 7.36 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express');
const multer = require('multer');
const axios = require('axios');
const path = require('path');
const fs = require('fs').promises;
require('dotenv').config();
const app = express();
const uploadDir = path.join(__dirname, 'uploads');
const storage = multer.diskStorage({
destination: async (req, file, cb) => {
try {
await fs.mkdir(uploadDir, { recursive: true });
cb(null, uploadDir);
} catch (error) {
cb(error);
}
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}_${file.originalname}`);
},
});
const upload = multer({ storage });
const requiredEnvVars = ['GITHUB_TOKEN', 'REPO_OWNER', 'REPO_NAME', 'TELEGRAM_TOKEN', 'WEBHOOK_URL'];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.error(`Error: Environment variable ${envVar} is missing.`);
process.exit(1);
}
}
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const REPO_OWNER = process.env.REPO_OWNER;
const REPO_NAME = process.env.REPO_NAME;
const TELEGRAM_TOKEN = process.env.TELEGRAM_TOKEN;
const WEBHOOK_URL = process.env.WEBHOOK_URL.replace(/\/$/, '');
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} ${req.method} ${req.url}`);
next();
});
app.use(express.json());
async function validateGitHubToken() {
try {
const response = await axios.get('https://api.github.com/user', {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
Accept: 'application/vnd.github.v3+json',
},
});
console.log(`GitHub token validated for user: ${response.data.login}`);
return true;
} catch (error) {
console.error('GitHub token validation failed:', error.response?.data?.message || error.message);
console.error('Ensure GITHUB_TOKEN is valid and has "repo" scope.');
process.exit(1);
}
}
async function checkFileExists(repoOwner, repoName, filePath, githubToken) {
try {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filePath}`;
await axios.get(url, {
headers: {
Authorization: `token ${githubToken}`,
Accept: 'application/vnd.github.v3+json',
},
});
return true;
} catch (error) {
if (error.response?.status === 404) return false;
throw error;
}
}
async function uploadImageToGitHub(imagePath, repoOwner, repoName, uploadPath, githubToken, branch = 'main', commitMessage = 'Upload image via API') {
try {
const imageBuffer = await fs.readFile(imagePath);
const encodedImage = imageBuffer.toString('base64');
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${uploadPath}`;
let payload = {
message: commitMessage,
content: encodedImage,
branch: branch,
};
const fileExists = await checkFileExists(repoOwner, repoName, uploadPath, githubToken);
if (fileExists) {
const existingFile = await axios.get(url, {
headers: {
Authorization: `token ${githubToken}`,
Accept: 'application/vnd.github.v3+json',
},
});
payload.sha = existingFile.data.sha;
}
const response = await axios.put(url, payload, {
headers: {
Authorization: `token ${githubToken}`,
Accept: 'application/vnd.github.v3+json',
},
});
return response.data.content.download_url;
} catch (error) {
console.error('GitHub upload error:', error.response?.data?.message || error.message);
throw error;
}
}
app.post('/upload', upload.single('image'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No image provided' });
}
const uploadPath = `images/${Date.now()}_${req.file.originalname}`;
const imageUrl = await uploadImageToGitHub(req.file.path, REPO_OWNER, REPO_NAME, uploadPath, GITHUB_TOKEN);
await fs.unlink(req.file.path).catch((err) => console.error('Failed to delete temp file:', err.message));
res.json({ imageUrl });
} catch (error) {
console.error('Upload endpoint error:', error.message);
res.status(500).json({ error: error.message });
}
});
app.post(`/webhook/${TELEGRAM_TOKEN}`, async (req, res) => {
let tempPath;
try {
const update = req.body;
if (update.message && update.message.photo) {
const fileId = update.message.photo[update.message.photo.length - 1].file_id;
const file = await axios.get(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/getFile?file_id=${fileId}`);
const filePath = file.data.result.file_path;
const fileUrl = `https://api.telegram.org/file/bot${TELEGRAM_TOKEN}/${filePath}`;
const response = await axios({ url: fileUrl, responseType: 'arraybuffer' });
tempPath = path.join(__dirname, `temp_${Date.now()}.jpg`);
await fs.writeFile(tempPath, response.data);
const uploadPath = `images/${Date.now()}.jpg`;
const imageUrl = await uploadImageToGitHub(tempPath, REPO_OWNER, REPO_NAME, uploadPath, GITHUB_TOKEN);
await axios.post(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
chat_id: update.message.chat.id,
text: `Image uploaded successfully! URL: ${imageUrl}`,
});
await fs.unlink(tempPath).catch((err) => console.error('Failed to delete temp file:', err.message));
} else if (update.message && update.message.text === '/start') {
await axios.post(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
chat_id: update.message.chat.id,
text: 'Send an image to upload to GitHub!',
});
}
res.sendStatus(200);
} catch (error) {
console.error('Webhook error:', error.message);
if (update.message && update.message.chat) {
await axios.post(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
chat_id: update.message.chat.id,
text: `Error: ${error.message}. Please ensure the bot is properly configured.`,
}).catch((err) => console.error('Failed to send error message to Telegram:', err.message));
}
if (tempPath) {
await fs.unlink(tempPath).catch((err) => console.error('Failed to delete temp file:', err.message));
}
res.sendStatus(500);
}
});
app.get('/setWebhook', async (req, res) => {
try {
const webhookUrl = `${WEBHOOK_URL}/webhook/${TELEGRAM_TOKEN}`;
const response = await axios.get(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook?url=${webhookUrl}`);
console.log('Webhook set response:', response.data);
res.json(response.data);
} catch (error) {
console.error('Set webhook error:', error.message);
res.status(500).json({ error: error.message });
}
});
app.get('/getWebhookInfo', async (req, res) => {
try {
const response = await axios.get(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/getWebhookInfo`);
res.json(response.data);
} catch (error) {
console.error('Get webhook info error:', error.message);
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, async () => {
console.log(`Server running on port ${PORT}`);
try {
await validateGitHubToken();
const webhookUrl = `${WEBHOOK_URL}/webhook/${TELEGRAM_TOKEN}`;
const response = await axios.get(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook?url=${webhookUrl}`);
console.log('Webhook set successfully:', response.data);
} catch (error) {
console.error('Startup error:', error.message);
process.exit(1);
}
});
module.exports = app;