Category Other  Published on 30/04/2023

Sample Telegram Function

Description

Use the below Telegram function to send telegram messages in your Indicator.

Search on google on how to create Token using BotFather and get ChatId

public void SendTelegram(String Token, String ChatId, String msg)
        {
            try
            {
                HttpRequest req = new(new($"https://api.telegram.org/bot{Token}/sendMessage?chat_id={ChatId}&text={msg}"));
                req.Method = HttpMethod.Post;
                var res = Http.Send(req);
                
                if (!res.IsSuccessful)
                    msg += $": Error Sending Telegram Message: {res.StatusCode}, {res.Body}";
                else
                    msg += ": Success";
            }
            catch (Exception e)
            {
                msg += ": EXCEPTION : " + e.Message + "\n\n" + e.StackTrace;
            }

            Print(msg);
        }


using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class AksTelegramSample : Indicator
    {
        [Parameter("Token", DefaultValue = "")]
        public string TOKEN { get; set; }

        [Parameter("ChatId", DefaultValue = "")]
        public string CHATID { get; set; }

        protected override void Initialize()
        {
        }
        
        public void SendTelegram(String msg)
        {
            try
            {
                HttpRequest req = new(new($"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHATID}&text={msg}"));
                req.Method = HttpMethod.Post;
                var res = Http.Send(req);
                
                if (!res.IsSuccessful)
                    msg += $": Error Sending Telegram Message: {res.StatusCode}, {res.Body}";
                else
                    msg += ": Success";
            }
            catch (Exception e)
            {
                msg += ": EXCEPTION : " + e.Message + "\n\n" + e.StackTrace;
            }

            Print(msg);
        }

        public override void Calculate(int index)
        {
            // Write Logic to invoke telegram message function
            // For example: SendTelegram("TEST: " + Bars.Count);
        }
    }
}

AK
aksbenz

Joined on 26.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Aks Telegram Sample.algo
  • Rating: 5
  • Installs: 519
  • Modified: 30/04/2023 00:17
Comments
Log in to add a comment.
ZA
zaffarkhatri22 · 1 year ago

Greetings Im itching to know if I may use this article in one of my blogs if I link back to you? Thanksbecome a credit card processor

RA
ralemo2114 · 1 year ago

I adore all of the threads, When i relished, I had appreciate considerably more facts on this, considering it truly is rather satisfying., Love suitable for presenting.selling credit card processing

AR
arhamabdul429 · 1 year ago

Audio began playing as soon as I opened this site, so frustrating!Packers and Movers Mumbai