Search
GBP
Trading Software
    Menu Close
    Back to all

    Coding cTrader Telegram Alerts

    This article will explain how you can add Telegram alerts into your existing or new automated trading system for the cTrader trading platform using Microsoft Visual Studio & the C# programming language.

     

    Getting Started

    The first step is to watch the video below and then use the code example at the bottom of this article to paste into your telegram class, also included in this article at the bottom is a link to the complete Visual Studio project that was created.

     

    Watch a Video Explainer

    We have recorded a hands-on tutorial on how to use the client API with your trading system.

    Duration: 10-minutes - Watch full screen on YouTube - Change video quality to 1440p HD

     

    Code cTrader Telegram Alerts

     

    Telegram Class Code

    The code block below can be used in your project by copying and pasting it into a new class, you can also find this code further down the page in a complete solution file.

     

    using System;
    using System.Net;
    using Telegram.Bot;

    namespace cAlgo
    {
       public class Telegram
       {
          public Telegram()
          {
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
          }

          public string SendTelegram(string chatId, string token, string telegramMessage)
          {
             string reply = string.Empty;
             long id = Convert.ToInt64(chatID);

             try
             {
                var bot = new TelegramBotClient(token);
                bot.SendTextMessageAsync(id, telegramMessage);
                reply = "SUCCESS";
             }
             catch (Exception ex)
             {
                reply = "ERROR: " + ex.Message;
             }

             return reply;
          }
       }
    }

     

    What is Telegram Bot?

    The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram and in the video we explain how to use this library in your Visual Studio project without having to manually add the assembly files using NuGet.

     

    Download Example Project

    If you want to download the complete Visual Studio project files just follow the link below, you will also need to install Microsoft Visual Studio 2019, if years in the future this version is out of date, just open the project in your current version of Visual Studio and it will upgrade the code.

    File Size: 6MB

     

    How to Test Your Telegram Credentials

    It would help if you could also test your Telegram credentials first before running this example code to make sure they work, the free tool below will allow you to test your Bot Token and Chat ID and if they are incorrect it also provides links to other articles which will help you.

     

    Need Additional Help?

    We offer a custom development service to help traders build their trading robots and indicators, contact us today for a chat.