Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.5 kB
3
Indexable
Never
#include <Wininet.mqh>
#include <stdlib.mqh>

#define TELEGRAM_BOT_TOKEN  "your_bot_token"
#define TELEGRAM_CHAT_ID    "your_chat_id"
#define SCREENSHOT_ENABLED  true

string message;

// Sends a message to a Telegram chat
void SendTelegramMessage(string message)
{
   string url = "https://api.telegram.org/bot" + TELEGRAM_BOT_TOKEN + "/sendMessage?chat_id=" + TELEGRAM_CHAT_ID + "&text=" + message;
   url = StringReplace(url, " ", "%20");
   url = StringReplace(url, "\n", "%0A");

   InternetCloseHandle(InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL));

   HINTERNET hInternet = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   if (hInternet == NULL)
      return;

   HINTERNET hConnect = InternetConnect(hInternet, "api.telegram.org", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
   if (hConnect == NULL)
      return;

   HINTERNET hRequest = HttpOpenRequest(hConnect, "GET", url, "HTTP/1.1", NULL, NULL, INTERNET_FLAG_SECURE, 0);
   if (hRequest == NULL)
      return;

   BOOL bSend = HttpSendRequest(hRequest, NULL, 0, NULL, 0);
   if (!bSend)
      return;

   InternetCloseHandle(hRequest);
   InternetCloseHandle(hConnect);
   InternetCloseHandle(hInternet);
}

// Sends a screenshot to a Telegram chat
void SendTelegramScreenshot(string message)
{
   string fileName = "screenshot.png";

   int width = WindowGetWidht(0);
   int height = WindowGetHeight(0);
   uchar* buffer = new uchar[width*height*3];
   if (buffer == NULL)
      return;

   if (!ScreenShot(buffer, fileName, width, height))
   {
      delete[] buffer;
      return;
   }

   string boundary = "---------------------------7e01fd520604e";
   string data = "--" + boundary + "\r\n"
               + "Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n" + TELEGRAM_CHAT_ID + "\r\n"
               + "--" + boundary + "\r\n"
               + "Content-Disposition: form-data; name=\"caption\"\r\n\r\n" + message + "\r\n"
               + "--" + boundary + "\r\n"
               + "Content-Disposition: form-data; name=\"photo\"; filename=\"screenshot.png\r\n"
               + "Content-Type: image/png\r\n\r\n";
   int fileSize = FileSize(fileName);
   if (fileSize <= 0)
   {
      delete[] buffer;
      return;
   }

   uchar* fileBuffer = new uchar[fileSize];
   if (fileBuffer == NULL)
   {
      delete[] buffer;
      return;
   }

   int result = FileReadArray(fileName, fileBuffer, fileSize);
   if (result != fileSize)
   {
      delete[] fileBuffer;
      delete[] buffer;
      return;
   }

   data += (string)fileBuffer;
   delete[] fileBuffer;

   data += "\r\n--" + boundary + "--\r\n";

   InternetCloseHandle(InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL));

   hInternet = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   if (hInternet == NULL)
   {
      delete[] buffer;
      return;
   }

   hConnect = InternetConnect(hInternet, "api.telegram.org", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
   if (hConnect == NULL)
   {
      delete[] buffer;
      return;
   }

   string url = "https://api.telegram.org/bot" + TELEGRAM_BOT_TOKEN + "/sendPhoto";
   hRequest = HttpOpenRequest(hConnect, "POST", url, "HTTP/1.1", NULL, NULL, INTERNET_FLAG_SECURE, 0);
   if (hRequest == NULL)
   {
      delete[] buffer;
      return;
   }

   string headers = "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"
                  + "Content-Length: " + (string)data.Length() + "\r\n";

   bSend = HttpSendRequest(hRequest, headers, headers.Length(), (char*)data.c_str(), data.Length());
   if (!bSend)
   {
      delete[] buffer;
      return;
   }

   InternetCloseHandle(hRequest);
   InternetCloseHandle(hConnect);
   InternetCloseHandle(hInternet);

   delete[] buffer;
}

int OnInit()
{
   // Initialize the Expert Advisor
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   // Deinitialize the Expert Advisor
}

void OnTick()
{
   // Check if an alert message is available
   if (AlertGet(message))
   {
      // Send the alert message to the Telegram chat
      SendTelegramMessage(message);

      // If screenshot is enabled, send a screenshot of the chart to the Telegram chat
      if (SCREENSHOT_ENABLED)
         SendTelegramScreenshot(message);
   }
}