[Instruction] Telegram notifications
[Instruction] Telegram notifications
26 May 2020, 16:08
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
Replies
maciel.rafael1
27 Oct 2020, 17:50
( Updated at: 21 Dec 2023, 09:22 )
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 tokenIn 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 yoursprivate 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?
@maciel.rafael1
nguyendan81985
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
@nguyendan81985
AlgoCreators
08 Feb 2022, 09:10
How many requests can be sent per second?
Hello
thank you,The code works fine
But I want to be able to send a message every second
is this possible?
@AlgoCreators
amusleh
09 Feb 2022, 08:53
RE: How many requests can be sent per second?
meeting.chegini said:
Hello
thank you,The code works fine
But I want to be able to send a message every second
is this possible?
Hi,
You can start cBot/Indicator Timer with 1 second interval, then call the send method inside OnTimer method every one second.
@amusleh
AlgoCreators
14 Mar 2022, 05:16
how to send messages to telegram channel?
I want to send messages to a telegram channel
How can I do this?
Thanks
@AlgoCreators
amusleh
14 Mar 2022, 08:32
RE: how to send messages to telegram channel?
meeting.chegini said:
I want to send messages to a telegram channel
How can I do this?
Thanks
Hi,
You have to add the Telegram bot as an admin on your channel and then you will be able to get a chat ID with it that you can use it to send messages.
Here is a tutorial: Telegram · afhacker/ctrader-alert_popup Wiki (github.com)
@amusleh
AlgoCreators
14 Mar 2022, 23:46
RE: RE: how to send messages to telegram channel?
amusleh said:
meeting.chegini said:
I want to send messages to a telegram channel
How can I do this?
ThanksHi,
You have to add the Telegram bot as an admin on your channel and then you will be able to get a chat ID with it that you can use it to send messages.
Here is a tutorial: Telegram · afhacker/ctrader-alert_popup Wiki (github.com)
Thank you
I also noticed that i can write channel name instead of the chat ID
@AlgoCreators
paolo.panicali
28 Jul 2022, 17:05
Telegram notifications whenever a Trade is Opened or Closed and an update of opened positions every hour
Hello I coded this so that I know when my bot running on a VPS opens or closes a position and I get an update every hour. Do not try to send a message every second, 1) because telegram does not allow 2) keep calm.
You have to grant AccessRights.Internet otherwise the bot won't be able to send messages(just copy paste the code and when prompted you have to click Yes); also, set the timeframe to 5 minutes if you want the updates, otherwise you can set it to 1h timeframe and you will only get the position Opened and Closed message.
Of course you first have to create a Telegram bot and make him joining a group as previously explained from others.
// Telegram notifications on Open, Close position and Update every hour
// by paolo panicali july 2022
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.IO;
using System.Net;
using System.Text;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet)]
public class July19TelegramOpenCloseUpdate : Robot
{
string text, urlString, apiToken, chatId;
protected override void OnStart()
{
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
apiToken = "your bot token";
chatId = "chat id";
}
protected override void OnBar()
{
if (Bars.OpenTimes.LastValue.Minute == 5)
{
SendMessageToTelegram();
}
}
protected void SendMessageToTelegram()
{
foreach (var position in Positions)
{
text = "UPDATE CURRENTLY OPEN POSITION : ////--- BOTNAME: " + position.Label + "--- Symbol: " + position.SymbolName + "---- ENTRY: " + position.EntryPrice + "---- SL: " + position.StopLoss + "---- TP: " + position.TakeProfit + "---- TIME UTC: " + position.EntryTime + "---- PROFIT: " + position.Pips + " pips";
urlString = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}";
urlString = String.Format(urlString, apiToken, chatId, text);
WebRequest request = WebRequest.Create(urlString);
Stream rs = request.GetResponse().GetResponseStream();
System.Threading.Thread.Sleep(5000);
}
}
private void PositionsOnOpened(PositionOpenedEventArgs args)
{
Print("Position opened {0}", args.Position.SymbolName);
text = "NEW TRADE OPENED : ////--- BOTNAME: " + args.Position.Label + "--- Symbol: " + args.Position.SymbolName + "---- ENTRY: " + args.Position.EntryPrice + "---- SL: " + args.Position.StopLoss + "---- TP: " + args.Position.TakeProfit + "---- TIME UTC: " + args.Position.EntryTime + "---- PROFIT: " + args.Position.Pips + " pips";
urlString = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}";
urlString = String.Format(urlString, apiToken, chatId, text);
WebRequest request = WebRequest.Create(urlString);
Stream rs = request.GetResponse().GetResponseStream();
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var position = args.Position;
Print("Position closed with {0} profit", position.SymbolName);
text = "TRADE CLOSED : ////--- BOTNAME: " + args.Position.Label + "--- Symbol: " + args.Position.SymbolName + "---- ENTRY: " + args.Position.EntryPrice + "---- SL: " + args.Position.StopLoss + "---- TP: " + args.Position.TakeProfit + "---- TIME UTC: " + args.Position.EntryTime + "---- PROFIT: " + args.Position.Pips + " pips";
urlString = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}";
urlString = String.Format(urlString, apiToken, chatId, text);
WebRequest request = WebRequest.Create(urlString);
Stream rs = request.GetResponse().GetResponseStream();
}
}
}
Hope this will help, bye.
@paolo.panicali
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 tokenIn 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 yoursprivate 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
PanagiotisChar
01 Oct 2022, 16:58
Hi Alex,
There are easier ways to do this. Check this video.
Need Help? Join us on Telegram
@PanagiotisChar
alexempedrad
03 Oct 2022, 03:51
RE:
PanagiotisChar said:
Hi Alex,
There are easier ways to do this. Check this video.
Need Help? Join us on Telegram
Hi PanagiotisChar,
Thank you very much for the link. My bot works flawlessly now.
Alex
@alexempedrad
gnsmaciej
02 Nov 2023, 16:35
what could be blocking the sending of push alerts to telegram from cTrader, I have already added cTrader to the windows firewall, except that I have an old windows 8.1 any ideas what else to check maybe I need to install some add-on to Windows?
@gnsmaciej
PanagiotisCharalampous
03 Nov 2023, 06:25
( Updated at: 03 Nov 2023, 06:26 )
RE: [Instruction] Telegram notifications
gnsmaciej said:
what could be blocking the sending of push alerts to telegram from cTrader, I have already added cTrader to the windows firewall, except that I have an old windows 8.1 any ideas what else to check maybe I need to install some add-on to Windows?
Hi there,
Does your cBot have full access? You can use the tool below to check if there is any issue with your system. If the tool sends telegram messages, then the issue is somewhere in your code
https://clickalgo.com/telegram-setup-test
@PanagiotisCharalampous
keltrem
23 Oct 2020, 13:27
Cannot get this code to work, any suggestions?
@keltrem