DmitrMakeev commited on
Commit
7ff616c
1 Parent(s): 519b6bf

Update app.py

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