[Instruction] Telegram notifications

bart1's avatar

bart1 since: 03 Aug 2017;

  26 May 2020, 16:08
[Instruction] Telegram notifications

Here is a simple instruction to add Telegram notifications into your cBot or indicator.

First you need to create your own bot by chatting with BotFather in Telegram

Send /newbot and it will ask desired display name and user name for your new Telegram bot.

From the final message you need Token. Also you need to get chat ID for your new bot to send messages to you.

Go to your bot by clicking link in the final message and send some text.

To get chat ID you need to request latest updates from your bot by entering following URL in your browser:  https://api.telegram.org/bot<TOKEN>/getUpdates
replace <TOKEN> placeholder with your bot token

In response you will get text where you can find chat id
Example: ..."chat":{"id":123,...

Now you have 2 required parameters to send Telegram notifications and you can create cBot or indicator to test it. Telegram messages can be send using simple HTTP request. Here is a helper class that will do it for you.

public class TelegramBot
{
    public string Token { get; private set; }
    public int ChatId { get; private set; }

    public TelegramBot(string token, int chatId)
    {
        UseTls12();
        Token = token;
        ChatId = chatId;
    }

    public bool Send(string message)
    {
        var url = string.Format("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}", Token, ChatId, message);
        var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        var response = (System.Net.HttpWebResponse)request.GetResponse();
        return response.StatusCode == System.Net.HttpStatusCode.OK;
    }

    public bool Send(string message, params object[] args)
    {
        var formattedMessage = string.Format(message, args);
        return Send(formattedMessage);
    }

    private void UseTls12()
    {
        // Required for SSL/TLS connection with Telegram
        // Will work only on .Net 4.5 or higher
        // Using number to avoid compilation error
        System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
    }
}

After adding this class to your cBot or indicator you can send notifications like this:
replace <TOKEN> and <CHAT_ID> with yours

private TelegramBot TelegramBot;

protected override void OnStart()
{
    TelegramBot = new TelegramBot("<TOKEN>", <CHAT_ID>);
    TelegramBot.Send("Started {0} {1}", SymbolName, TimeFrame);
}

NOTE: cBot and indicators need extended permission. You need to use Internet or FullAccess permissions:

[Robot(AccessRights = AccessRights.Internet)]
public class NewcBot : Robot

keltrem since: 08 Feb 2020;

  23 Oct 2020, 13:27

Cannot get this code to work, any suggestions?

ClickAlgo's avatar

ClickAlgo since: 05 Feb 2015;

  23 Oct 2020, 15:40

You can also try this: 

~ Contact us for all your cBot & Indicator development ~

maciel.rafael1 since: 17 Sep 2020;

  27 Oct 2020, 17:50
RE:

bart1 said:

Here is a simple instruction to add Telegram notifications into your cBot or indicator.

First you need to create your own bot by chatting with BotFather in Telegram

Send /newbot and it will ask desired display name and user name for your new Telegram bot.

From the final message you need Token. Also you need to get chat ID for your new bot to send messages to you.

Go to your bot by clicking link in the final message and send some text.

To get chat ID you need to request latest updates from your bot by entering following URL in your browser:  https://api.telegram.org/bot<TOKEN>/getUpdates
replace <TOKEN> placeholder with your bot token

In response you will get text where you can find chat id
Example: ..."chat":{"id":123,...

Now you have 2 required parameters to send Telegram notifications and you can create cBot or indicator to test it. Telegram messages can be send using simple HTTP request. Here is a helper class that will do it for you.

public class TelegramBot
{
    public string Token { get; private set; }
    public int ChatId { get; private set; }

    public TelegramBot(string token, int chatId)
    {
        UseTls12();
        Token = token;
        ChatId = chatId;
    }

    public bool Send(string message)
    {
        var url = string.Format("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}", Token, ChatId, message);
        var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        var response = (System.Net.HttpWebResponse)request.GetResponse();
        return response.StatusCode == System.Net.HttpStatusCode.OK;
    }

    public bool Send(string message, params object[] args)
    {
        var formattedMessage = string.Format(message, args);
        return Send(formattedMessage);
    }

    private void UseTls12()
    {
        // Required for SSL/TLS connection with Telegram
        // Will work only on .Net 4.5 or higher
        // Using number to avoid compilation error
        System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
    }
}

After adding this class to your cBot or indicator you can send notifications like this:
replace <TOKEN> and <CHAT_ID> with yours

private TelegramBot TelegramBot;

protected override void OnStart()
{
    TelegramBot = new TelegramBot("<TOKEN>", <CHAT_ID>);
    TelegramBot.Send("Started {0} {1}", SymbolName, TimeFrame);
}

NOTE: cBot and indicators need extended permission. You need to use Internet or FullAccess permissions:

[Robot(AccessRights = AccessRights.Internet)]
public class NewcBot : Robot

I tried this in my bot and I get the following errors : TelegramBot is a type but is used like a variable and the second error is an object reference is required for the non-static field, method, or property. Both points to the lines OnStart

Any suggestions how to fix?

nguyendan81985 since: 24 Oct 2018;

  28 Sep 2021, 04:05
RE:

ClickAlgo said:

You can also try this: 

it worked. thanks. But i want to add this bot to Telegram Channel. could you pls hep me how to code? thanks