Dooratre commited on
Commit
044bd03
·
verified ·
1 Parent(s): cb8be24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -20,31 +20,32 @@ logger = logging.getLogger(__name__)
20
  # Initialize Flask app
21
  app = Flask(__name__)
22
 
23
- # Telegram configuration
24
- TELEGRAM_TOKEN = "7750258010:AAEfEn1Hc1h0n6uRc1KcPdZf7ozBEkehnEY"
25
- TELEGRAM_CHAT_ID = "6859142642"
26
- TELEGRAM_CHAT_ID2 = "5666511049"
27
-
28
- def send_telegram_message(text):
29
- """Send a message to Telegram."""
30
- url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
31
- data = {
32
- "chat_id": TELEGRAM_CHAT_ID,
33
- "text": text,
34
- "parse_mode": "HTML"
35
  }
36
- data2 = {
37
- "chat_id": TELEGRAM_CHAT_ID2,
38
- "text": text,
39
- "parse_mode": "HTML"
40
  }
 
41
  try:
42
- response = requests.post(url, data=data)
43
- response2 = requests.post(url, data=data2)
44
- return response.json()
 
 
 
 
45
  except Exception as e:
46
- logger.error(f"Error sending Telegram message: {e}")
47
- return {"ok": False, "error": str(e)}
48
 
49
  def send_to_api(pair_name, message):
50
  """Send the message to the external API."""
@@ -204,12 +205,12 @@ def check_and_process_signals():
204
  close_signal = False
205
 
206
  for pair, message_data in all_messages.items():
207
- # Send to Telegram
208
- telegram_result = send_telegram_message(message_data["full_message"])
209
- if telegram_result.get("ok"):
210
- logger.info(f"Telegram message sent for {pair}")
211
  else:
212
- logger.error(f"Failed to send Telegram message: {telegram_result}")
213
 
214
  # Handle based on message type
215
  if message_data["type"] == "regular":
@@ -233,12 +234,12 @@ def check_and_process_signals():
233
  message_data = extract_message_content(analysis, main_pair)
234
 
235
  if message_data:
236
- # Send to Telegram
237
- telegram_result = send_telegram_message(message_data["full_message"])
238
- if telegram_result.get("ok"):
239
- logger.info(f"Telegram message sent for {main_pair}")
240
  else:
241
- logger.error(f"Failed to send Telegram message: {telegram_result}")
242
 
243
  # Handle based on message type
244
  if message_data["type"] == "regular":
 
20
  # Initialize Flask app
21
  app = Flask(__name__)
22
 
23
+ # Message API configuration
24
+ MESSAGE_API_URL = "https://aoamrnuwara.pythonanywhere.com/api/send-message"
25
+ MESSAGE_API_KEY = "Seakp0683asppoit"
26
+
27
+ def send_message_to_api(message):
28
+ """Send a message via the message forwarding API."""
29
+ headers = {
30
+ "Content-Type": "application/json",
31
+ "X-API-Key": MESSAGE_API_KEY
 
 
 
32
  }
33
+
34
+ payload = {
35
+ "message": message
 
36
  }
37
+
38
  try:
39
+ response = requests.post(MESSAGE_API_URL, headers=headers, data=json.dumps(payload))
40
+ response.raise_for_status()
41
+ logger.info(f"Message sent to API successfully. Status Code: {response.status_code}")
42
+ return {"success": True, "response": response.json()}
43
+ except requests.exceptions.RequestException as e:
44
+ logger.error(f"API request error: {e}")
45
+ return {"success": False, "error": str(e)}
46
  except Exception as e:
47
+ logger.error(f"Unexpected error sending message to API: {e}")
48
+ return {"success": False, "error": str(e)}
49
 
50
  def send_to_api(pair_name, message):
51
  """Send the message to the external API."""
 
205
  close_signal = False
206
 
207
  for pair, message_data in all_messages.items():
208
+ # Send to messaging API instead of directly to Telegram
209
+ api_msg_result = send_message_to_api(message_data["full_message"])
210
+ if api_msg_result["success"]:
211
+ logger.info(f"Message sent via API for {pair}")
212
  else:
213
+ logger.error(f"Failed to send message via API: {api_msg_result.get('error')}")
214
 
215
  # Handle based on message type
216
  if message_data["type"] == "regular":
 
234
  message_data = extract_message_content(analysis, main_pair)
235
 
236
  if message_data:
237
+ # Send to messaging API instead of directly to Telegram
238
+ api_msg_result = send_message_to_api(message_data["full_message"])
239
+ if api_msg_result["success"]:
240
+ logger.info(f"Message sent via API for {main_pair}")
241
  else:
242
+ logger.error(f"Failed to send message via API: {api_msg_result.get('error')}")
243
 
244
  # Handle based on message type
245
  if message_data["type"] == "regular":