Dooratre commited on
Commit
c1f9f48
·
verified ·
1 Parent(s): c845e0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -19,39 +19,34 @@ logger = logging.getLogger(__name__)
19
  # Initialize Flask app
20
  app = Flask(__name__)
21
 
22
- # Telegram configuration
23
- TELEGRAM_TOKEN = "7750258010:AAEfEn1Hc1h0n6uRc1KcPdZf7ozBEkehnEY"
24
- TELEGRAM_CHAT_ID = "6859142642"
25
- TELEGRAM_CHAT_ID2 = "5666511049"
26
-
27
- def send_telegram_message(message):
28
- """Send message to Telegram user"""
29
- url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
30
- data = {
31
- "chat_id": TELEGRAM_CHAT_ID,
32
- "text": message,
33
- "parse_mode": "HTML"
34
  }
35
- data2 = {
36
- "chat_id": TELEGRAM_CHAT_ID2,
37
- "text": message,
38
- "parse_mode": "HTML"
39
  }
 
40
  try:
41
- logger.info(f"Sending Telegram message to chat ID: {TELEGRAM_CHAT_ID}")
42
- response = requests.post(url, data=data)
43
- response2 = requests.post(url, data=data2)
44
  response_json = response.json()
45
-
46
- if response_json.get("ok"):
47
- logger.info("Telegram message sent successfully")
48
- return response_json
49
- else:
50
- logger.error(f"Telegram API error: {response_json.get('description', 'Unknown error')}")
51
- return None
52
  except Exception as e:
53
- logger.error(f"Error sending Telegram message: {e}", exc_info=True)
54
- return None
55
 
56
  def get_active_signals():
57
  """Get all active signals from the signals database"""
@@ -213,9 +208,9 @@ def format_telegram_message(signal_data):
213
  message = "🔔 <b>إشارة فوركس جديدة</b> 🔔\n\n"
214
  message += f"<b>🔹 الزوج:</b> {signal_data['pair']}\n"
215
  message += f"<b>📊 النوع:</b> {signal_data['type']}\n"
216
- message += f"<b>🎯 الدخول:</b> {signal_data['entry']}\n"
217
  message += f"<b>🛡️ وقف الخسارة:</b> {signal_data['stop_loss']}\n"
218
- message += f"<b>✨ الهدف:</b> {signal_data['take_profit']}\n"
219
 
220
  return message
221
 
@@ -278,15 +273,15 @@ def analyze_forex_groups():
278
  if update_signals_file(signal_data, pairs):
279
  logger.info(f"Signal for group {group_key} saved successfully")
280
 
281
- # Format and send Telegram message AFTER successfully saving to database
282
- telegram_message = format_telegram_message(signal_data)
283
- logger.info("Attempting to send Telegram message...")
284
- telegram_response = send_telegram_message(telegram_message)
285
 
286
- if telegram_response and telegram_response.get("ok"):
287
- logger.info(f"Telegram message for group {group_key} sent successfully")
288
  else:
289
- logger.error(f"Failed to send Telegram message for group {group_key}. Response: {telegram_response}")
290
 
291
  # Remove the group from deeper.json
292
  if remove_group_from_deeper(group_key):
 
19
  # Initialize Flask app
20
  app = Flask(__name__)
21
 
22
+ # Message API configuration
23
+ MESSAGE_API_URL = "https://aoamrnuwara.pythonanywhere.com/api/send-message"
24
+ MESSAGE_API_KEY = "Seakp0683asppoit"
25
+
26
+ def send_message_to_api(message):
27
+ """Send a message via the message forwarding API."""
28
+ headers = {
29
+ "Content-Type": "application/json",
30
+ "X-API-Key": MESSAGE_API_KEY
 
 
 
31
  }
32
+
33
+ payload = {
34
+ "message": message
 
35
  }
36
+
37
  try:
38
+ logger.info("Sending message via API")
39
+ response = requests.post(MESSAGE_API_URL, headers=headers, data=json.dumps(payload))
40
+ response.raise_for_status()
41
  response_json = response.json()
42
+ logger.info(f"Message sent to API successfully. Status Code: {response.status_code}")
43
+ return {"success": True, "response": response_json}
44
+ except requests.exceptions.RequestException as e:
45
+ logger.error(f"API request error: {e}", exc_info=True)
46
+ return {"success": False, "error": str(e)}
 
 
47
  except Exception as e:
48
+ logger.error(f"Unexpected error sending message to API: {e}", exc_info=True)
49
+ return {"success": False, "error": str(e)}
50
 
51
  def get_active_signals():
52
  """Get all active signals from the signals database"""
 
208
  message = "🔔 <b>إشارة فوركس جديدة</b> 🔔\n\n"
209
  message += f"<b>🔹 الزوج:</b> {signal_data['pair']}\n"
210
  message += f"<b>📊 النوع:</b> {signal_data['type']}\n"
211
+ message += f"<b>🎯 الد��ول:</b> {signal_data['entry']}\n"
212
  message += f"<b>🛡️ وقف الخسارة:</b> {signal_data['stop_loss']}\n"
213
+ message += f"<b>💰 الهدف:</b> {signal_data['take_profit']}\n"
214
 
215
  return message
216
 
 
273
  if update_signals_file(signal_data, pairs):
274
  logger.info(f"Signal for group {group_key} saved successfully")
275
 
276
+ # Format message and send via API instead of directly to Telegram
277
+ formatted_message = format_telegram_message(signal_data)
278
+ logger.info("Attempting to send message via API...")
279
+ api_response = send_message_to_api(formatted_message)
280
 
281
+ if api_response["success"]:
282
+ logger.info(f"Message for group {group_key} sent successfully via API")
283
  else:
284
+ logger.error(f"Failed to send message for group {group_key} via API. Error: {api_response.get('error')}")
285
 
286
  # Remove the group from deeper.json
287
  if remove_group_from_deeper(group_key):