Webrequest Telegram with basic html

 

hello all

I've been trying to send a webrequest from mt4 to Telegram to send a message with the minimum supported html, eg bold, some emoticons, italics via parse, parsemode2, html but all not working, I receive message 400 obviously the raw text works and all mt4 settings are correct; can someone give me direction?

 
here the example of code with debug 
and errors.. in substance it reads 0 and not the real message that is just an exmaple in my code
#property link      ""
#property version   "1.00"

// Telegram Bot Token
input string TelegramBotToken = "hided"; // Your bot token

// Telegram Chat ID
input long   TelegramChatID   = -100hided; // Your chat ID

// Function to send a message to Telegram
bool SendTelegramMessage(string message) {
  Print("SendTelegramMessage function called!"); // Check if the function is called
  Print("Received message: ", message);

  // --- Force the message to a test value ---
  message = "Forced test message from minimal EA!";

  string url     = "https://api.telegram.org/bot" + TelegramBotToken + "/sendMessage";

  // Construct the full request string (headers + body)
  string request = "";
  request += "POST /bot" + TelegramBotToken + "/sendMessage HTTP/1.1\r\n";
  request += "Host: api.telegram.org\r\n";
  request += "Content-Type: application/json\r\n";
  request += "User-Agent: MT4-Telegram-EA\r\n";

  string postData = "{ \"chat_id\": \"" + (string)TelegramChatID +
                    "\", \"text\": \"" + StringReplace(message,"\"","\\\"") +
                    "\", \"parse_mode\": \"MarkdownV2\" }";

  Print("postData: ", postData); // Print postData for debugging

  request += "Content-Length: " + StringLen(postData) + "\r\n";
  request += "\r\n"; // Empty line to separate headers from body
  request += postData;

  Print("request: ", request); // Print request for debugging

  // Variables for WebRequest
  char   response[];
  string response_headers = "";

  // Convert request (string) to request_char_array (char array)
  char   request_char_array[];
  StringToCharArray(request, request_char_array);

  // Send the HTTP request using the simpler WebRequest overload
  int result_code = WebRequest("POST", url, "", 5000, request_char_array, response, response_headers);

  if (result_code == -1) {
    Print("WebRequest error: ", GetLastError());
    ResetLastError();
    return false;
  }

  // Check the HTTP response code (extracted from response_headers)
  int responseCode = GetResponseCodeFromHeaders(response_headers);
  if (responseCode != 200) {
    Print("HTTP Response Error: ", responseCode);
    Print("Response Headers: ", response_headers);
    Print("Response Body: ", CharArrayToString(response));
    return false;
  }

  return true;
}

// Helper function to extract the HTTP response code from the headers
int GetResponseCodeFromHeaders(string headers) {
  string lines[];
  int numLines = StringSplit(headers, '\r', lines);

  for (int i = 0; i < numLines; i++) {
    string line = StringTrimLeft(StringTrimRight(lines[i]));
    if (StringFind(line, "HTTP") == 0) {
      string parts[];
      int numParts = StringSplit(line, ' ', parts);
      if (numParts >= 2) {
        return (int)StringToInteger(parts[1]);
      }
    }
  }
  return -1; // Return -1 if not found
}

void OnInit() {
  Print("OnInit() function called!"); // Check if OnInit is called

  // --- Call SendTelegramMessage with a test message ---
  if (SendTelegramMessage("Test message from minimal EA!")) {
      Print("Test message sent successfully!");
  } else {
      Print("Failed to send test message.");
  }
}



Failed to send test message.
 Response Body: {"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
Response Headers: HTTP/1.1 400 Bad Request
Connection: keep-alive
Date: Thu, 16 Jan 2025 20:13:45 GMT
Content-Length: 80
Content-Type: application/json
Server: nginx/1.18.0
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection


HTTP Response Error: 400
request: POST /bothHided/sendMessage HTTP/1.1
Host: api.telegram.org
Content-Type: application/json
User-Agent: MT4-Telegram-EA
Content-Length: 72

{ "chat_id": "-hided", "text": "0", "parse_mode": "MarkdownV2" }
: postData: { "chat_id": "-hided", "text": "0", "parse_mode": "MarkdownV2" }
 Received message: Test message from minimal EA!
SendTelegramMessage function called!
OnInit() function called!

 
you are using webrequest wrongly, search this forum and you'll find many similar issues solved.
good luck
 
Soewono Effendi #:
you are using webrequest wrongly, search this forum and you'll find many similar issues solved.
good luck

would you provide some more examples or guidance? cannot really find

 
Enrico De Landerset #:

would you provide some more examples or guidance? cannot really find

Learn to search!

You would have found this series: https://www.mql5.com/en/search#!keyword=telegram&module=mql5_module_articles

So either you switch to the modern and faster MT5 or you use it to convert the MQL4 code to MQL5.

 
I need it in mt4 obviously because I need to send signals from a complex bot only mt4.  if someone wants to provide a simple code of webrequest mt4 to Telegram sending a message with basic formatting (bold, Italics, emoticons) I am happy to pay; contact me pivately.

or even better formatted like this EA (MT4 TO TLGR) one that is amazing...  but unfortunately some functionalities missing