76 lines
2.6 KiB
C#
76 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
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 Dictionary<string,string> messagesSchedule = new Dictionary<string, string>();
|
|
|
|
public void ScheduleMessage(string text, string filename=""){
|
|
messagesSchedule.Add(text,filename);
|
|
}
|
|
|
|
public async void InitializeScheduler(){
|
|
string _chatId = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? chatId_test : chatId;
|
|
|
|
while(true){
|
|
await Task.Delay(3500);
|
|
if(messagesSchedule.Count <=0){
|
|
continue;
|
|
}
|
|
|
|
string message = messagesSchedule.Keys.ElementAt(0);
|
|
string filename = messagesSchedule[message];
|
|
|
|
try{
|
|
if(filename.Length < 2){
|
|
await botClient.SendTextMessageAsync(_chatId, message);
|
|
}else{
|
|
FileStream fsSource = new FileStream($"{filename}.png", FileMode.Open, FileAccess.Read);
|
|
InputFile photo = InputFile.FromStream(fsSource);
|
|
await botClient.SendPhotoAsync(_chatId, photo,caption: message);
|
|
}
|
|
|
|
messagesSchedule.Remove(message);
|
|
}catch(Exception e){
|
|
Console.WriteLine($"Error occured while sending {message} to {filename}");
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
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<UpdateType>() // receive all update types except ChatMember related updates
|
|
};
|
|
|
|
InitializeScheduler();
|
|
}
|
|
}
|
|
}
|