MQL5 WebRequest отправить jsonPython

Программы на Python
Anonymous
MQL5 WebRequest отправить json

Сообщение Anonymous »

input string WebhookURL = "https://discord.com/api/webhooks/128802 ... XwUgeMOfB0";

void SendDiscordMessage(string message)
{
Print("Raw message: ", message);

// Escape newlines in the message
StringReplace(message, "\n", "\\n");

string json = "{\"content\":\"" + message + "\"}";
Print("Final JSON Payload: ", json);

char data[];
StringToCharArray(json, data);
ArrayResize(data, StringLen(json) + 1); // Ensure the array has enough space

char result[];
string headers = "Content-Type: application/json";
Print("Sending with headers: ", headers);
string result_headers;

int res = WebRequest("POST", WebhookURL, headers, 5000, data, result, result_headers);

if (res < 0)
{
Print("Error sending message: ", GetLastError());
return;
}

string response = CharArrayToString(result);
Print("HTTP Response Code: ", res);
Print("Response Raw Output: ", response);
}

void OnStart()
{
Print("Discord notification script running on MT5...");

int totalPositions = PositionsTotal();
Print("Total open positions: ", totalPositions);

for (int i = 0; i < totalPositions; i++)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
string symbol = PositionGetString(POSITION_SYMBOL);
double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double stopLoss = PositionGetDouble(POSITION_SL);
double takeProfit = PositionGetDouble(POSITION_TP);

// Format the message
string message = StringFormat("New Trade: %s\\nEntry: %.2f\\nSL: %.2f\\nTP: %.2f",
symbol, entryPrice, stopLoss, takeProfit);

Print("Complete message: ", message); // Log the complete message
SendDiscordMessage(message);
}
else
{
Print("Failed to select position with ticket: ", ticket);
}
}
}


Вывод:
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Discord notification script running on MT5...
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Total open positions: 1
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Complete message: New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Raw message: New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Final JSON Payload: {"content":"New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00"}
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Sending with headers: Content-Type: application/json
2024.09.24 18:03:06.211 TelegeramALert (MESZ24,H1) HTTP Response Code: 400
2024.09.24 18:03:06.211 TelegeramALert (MESZ24,H1) Response Raw Output: {"message": "The request body contains invalid JSON.", "code": 50109}


Подробнее здесь: https://stackoverflow.com/questions/790 ... -send-json

Вернуться в «Python»