using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Exceptions; using Telegram.Bot.Polling; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; namespace SignalsTest { public class Messenger { TelegramBotClient botClient; private static Messenger m_instance; public static Messenger instance { get { if (m_instance == null) { m_instance = new Messenger(); } return m_instance; } } public const string chatId = "@doralockscryptosignals"; public const string chatId_test = "@SignalTestPrivate"; public List messagesSchedule = new List(); public void ScheduleMessage(string text, string symbol="", string interval=""){ messagesSchedule.Add(new MessageQueueItem(){ symbol = symbol, interval=interval, text=text }); } public async void InitializeScheduler(){ string _chatId = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? chatId_test : chatId; while(true){ await Task.Delay(1500); if(messagesSchedule.Count <=0){ continue; } MessageQueueItem queueItem = messagesSchedule[0]; string message = queueItem.text; string filename = queueItem.filename; string symbol = queueItem.symbol; try{ if(symbol.Length < 2){ await botClient.SendTextMessageAsync(_chatId, message); }else{ FileStream fsSource = new FileStream(filename, FileMode.Open, FileAccess.Read); InputFile photo = InputFile.FromStream(fsSource); string url = $"https://www.tradingview.com/chart/?symbol=BINANCE%3A{symbol}"; // string formattedMessage = message.Replace($"`{filename}`", $"`{filename}`[Trading View]({url})"); string formattedMessage = message.Replace( $"`{symbol}`", $"{symbol}" ) + $"\n#{symbol}"; await botClient.SendPhotoAsync(_chatId, photo,caption: formattedMessage,parseMode: ParseMode.Html); } messagesSchedule.Remove(queueItem); }catch(Exception e){ Console.WriteLine($"Error occured while sending {message} to {filename}"); Console.WriteLine(e.ToString()); } await Task.Delay(1500); } } public Messenger() { botClient = new TelegramBotClient(Secrets.TELEGRAM_BOT_TOKEN); using CancellationTokenSource cts = new(); // StartReceiving does not block the caller thread. Receiving is done on the ThreadPool. ReceiverOptions receiverOptions = new() { AllowedUpdates = Array.Empty() // receive all update types except ChatMember related updates }; InitializeScheduler(); } } }