DmitrMakeev commited on
Commit
1d05f9c
1 Parent(s): 40c4f6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +217 -13
app.py CHANGED
@@ -17,13 +17,14 @@ app = Flask(__name__, template_folder="./")
17
 
18
 
19
 
20
- app.config['UPLOAD_FOLDER'] = 'static'
 
 
21
  IMAGE_FILENAME = 'latest_image.jpg'
22
 
23
  # Создание директории, если она не существует
24
- if not os.path.exists(app.config['UPLOAD_FOLDER']):
25
- os.makedirs(app.config['UPLOAD_FOLDER'])
26
-
27
 
28
 
29
 
@@ -79,9 +80,6 @@ def se_mes_im():
79
  return render_template('se_mes_im.html')
80
 
81
 
82
- @app.route('/se_mes_im2', methods=['GET'])
83
- def se_mes_im2():
84
- return render_template('se_mes_im2.html')
85
 
86
 
87
 
@@ -98,21 +96,227 @@ def se_mes_im2():
98
 
99
 
100
 
 
 
 
 
 
 
 
 
 
 
101
  @app.route('/upload', methods=['POST'])
102
  def upload_file():
103
  if 'photo' not in request.files:
104
- return "No file part", 400
105
  file = request.files['photo']
106
  if file.filename == '':
107
- return "No selected file", 400
108
- save_path = os.path.join(app.config['UPLOAD_FOLDER'], IMAGE_FILENAME)
109
  file.save(save_path)
110
- file_url = f"/{app.config['UPLOAD_FOLDER']}/{IMAGE_FILENAME}"
111
- return file_url, 200
112
 
113
  @app.route('/image', methods=['GET'])
114
  def get_image():
115
- return send_from_directory(app.config['UPLOAD_FOLDER'], IMAGE_FILENAME)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
 
118
 
 
17
 
18
 
19
 
20
+ app.config['DEBUG'] = True
21
+
22
+ UPLOAD_FOLDER = 'static'
23
  IMAGE_FILENAME = 'latest_image.jpg'
24
 
25
  # Создание директории, если она не существует
26
+ if not os.path.exists(UPLOAD_FOLDER):
27
+ os.makedirs(UPLOAD_FOLDER)
 
28
 
29
 
30
 
 
80
  return render_template('se_mes_im.html')
81
 
82
 
 
 
 
83
 
84
 
85
 
 
96
 
97
 
98
 
99
+
100
+
101
+
102
+
103
+
104
+
105
+ @app.route('/online', methods=['GET'])
106
+ def online():
107
+ return render_template('online.html')
108
+
109
  @app.route('/upload', methods=['POST'])
110
  def upload_file():
111
  if 'photo' not in request.files:
112
+ return jsonify({'error': 'No file part'}), 400
113
  file = request.files['photo']
114
  if file.filename == '':
115
+ return jsonify({'error': 'No selected file'}), 400
116
+ save_path = os.path.join(UPLOAD_FOLDER, IMAGE_FILENAME)
117
  file.save(save_path)
118
+ return jsonify({'message': 'File uploaded successfully', 'file_url': f'/image', 'file_name': file.filename}), 200
 
119
 
120
  @app.route('/image', methods=['GET'])
121
  def get_image():
