• 概述
  • 评论 (4)
  • 评论 (22)

Telegram Connector

About MT4 to Telegram

Telegram Connector is a FREE library that joins any EAs with telegram's channels, groups or private chat. Both ways: from MT4 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.ex4"
   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
 oneTime = 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" + WindowExpertName() + " 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(AccountEquity(), 2), false, BuildReplyKeyboardHide());
      }
   }
}


Troubleshooting

1. Cannot call 'Telegram Connector.ex4::TelegramInit', 'Telegram Connector.ex4' is not loaded

Solution: Double check where Terminal has stored "Telegram Connector.ex4" file. For some reasons it can be "\MQL4\Scripts\Market\". Just move file into "\MQL4\Libraries\" folder.

Yudha Adicita
190
Yudha Adicita 2020.03.04 16:42 
 

Great works Andrii, help me a lot to create my signal bot. Thanks for your quick support.

gtmasterprofit
156
gtmasterprofit 2020.02.07 07:58 
 

Thank you Andrii for this awesome library and your support!

works like a charm, you deserve more than 5 stars.

Cheers!

Vitalii Kolodka
136
Vitalii Kolodka 2020.02.02 21:52 
 

Thank you very much!

Great library!

Easy to use! No bugs.

Bogdan Sadovoy
124
Bogdan Sadovoy 2020.01.23 08:10 
 

Thanks for doing this for free!

Everything works well.

I will look forward to your new products.

Good luck, Andrii.