Topics

Forum Topics not found

Replies

alexempedrad
03 Oct 2022, 03:51

RE:

PanagiotisChar said:

Hi Alex,

There are easier ways to do this. Check this video.

Aieden Technologies

Need Help? Join us on Telegram

 

Hi PanagiotisChar,

Thank you very much for the link. My bot works flawlessly now.

 

Alex


@alexempedrad

alexempedrad
01 Oct 2022, 11:47 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

maciel.rafael1 said:

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?

I was able to send telegram messages using the code above. Thank you very much. However, there is a compiler warning - "WebRequest.Create(string) is obsolete ... " . How do I change this to HttpClient Request?

Thank you for answering.

Alex


@alexempedrad