122
+ return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
123
+
124
+ @app.route('/se_mes_im2')
125
+ def index():
126
+ html = '''
127
+ <!DOCTYPE html>
128
+ <html lang="en">
129
+ <head>
130
+ <meta charset="UTF-8">
131
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
132
+ <title>Send Messages</title>
133
+ <style>
134
+ body {
135
+ font-family: Arial, sans-serif;
136
+ text-align: center;
137
+ background-color: #f0f0f0;
138
+ margin: 0;
139
+ padding: 0;
140
+ }
141
+ h1 {
142
+ background-color: #4CAF50;
143
+ color: white;
144
+ padding: 20px;
145
+ margin: 0;
146
+ border-bottom: 2px solid #388E3C;
147
+ }
148
+ .input-row {
149
+ display: flex;
150
+ justify-content: center;
151
+ gap: 10px;
152
+ margin-top: 20px;
153
+ }
154
+ .input-row input, .input-row textarea {
155
+ padding: 10px;
156
+ font-size: 16px;
157
+ border: 1px solid #ccc;
158
+ border-radius: 5px;
159
+ }
160
+ #messageInput {
161
+ width: 80%;
162
+ margin-top: 20px;
163
+ min-height: 100px;
164
+ }
165
+ #progressBarContainer {
166
+ width: 80%;
167
+ margin: 20px auto;
168
+ }
169
+ #progressBar {
170
+ width: 100%;
171
+ background-color: #ddd;
172
+ }
173
+ #progress {
174
+ width: 0%;
175
+ height: 30px;
176
+ background-color: #4CAF50;
177
+ text-align: center;
178
+ line-height: 30px;
179
+ color: white;
180
+ }
181
+ #sendButton {
182
+ color: white;
183
+ background-color: #4CAF50;
184
+ border: none;
185
+ cursor: pointer;
186
+ padding: 10px 20px;
187
+ font-size: 16px;
188
+ border-radius: 5px;
189
+ margin-top: 20px;
190
+ }
191
+ #sendButton:hover {
192
+ background-color: #388E3C;
193
+ }
194
+ </style>
195
+ </head>
196
+ <body>
197
+ <h1>Отправка сообщения(текст)</h1>
198
+ <div class="input-row">
199
+ <input type="text" id="apiKeyInput" placeholder="Введите API ключ">
200
+ <input type="number" id="minDelayInput" placeholder="Min Delay (ms)" value="500">
201
+ <input type="number" id="maxDelayInput" placeholder="Max Delay (ms)" value="1000">
202
+ <label>
203
+ Показывать превью:
204
+ <input type="checkbox" id="linkPreviewCheckbox" checked>
205
+ </label>
206
+ </div>
207
+ <textarea id="messageInput" placeholder="Введите текст сообщения, превью ссылки отображается в зависимости от настроек."></textarea>
208
+ <div id="progressBarContainer">
209
+ <div id="progressBar">
210
+ <div id="progress">0%</</div>
211
+ </div>
212
+ </div>
213
+ <input type="file" id="fileInput" accept=".txt">
214
+ <button id="sendButton">Запустить рассылку</button>
215
+
216
+ <h1>Upload Image</h1>
217
+ <form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
218
+ <input type="file" name="photo" id="photoInput">
219
+ <button type="submit">Upload Image</button>
220
+ </form>
221
+ <div id="uploadMessage"></div>
222
+ <h1>Uploaded Image</h1>
223
+ <img id="cameraImage" src="" alt="No image uploaded" style="width:100%;">
224
+
225
+ <script>
226
+ document.getElementById('uploadForm').addEventListener('submit', function(event) {
227
+ event.preventDefault();
228
+ var formData = new FormData(this);
229
+ fetch('/upload', {
230
+ method: 'POST',
231
+ body: formData
232
+ })
233
+ .then(response => response.json())
234
+ .then(data => {
235
+ if (data.error) {
236
+ document.getElementById('uploadMessage').innerText = data.error;
237
+ } else {
238
+ document.getElementById('uploadMessage').innerText = data.message;
239
+ document.getElementById('cameraImage').src = data.file_url + '?' + new Date().getTime();
240
+ document.getElementById('uploadedFileName').innerText = data.file_name;
241
+ }
242
+ })
243
+ .catch(error => {
244
+ document.getElementById('uploadMessage').innerText = 'Error: ' + error.message;
245
+ });
246
+ });
247
+
248
+ document.getElementById('sendButton').addEventListener('click', function() {
249
+ const apiKey = document.getElementById('apiKeyInput').value;
250
+ const message = document.getElementById('messageInput').value;
251
+ const minDelay = parseInt(document.getElementById('minDelayInput').value) || 500;
252
+ const maxDelay = parseInt(document.getElementById('maxDelayInput').value) || 10000;
253
+ const linkPreview = document.getElementById('linkPreviewCheckbox').checked;
254
+ if (!apiKey) {
255
+ alert('Please enter your API key.');
256
+ return;
257
+ }
258
+ if (!message) {
259
+ alert('Please enter a message.');
260
+ return;
261
+ }
262
+ if (minDelay >= maxDelay) {
263
+ alert('Min delay must be less than max delay.');
264
+ return;
265
+ }
266
+ const fileInput = document.getElementById('fileInput');
267
+ const file = fileInput.files[0];
268
+ if (!file) {
269
+ alert('Please select a file.');
270
+ return;
271
+ }
272
+ const reader = new FileReader();
273
+ reader.onload = function(event) {
274
+ const text = event.target.result;
275
+ const phones = text.split('\n').map(phone => phone.trim()).filter(phone => phone);
276
+ sendMessages(phones, apiKey, message, minDelay, maxDelay, linkPreview);
277
+ };
278
+ reader.readAsText(file);
279
+ });
280
+
281
+ async function sendMessages(phones, apiKey, message, minDelay, maxDelay, linkPreview) {
282
+ const totalPhones = phones.length;
283
+ const progressBar = document.getElementById('progress');
284
+ for (let i = 0; i < totalPhones; i++) {
285
+ const phone = phones[i];
286
+ try {
287
+ const response = await fetch(`https://api.green-api.com/waInstance1101952913/sendMessage/${apiKey}`, {
288
+ method: 'POST',
289
+ headers: {
290
+ 'Content-Type': 'application/json'
291
+ },
292
+ body: JSON.stringify({
293
+ chatId: `${phone}@c.us`,
294
+ message: message,
295
+ linkPreview: linkPreview
296
+ })
297
+ });
298
+ if (!response.ok) {
299
+ throw new Error(`HTTP error! status: ${response.status}`);
300
+ }
301
+ const data = await response.json();
302
+ console.log(`Message sent to ${phone}:`, data);
303
+ } catch (error) {
304
+ console.error(`Error sending message to ${phone}:`, error);
305
+ }
306
+ const progress = ((i + 1) / totalPhones) * 100;
307
+ progressBar.style.width = `${progress}%`;
308
+ progressBar.textContent = `${progress.toFixed(2)}%`;
309
+ if (i < totalPhones - 1) {
310
+ const randomDelay = Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
311
+ await new Promise(resolve => setTimeout(resolve, randomDelay));
312
+ }
313
+ }
314
+ }
315
+ </script>
316
+ </body>
317
+ </html>
318
+ '''
319
+ return render_template_string(html)
320
 
321
 
322