Loading…

Telegram Sender

WebRequest fails with a return of -1 and GetLastError() equal to 4060, ERR_FUNCTION_NOT_ALLOWED, the first time any URL is used from a terminal that has not had it added to Tools > Options > Expert Advisors > Allow WebRequest for listed URL. Almost every "Telegram alert doesn't work" report traces back to exactly this, and a bare numeric error code does not tell the person reading the log what to actually go and click. TelegramSend() detects this specific failure and TgErrorDescription() spells out the fix in the returned text, not just the fact that something failed. The message is sent as an x-www-form-urlencoded POST body, which means every character in the text that is not alphanumeric or one of a small unreserved set has to be percent-encoded first - an unescaped "&" would otherwise be read as the start of a second form field, and an unescaped "%" as the start of an encoding it is not. TgUrlEncode() is exposed as its own function because it is useful independently of sending anything, for building any other Telegram or generic form-encoded request from the same header. TelegramSend() returns a small set of named codes (TG_ERR_NONE, TG_ERR_ BAD_TOKEN, TG_ERR_NOT_ALLOWED, TG_ERR_HTTP, TG_ERR_NO_RESPONSE) rather than just true/false, so a caller that wants to react differently to "not configured yet" versus "blocked by the terminal" versus "Telegram rejected it" can do so without re-deriving the distinction from GetLastError() and the HTTP status itself. TelegramSendSimple() collapses all of that into a single bool plus an automatic log line, for the common case of not caring which failure occurred. Both headers are self-contained - each implements its own URL encoding rather than including UrlCodec.mqh or WebJson.mqh from this catalogue, and WebRequest's signature and error behaviour (including error 4060) are identical on both platforms, so the MQL5 version is the same logic top to bottom. Notes and limitations - WebRequest only reaches the network when its URL is on the terminal's allow-list, and that list is per terminal installation, not per script - every machine this is downloaded to needs the same one-time setup step, on both platforms. - Text outside plain ASCII (accented letters, non-Latin scripts, emoji) is not UTF-8 encoded by TgUrlEncode and is sent as a literal "?" placeholder rather than mangled bytes. Stick to ASCII messages, or extend the encoder with proper UTF-8 byte splitting if you need more. - This header only calls sendMessage. Photos, documents and other Bot API methods are not covered. - A successful HTTP 200 means Telegram accepted the request; it does not by itself guarantee the message reached every intended recipient of a group or channel, only that the API call succeeded. - Identical limitations on both platforms - none of them are platform specific.

Script · MT5 · v1.0 · Free

About Telegram Sender

Telegram Sender is a script for MetaTrader 5. It has 3 settings, such as Bot token, Chat id and Message. MQL source code included.

Files in the download: TelegramSender.mq5.mqh, TelegramSender.txt, TelegramSenderDemo.ex5, TelegramSenderDemo.mq5.

Settings

SettingDefaultWhat it does
Bot tokenTelegram bot token from @BotFather (leave blank to skip the live send)
Chat idDestination chat id (leave blank to skip the live send)
MessageTelegramSender demo & testMessage to send if credentials are supplied

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Alerts · Telegram · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria