private async Task CheckForCsgoUpdate()
        {
            //Only check every 5 minutes (So Valve doesnt get mad at me)
            if (DateTime.Now.Minute % 5 == 0)
            {
                try
                {
                    var result = await _httpHandler.Get("https://blog.counter-strike.net/index.php/category/updates/");

                    var resultString = await result.Content.ReadAsStringAsync();

                    HtmlDocument pageDocument = new HtmlDocument();
                    pageDocument.LoadHtml(resultString);

                    var linkElement = pageDocument.DocumentNode.SelectSingleNode("//*[@id=\"post_container\"]/div[1]/h2/a");

                    var    newestCsUpdate = linkElement.InnerText;
                    string dateString     = Regex.Replace(newestCsUpdate, "[^(0-9/).]", "");
                    string lastCsUpdate   = (await _dapperDB.LoadFromDBStorage("lastCsgoUpdate")).value;

                    var link = linkElement.Attributes[0].Value;

                    if (lastCsUpdate != dateString)
                    {
                        await _dapperDB.SaveToDBStorage(new DBStorage()
                        {
                            key   = "lastCsgoUpdate",
                            value = dateString
                        });

                        var    firstUpdateLogs = pageDocument.DocumentNode.SelectSingleNode("//*[@id=\"post_container\"]/div[1]");
                        string releaseNotes    = "Release Notes:\n";
                        foreach (var element in firstUpdateLogs.ChildNodes)
                        {
                            if (element.Name == "p")
                            {
                                releaseNotes += element.InnerText + "\n";
                            }
                        }

                        releaseNotes = HttpUtility.HtmlDecode(releaseNotes);

                        var subs = await _dapperDB.GetAllCsgoUpdateSubscriber();

                        foreach (var sub in subs)
                        {
                            await _telegram.SendMessage(sub.chatId, "<b>New CS:GO release for " + dateString + "</b>\n" + link + "\n\nAll past updates: https://blog.counter-strike.net/index.php/category/updates/ \n\n" + releaseNotes);
                        }
                    }
                }
                catch (Exception e)
                {
                    _dapperDB.WriteEventLog("Init", "Error", "Could not check for CS updates - Exception: " + e.Message);
                    _telegram.SendErrorMessage("There is something wrong with the CSUpdate checker - FIX IT!");
                    _telegram.SendErrorMessage("Error was: " + e.Message);
                }
            }
        }
示例#2
0
        public async void checkForUpdates()
        {
            try
            {
                string versionFromFile = System.IO.File.ReadAllText(@"./VERSIONLOG/currentVersion.txt").Replace(" ", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty);
                string versionInDB     = (await _dapperDB.GetCurrentVersion()).Replace(" ", string.Empty);

                //Check if there is a new version
                if (versionFromFile != versionInDB)
                {
                    _dapperDB.UpdateVersion(versionFromFile);
                    string updateLog = System.IO.File.ReadAllText(@"./VERSIONLOG/currentUpdateLog.txt");

                    string updateMessage = "<b>v" + versionFromFile + "</b> \n------------------------------------------------\n" + updateLog + "\n------------------------------------------------\n If you don't want to receive this messages in the future just type /unsubupdates";

                    var subscribers = await _dapperDB.GetAllUpdateSubscriber();

                    foreach (var sub in subscribers)
                    {
                        await _telegram.SendMessage(sub.chatId, updateMessage);
                    }
                }
            }
            catch (Exception e)
            {
                _dapperDB.WriteEventLog("UpdateNotifyHandler", "ERROR", "There was an Error while checkingForUpdateNotifing" + e.Message, "checkForUpdates");
            }
        }
示例#3
0
        public async Task SetUserAufAbruf(string[] command, string userId)
        {
            if (ConvertDict.TlgrmID2DcID.ContainsKey(userId))
            {
                string   dcId;
                float    readyHours;
                DateTime readyEnds;

                var readyPlayers = await _dapperDB.GetReadyToPlayUsers();

                ConvertDict.TlgrmID2DcID.TryGetValue(userId, out dcId);

                if (command.Length >= 2 && float.TryParse(command[1], out readyHours))
                {
                    readyEnds = DateTime.UtcNow.AddHours(readyHours);
                }
                else
                {
                    readyEnds = DateTime.UtcNow.AddHours(1);
                }

                if (readyPlayers.Where(e => { return(e.tlgrmId == userId); }).Any())
                {
                    //this has to block so user is deleted before he is added again
                    _dapperDB.DeleteReadyPlayer(userId).Wait();
                }
                else
                {
                    //only adds role if user wasn't already set as ready
                    await _dc.AddRoleToUser(UInt64.Parse(dcId), _settings.dcAufAbrufRoleId);
                }

                _dapperDB.InsertReadyPlayer(new ReadyToPlayUsers()
                {
                    tlgrmId        = userId,
                    dcId           = dcId,
                    readyStartDate = DateTime.UtcNow,
                    readyEndDate   = readyEnds
                }).Wait();
                UpdateReadyToPlay().Wait();

                readyPlayers = await _dapperDB.GetReadyToPlayUsers();

                if (readyPlayers.Count == 4 || readyPlayers.Count == 5)
                {
                    await _telegram.SendMessage(_settings.tlgrmSbrGroupId, await GetCurrentReadyStateString());
                }
            }
        }
示例#4
0
        public async void HandleCommand(dynamic body)
        {
            List <string[]> commands = new List <string[]>();

            if (body.message.entities != null)
            {
                Entity[] entities    = Entity.convertDynamicToEntitiesArray(body.message.entities);
                string   messageText = body.message.text;
                commands = DecodeCommand(messageText, entities);
            }

            string userId      = body.message.from.id;
            string chatId      = body.message.chat.id;
            string chatType    = body.message.chat.type;
            string username    = body.message.from.username;
            string displayName = body.message.from.first_name + " " + body.message.from.last_name;

            int?replyMessageId = null;

            try
            {
                replyMessageId = body.message.reply_to_message.message_id;
            }
            catch
            {
                /*Fall through*/
            }


            foreach (string[] command in commands)
            {
                try
                {
                    switch (command[0])
                    {
                    case "/start":
                        SubscribeToUpdates(chatId);
                        await _telegram.SendMessage(chatId, "Heyho :)");

                        break;

                    case "/help":
                    case "/help@sbrcs_bot":
                        SendHelp(chatId);
                        break;

                    case "/ping":
                    case "/ping@sbrcs_bot":
                        if (chatType == "private")
                        {
                            await _telegram.SendMessage(chatId, "Pong");
                        }
                        break;

                    case "/unsubupdates":
                    case "/unsubupdates@sbrcs_bot":
                        UnsubscribeToUpdates(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from update log notification");

                        break;

                    case "/subfunfacts":
                    case "/subfunfacts@sbrcs_bot":
                        SubscribeToFunFacts(command, chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to FunFacts by Joseph Paul");

                        break;

                    case "/unsubfunfacts":
                    case "/unsubfunfacts@sbrcs_bot":
                        UnsubscribeFromFunFacts(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from FunFacts");

                        break;

                    case "/submemes":
                    case "/submemes@sbrcs_bot":
                        SubscribeToMemes(command, chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to RedditMemes");

                        break;

                    case "/unsubmemes":
                    case "/unsubmemes@sbrcs_bot":
                        UnsubscribeFromMemes(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from Memes");

                        break;

                    case "/subcsgoupdates":
                    case "/subcsgoupdates@sbrcs_bot":
                        SubscribeToCsgoUpdates(chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to CSGO Updates");

                        break;

                    case "/unsubcsgoupdates":
                    case "/unsubcsgoupdates@sbrcs_bot":
                        UnsubscribeFromCsgoUpdates(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from CSGO Updates");

                        break;

                    case "/subalmanmemes":
                    case "/subalmanmemes@sbrcs_bot":
                        SubscribeToDeutscheMemes(command, chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to Ich_Iel Memes");

                        break;

                    case "/unsubalmanmemes":
                    case "/unsubalmanmemes@sbrcs_bot":
                        UnsubscribeFromDeutscheMemes(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from Ich_Iel Memes");

                        break;

                    case "/iguana":
                    case "/iguana@sbrcs_bot":
                        SendLizardPic(chatId);
                        break;

                    case "/idea":
                    case "/idea@sbrcs_bot":
                        if (chatType == "private")
                        {
                            newIdea(command, chatId, username, displayName);
                            //This is already done in newIdea function: _telegramAPICommunicator.SendMessage(chatId, "Your idea was submitted");
                        }
                        else
                        {
                            await _telegram.SendMessage(chatId, "This command is only available in private conversations with the bot");
                        }
                        break;

                    case "/setcountdown":
                    case "/setcountdown@sbrcs_bot":
                        SetCountdown(command, chatId);
                        break;

                    case "/stopcountdown":
                    case "/stopcountdown@sbrcs_bot":
                        StopCountdown(chatId, replyMessageId);
                        break;

                    case "/getutctime":
                    case "/getutctime@sbrcs_bot":
                        await _telegram.SendMessage(chatId, DateTime.UtcNow.ToString("dd.MM.yyyy-HH:mm:ss"));

                        break;

                    case "/subducks":
                    case "/subducks@sbrcs_bot":
                        SubscribeToDucks(command, chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to awesome duck images");

                        break;

                    case "/unsubducks":
                    case "/unsubducks@sbrcs_bot":
                        UnsubscribeFromDucks(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from duck images");

                        break;

                    case "/subalpacas":
                    case "/subalpacas@sbrcs_bot":
                        SubscribeToAlpacas(command, chatId);
                        await _telegram.SendMessage(chatId, "Successfully subscribed to awesome alpaca images");

                        break;

                    case "/unsubalpacas":
                    case "/unsubalpacas@sbrcs_bot":
                        UnsubscribeFromAlpacas(chatId);
                        await _telegram.SendMessage(chatId, "Successfully unsubscribed from alpaca images");

                        break;

                    case "/aufabruf":
                    case "/aufabruf@sbrcs_bot":
                        await _readyToPlayHandler.SetUserAufAbruf(command, userId);

                        await _telegram.SendMessage(chatId, $"@{username} ist jetzt auf Abruf");

                        break;

                    case "/offline":
                    case "/offline@sbrcs_bot":
                        await _readyToPlayHandler.RemoveFromAufAbruf(userId);

                        await _telegram.SendMessage(chatId, $"@{username} ist nicht mehr auf Abruf");

                        break;

                    case "/whosready":
                    case "/whosready@sbrcs_bot":
                    case "/whoisready":
                    case "/whoisready@sbrcs_bot":
                        if (ConvertDict.TlgrmID2DcID.ContainsKey(userId))
                        {
                            var msg = await _readyToPlayHandler.GetCurrentReadyStateString();

                            await _telegram.SendMessage(chatId, msg);
                        }
                        break;

                    default:
                        /* Fall through */
                        break;
                    }
                }
                catch
                {
                    await _telegram.SendMessage(chatId, "There was an error while processing your command :(");
                }
            }
        }