Introduction isn't just for sending and receiving chat messages. It's also for automating your dialog flow, including work flow. Using a Telegram Bot gives you the ability to check prices, query status, manage trades, and even have a fun conversation. And if you're a serious crypto or forex trader, you can create your own Telegram Bot to manage your order flow. Telegram In this tutorial you'll use a Telegram Bot to query your orders on a Metatrader 4 account. You'll create a Telegram Bot ["bot"], build an Expert Advisor ["EA"] that can listen and process messages from a user, as well as reply to the user with orders and account data. Prerequisites * Metatrader 4 ["MT4"] client and demo account with any broker. * Telegram Bot created in your Telegram account. The tutorial walks you through creating a bot and configuring your MT4 client. How to Create a New Telegram Bot * Windows application to understand how the Telegram HTTP API works. Postman Step 1 - Peeking into Telegram's HTTP API with Postman Before diving into the MT4 EA build, let's take a peek at how the Telegram HTTP API work, in particular the method, with the Postman app. getUpdates The method returns messages from all channels, groups, and chats that the Bot is a member of. getUpdates In other words, the JSON message returned by this function can get crowded very quickly, if the Bot is a member of more than one group or channel. Each Bot can also have a private chat with whomever sends a private message to the Bot. For example, my Bot belongs to both a channel and a private chat where I can sent it private messages. TradeTitanSignal GET https: //api.telegram.org/bot**token**/getUpdates The response in JSON format requires some explanation: * The value represents a sequential number that is assigned to every message regardless of whether the message is from a channel post, or a private message, etc. update_id : , : , "update_id" 769794061 "update_id" 769794062 * The update id is followed by the message type, i.e for a channel message, while a private message begins with head. channel_post message * A channel has a negative , so we may have to use to scan for a channel. chat_id chat_title : { : , : , "chat" "id" -1001326947729 "title" "TradeTitanSignal" * A private chat has a positive , so we can use method to chat to the person. chat_id sendMessage : { : , : , "chat" "id" 902090608 "first_name" "Dennis" * A channel post has and , but a private message has , , , , and . chat_title chat_username from_is_bot from_first_name from_last_name chat_first_name chat_last_name : , : { : , : { : , : , : , : }, : , : "update_id" 769794061 "channel_post" "message_id" 4 "chat" "id" -1001326947729 "title" "TradeTitanSignal" "username" "tradetitansignal" "type" "channel" "date" 1569929874 "text" "hi i’m dennis" * Both channel post and private message have , , , , and . For both channel post and private message, the content can be accessed using . update_id message_id chat_id chat_type date text text : , : { : , : { : , : , : , : , : }, : { : , : , : , : }, : , : "update_id" 769794062 "message" "message_id" 4 "from" "id" 902090608 "is_bot" false "first_name" "Dennis" "last_name" "Lee" "language_code" "en" "chat" "id" 902090608 "first_name" "Dennis" "last_name" "Lee" "type" "private" "date" 1569931564 "text" "hi" * The response has a limit of 100 messages, but it doesn't clear automatically each time you call the method, unless you pass it an parameter. getUpdate offset * After processing the above messages, you should call the method, but with an offset value equal to the highest + 1, in this example above, i.e. 769794062 + 1. getUpdates update_id GET https: //api.telegram.org/bot**token**/getUpdates?offset=769794063 We should get an empty response if there are no new messages. It is important to note that calling the method again, without the offset value, returns an empty response. getUpdates This is because the Telegram API server stores the last offset that we passed as a parameter, so that we don't have to specify the same offset again. { : , : [] } "ok" true "result" Step 2 - Creating a New MT4 Expert Advisor In this section, let's create a new Expert Advisor ["EA"] in MetaEditor, and name the EA . TelegramRecon.mq4 Get the for the above MQ4 file. source code First, we include the file , which provides the class to manage a Telegram Bot. Telegram.mqh CCustomBot #include <Telegram.mqh> Second, we declare an input variable , which the user must provide. This is the HTTP API token for the Telegram Bot. TgrToken input string TgrToken; Third, we declare two global variables: (1) The variable is of type , which is a class defined in . This bot is used to send and process Telegram messages. bot CCustomBot Telegram.mqh (2) The variable is an integer, which holds the result of the method. The method returns zero if successful. intResult bot.GetMe() CCustomBot bot; int intResult; Fourth, in the function, we call the method and passing it the variable . OnInit() bot.Token() TgrToken Then we call the method, which returns a zero if successful. bot.GetMe() We then set the Timer to repeat every three seconds to call the function. OnTimer() bot.Token(TgrToken); intResult=bot.GetMe(); EventSetTimer( ); OnTimer(); //--- create timer 3 Finally, in the function, we check the variable . If it is a non-zero value, then we display the Error Description on the chart. OnTimer() intResult Otherwise, if the value of is zero (success), then we display the bot Name using the method. intResult bot.Name() ( intResult!= ) { BigComment( +GetErrorDescription(intResult) ); ; } BigComment( +bot.Name() ); if 0 "Error: " return "Bot name: " Compile the above source code, and you should see the EA in the Navigator under the Expert Advisors tab. TelegramRecon Step 3 - Running the MT4 EA for First Time Before running the EA, we have to add a URL to the List of allowed WebRequest URLs in MT4. Click on menu , then click on menu tab . Tools --> Options (Ctrl+O) Expert Advisors Check the box Allow WebRequest for listed URL, and add the URL https://api.telegram.org Click OK button to save the dialog window. Next, attach the EA to any chart, and in the Input dialog window, enter your unique HTTP API token in the Input field . TgrToken If you had done every step above correctly, you should see your Bot Name displayed on the chart. Step 4 - Building a Bot Query Tool In order to build a Bot Query Tool, we have to be able to both send and process messages to and from a user respectively. In this section, let's create a new include file in MetaEditor, and name the file . CPlusBotRecon.mqh Get the for the above MQH file. source code First, we include both the files and . The first MQH file is one that we will create later that does all the order queries, while the latter MQH contains the class , as previously discussed. PlusBotRecon.mqh Telegram.mqh CCustomBot #include <PlusBotRecon.mqh> #include <Telegram.mqh> Second, we declare a new class , which inherits all the methods and data of . In addition, we declare a new public method . CPlusBotRecon CCustomBot ProcessMessage() : class CPlusBotRecon public CCustomBot The method checks and parses any messages into commands, prepended by a slash ["/"], that we defined as follows: ProcessMessage() 1. /ordertotal - Return a count of opened orders 2. /ordertrade - Return ALL opened orders, where EACH order includes ticket, symbol, type, lots, openprice, stoploss, takeprofit, and prevticket 3. /orderticket <ticket> - Return an order by ticket 4. /historytotal - Return a count of history 5. /historyticket <ticket> - Return a history by ticket 6. /account - Return account number, currency, balance, equity, margin, freemargin, and profit. 7. /help - Display a list of bot commands Finally, let's create a new include file in MetaEditor, and name the file . PlusBotRecon.mqh For now, we simply return an empty string in each of our above function. Let's modify our previous EA in MetaEditor. TelegramRecon.mq4 Find the following code into the above MQ4 file and add the line to include file below as follows: CPlusBotRecon.mqh #include <Telegram.mqh> #include <CPlusBotRecon.mqh> Next, find and replace the following code: CCustomBot bot; with our inherited class: CPlusBotRecon bot; Next, find the code in the function and add two more lines below it as follows BigComment OnTimer() BigComment( +bot.Name() ); bot.GetUpdates(); bot.ProcessMessages(); "Bot name: " Compile and attach the EA to any chart, and in the Input dialog window, enter your unique HTTP API token in the Input field . TgrToken Open your Telegram app and send a message "/help" to your Telegram Bot. You should get the following response: Step 5 - Implementing the Bot Commands The final step is to actually implement the empty functions in our include file . PlusBotRecon.mqh Open your Telegram app and send a message "/ordertrade" to your Telegram Bot. You should get the following response: Conclusion In this tutorial, you used a Telegram Bot to query your orders from a Metatrader 4 client. You can use this approach to manage your order flow, view account details, open and close orders, or even broadcast trade signals to a Telegram group or channel. Get the Source Code You can download the above source code from GitHub repository . MT4-Telegram-Bot-Recon What To Do Next You can further extend your Bot in several meaningful ways: 1. Implementing Authentication - This is to ensure that only approved users have access to the Bot commands. 2. Implementing Open and Close Orders - This is to allow opening and closing orders using the Bot. 3. Implementing Modify SL and TP - This is to allow modifying the StopLoss and TakeProfit of an order. 4. Implementing Add and Delete Pending Orders - This is to manage pending orders using the Bot. 5. Create a Chart Query Tool: This is to allow users to query chart values, such as prices and indicator values for an instrument. 6. Broadcast Trading Signals in a Channel - This is to allow users to subscribe to your trade signals (one way communication). 7. Copy Trading Signal to a MT4 Client - This is to allow users to trade your signals automatically (requires a Client Bot).