Beispiel #1
0
        private async void Observe(object sender, DoWorkEventArgs e)
        {
            try
            {
                var telegramBotClient = new TelegramBotClient(this.token);
                this.eventManager = new EventManager(telegramBotClient);
                await telegramBotClient.SetWebhookAsync(string.Empty);

                int offset = 0;

                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                    try
                    {
                        var updates = await telegramBotClient.GetUpdatesAsync(offset);

                        foreach (var update in updates)
                        {
                            this.eventManager.ResolveUpdates(update);
                            offset = update.Id + 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggingManager.LogEvent(string.Format(LogTemplates.ConnectionError, ex.Message), LogType.Error);
                        continue;
                    }
                }
            }
            catch (Telegram.Bot.Exceptions.ApiRequestException ex)
            {
                LoggingManager.LogEvent(string.Format(LogTemplates.ReceiveUpdateFail, ex.Message), LogType.Error);
            }
        }
Beispiel #2
0
        private void ResolveMessageUpdate(Update update)
        {
            switch (update.Message.Type)
            {
            case MessageType.TextMessage:
            {
                if (this.expectToken)
                {
                    this.ObserveMiner(update);
                    this.expectToken = false;
                    return;
                }

                if (update.Message.Text[0] == '/')
                {
                    LoggingManager.LogEvent("Trying to resolve received command...", LogType.Event);
                    var command = GetCommandType(update.Message.Text);
                    if (command == Command.Subscribe)
                    {
                        this.expectToken = true;
                        messageManager.SendTextMessage(update.Message, "Please, provide your wallet...");
                        return;
                    }

                    ResolveCommand(command, update);
                }
                else
                {
                    messageManager.Reply(update.Message);
                }
                break;
            }

            case MessageType.PhotoMessage:
            {
                //messageManager.ReplyProfile(update.Message);
                break;
            }

            default:
            {
                messageManager.SendTextMessage(update.Message, "I don't understand you! Try again in another way bro...");
                break;
            }
            }
        }
Beispiel #3
0
        public void Start()
        {
            LoggingManager.LogEvent(LogTemplates.BotInit, LogType.Event);
            try
            {
                this.backgroundWorker.DoWork += this.Observe;

                if (this.backgroundWorker.IsBusy != true)
                {
                    this.backgroundWorker.RunWorkerAsync();
                }
                LoggingManager.LogEvent(LogTemplates.BotInitSuccess, LogType.Success);
            }
            catch (Exception ex)
            {
                LoggingManager.LogEvent(string.Format(LogTemplates.BotInitFail, ex.Message), LogType.Error);
            }
        }
Beispiel #4
0
        private void ObserveMiner(Update update)
        {
            try
            {
                string miner = update.Message.Text;
                if (miner.Length != 35 && miner.Length != 40)
                {
                    LoggingManager.LogEvent(string.Format("Failed to subscribe cash ***{0}", miner.Substring((int)(miner.Length / 1.5)).ToLower()), LogType.Error);
                    messageManager.SendTextMessage(update.Message, "It seems like you provide invalid token...");
                    return;
                }

                var matches = subs.Where(i => i.TokenId == miner).ToList();

                if (matches.Count > 0)
                {
                    LoggingManager.LogEvent(string.Format("Already subscribed on cash ***{0}", miner.Substring((int)(miner.Length / 1.5)).ToLower()), LogType.Warning);
                    messageManager.SendTextMessage(update.Message, string.Format("Already subscribed on cash ***{0} with status:{1}", miner.Substring((int)(miner.Length / 1.5)).ToLower(), matches.FirstOrDefault().Observer.Status));
                    return;
                }


                Subscriber sub = new Subscriber();
                sub.TokenId     = miner;
                sub.PoolManager = new PoolManager(telegramBotClient, miner);
                sub.Update      = update;

                sub.Observer = new Task(async() =>
                {
                    bool decreasing = false;
                    LoggingManager.LogEvent(string.Format("Trying to subscribe cash ***{0}", miner.Substring((int)(miner.Length / 1.5)).ToLower()), LogType.Event);
                    LoggingManager.LogEvent(string.Format("Subscribed on cash *{0}", miner.Substring((int)(miner.Length - 4)).ToLower()), LogType.Success);
                    messageManager.SendTextMessage((sub.Update as Update).Message, string.Format("Subscribed on cash ***{0}", miner.Substring((int)(miner.Length / 1.5)).ToLower()));

                    while (true)
                    {
                        var poolStat   = await(sub.PoolManager as PoolManager).GetPoolStat();
                        var poolStatus = (sub.PoolManager as PoolManager).GetMinerStatus(poolStat);
                        var margin     = new String(' ', 23);
                        var border     = new String('-', 50);
                        var logText    = string.Format("{0}\n{1}Process recived data from *{2}" +
                                                       "\n{3}-current hashrate: {4:0.00}" +
                                                       "\n{5}-active workers: {6}",
                                                       border,
                                                       margin,
                                                       miner.Substring((int)(miner.Length - 4)).ToLower(),
                                                       margin,
                                                       poolStat.Data.CurrentHashrate,
                                                       margin,
                                                       poolStat.Data.ActiveWorkers);

                        if (poolStatus == MinerStatus.PowerDecreasing && !decreasing)
                        {
                            decreasing = true;
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(60000);
                            continue;
                        }
                        decreasing = false;
                        LoggingManager.LogEvent(logText, LogType.Event);
                        messageManager.NotifyMinerObserver((sub.Update as Update).Message, poolStatus, poolStat);

                        System.Threading.Thread.Sleep(60000);
                    }
                });

                subs.Add(sub);
                sub.Observer.Start();
            }
            catch
            {
                messageManager.NotifyMinerObserver(update.Message, MinerStatus.WrongToken);
            }
        }