About MT5 to Telegram
Telegram Connector is a FREE library that joins any EAs with telegram's channels, groups or private chat. Both ways:
from MT5 to Telegram and back.
Key features
How to use library?
1. Add the following code to import funcs:
struct TelegramMessage
{
long MessageId;
string Text;
datetime TimestampGMT;
};
#import "Telegram Connector MT5.ex5"
bool TelegramInit(const string botToken, const long channelId);
bool TelegramInit(const string botToken, const string channelName);
void TelegramGetUpdates(TelegramMessage& messages[]);
string BuildReplyMarkup(const string keyboard, const bool resize = false, const bool false);
string BuildReplyKeyboardHide();
bool TelegramSendText(const string text, const bool htmlMode = false, const string replyMarkup = NULL);
bool TelegramSendPhoto(const string path, const string title, const bool useCommonFlag = false, const int timeout = 10000);
#import
2. Allow WebRequest for https://api.telegram.org/ (read
more https://www.metatrader4.com/en/trading-platform/help/setup/setup_experts)
3. Using @BotFather (https://core.telegram.org/bots)
find bot token and populate the following variables:
string botToken = "XXXXXXXXX:XXXXXXXXXXXXXXXXX_XXXXXXXX-X-XXXXXX";
string channelName = "TelegramChannelName"; // Channel or Group name
4.
Init library and send text with emoji
int OnInit()
{
if (!TelegramInit(botToken, channelName))
{
return INIT_FAILED;
}
TelegramSendText("========\n" + MQLInfoString(MQL_PROGRAM_NAME) + " has joined \xF44D" + "\n========");
EventSetTimer(3); // to check messages every 3 seconds
return(INIT_SUCCEEDED);
}
5. Create screenshot and send to Telegram
string filename = "tmp.png";
int width = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
int height = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
ChartScreenShot(0, filename, width, height);
string title = _Symbol + " " + EnumToString((ENUM_TIMEFRAMES)_Period);
StringReplace(title, "PERIOD_", "");
TelegramSendPhoto(filename, title);
FileDelete(filename);
6. Receive messages from Telegram
void OnTimer()
{
TelegramMessage messages[];
TelegramGetUpdates(messages);
for (int i = 0; i < ArraySize(messages); i++)
{
Print(TimeToString(messages[i].TimestampGMT, TIME_DATE | TIME_MINUTES | TIME_SECONDS), " ", messages[i].Text);
}
}
7. Send custom reply keyboard markup to Telegram (https://core.telegram.org/bots/api#replykeyboardmarkup)
Warning: only private chats support reply keyboard markup
#define KEYBOARD1 "[[\"\xF5A8 Reporting\"],[\"\xF4B9 Trading\"],[\"\xF4C8 Charts\"]]"
...
TelegramSendText("Choose your option", true, BuildReplyMarkup(KEYBOARD1));
8. Handle user's choice
void OnTimer()
{
TelegramMessage messages[];
TelegramGetUpdates(messages);
for (int i = 0; i < ArraySize(messages); i++)
{
if (StringFind(messages[i].Text, " Reporting") >= 0)
{
TelegramSendText("AccountEquity: " + DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY), 2), false, BuildReplyKeyboardHide());
}
}
}
Troubleshooting
1. Cannot call 'Telegram Connector.ex5::TelegramInit', 'Telegram Connector.ex5' is not loaded
Solution: Double check where Terminal has stored "Telegram Connector.ex5" file. For some reasons it can be "\MQL5\Scripts\Market\". Just move
file into "\MQL5\Libraries\" folder.