示例#1
0
        public async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            Message message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.TextMessage)
            {
                return;
            }
            _chatid = message.Chat.Id;
            try
            {
                Logger.ColoredConsoleWrite(ConsoleColor.Red, "[TelegramAPI] Got Request from " + message.From.Username + " | " + message.Text);
                string username       = _botSettings.TelegramName;
                string telegramAnswer = string.Empty;

                if (username != message.From.Username)
                {
                    using (System.IO.Stream stream = new System.IO.MemoryStream())
                    {
                        PokemonGo.RocketAPI.Logic.Properties.Resources.norights.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        stream.Position = 0;
                        await _telegram.SendPhotoAsync(_chatid, new FileToSend("norights.jpg", stream), replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);
                    }
                    return;
                }

                // [0]-Commando; [1+]-Argument
                var              msgText = message.Text;
                string[]         textCMD = msgText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                TelegramUtilTask cmd     = getTask(textCMD[0]);
                switch (cmd)
                {
                case TelegramUtilTask.UNKNOWN:
                    telegramAnswer = string.Format("Usage:\r\n{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}\r\n{5}\r\n{6}\r\n{7}\r\n{8}\r\n{9}",
                                                   @"/stats - Get Current Stats",
                                                   @"/livestats - Enable/Disable Live Stats",
                                                   @"/information <topic> - Enable/Disable Information topics",
                                                   @"/top <HowMany?> - Outputs Top (?) Pokemons",
                                                   @"/transfer pokemon cp - transfer pokemons that matches with the given data",
                                                   @"/items  - Outputs Items List",
                                                   @"/drop item amount  - Throws the amount of items given",
                                                   @"/forceevolve - Forces Evolve",
                                                   @"/snipe pokemon latitude, longitude - snipe desired pokemon",
                                                   @"/stopbot - stop running bot");
                    break;

                case TelegramUtilTask.GET_STATS:
                    var ps = _client.Inventory.GetPlayerStats();

                    int    l             = ps.First().Level;
                    long   expneeded     = ((ps.First().NextLevelXp - ps.First().PrevLevelXp) - StringUtils.getExpDiff(ps.First().Level));
                    long   curexp        = ((ps.First().Experience - ps.First().PrevLevelXp) - StringUtils.getExpDiff(ps.First().Level));
                    double curexppercent = (Convert.ToDouble(curexp) / Convert.ToDouble(expneeded)) * 100;
                    string curloc        = _client.CurrentLatitude + "%20" + _client.CurrentLongitude;
                    curloc = curloc.Replace(",", ".");
                    string curlochtml = "https://www.google.com/maps/search/" + curloc + "/";
                    double shortenLng = Math.Round(_client.CurrentLongitude, 3);
                    double shortenLat = Math.Round(_client.CurrentLatitude, 3);
                    var    player     = _client.Player.GetPlayer();
                    telegramAnswer += "\nNickname: " + player.PlayerData.Username;
                    telegramAnswer += "\nLevel: " + ps.First().Level;
                    telegramAnswer += "\nEXP Needed: " + ((ps.First().NextLevelXp - ps.First().PrevLevelXp) - StringUtils.getExpDiff(ps.First().Level));
                    telegramAnswer += $"\nCurrent EXP: {curexp} ({Math.Round(curexppercent)}%)";
                    telegramAnswer += "\nEXP to Level up: " + ((ps.First().NextLevelXp) - (ps.First().Experience));
                    telegramAnswer += "\nKM walked: " + ps.First().KmWalked;
                    telegramAnswer += "\nPokeStops visited: " + ps.First().PokeStopVisits;
                    telegramAnswer += "\nStardust: " + player.PlayerData.Currencies.ToArray()[1].Amount;
                    telegramAnswer += "\nPokemons: " + (_client.Inventory.GetPokemons()).Count() + "/" + player.PlayerData.MaxPokemonStorage;
                    telegramAnswer += "\nItems: " + _client.Inventory.GetItems().Count() + " / " + player.PlayerData.MaxItemStorage;
                    telegramAnswer += "\nCurentLocation:\n" + curlochtml;
                    break;

                case TelegramUtilTask.GET_TOPLIST:
                    int shows = 10;
                    if (textCMD.Length > 1 && !int.TryParse(textCMD[1], out shows))
                    {
                        telegramAnswer += $"Error! This is not a Number: {textCMD[1]}\nNevermind...\n";
                        shows           = 10; //TryParse error will reset to 0
                    }
                    telegramAnswer += "Showing " + shows + " Pokemons...\nSorting...";
                    await _telegram.SendTextMessageAsync(_chatid, telegramAnswer, replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);

                    var myPokemons = _client.Inventory.GetPokemons();
                    myPokemons = myPokemons.OrderByDescending(x => x.Cp);
                    var PogoUsername = _client.Player.GetPlayer().PlayerData.Username;
                    telegramAnswer = $"Top {shows} Pokemons of {PogoUsername}:";

                    IEnumerable <PokemonData> topPokemon = myPokemons.Take(shows);
                    foreach (PokemonData pokemon in topPokemon)
                    {
                        telegramAnswer += string.Format("\n{0} |  CP: {1} ({2}% perfect)",
                                                        pokemon.PokemonId,
                                                        pokemon.Cp,
                                                        Math.Round(PokemonInfo.CalculatePokemonPerfection(pokemon), 2));
                    }
                    break;

                case TelegramUtilTask.SWITCH_LIVESTATS:
                    _livestats = SwitchAndGetAnswer(_livestats, out telegramAnswer, "Live Stats");
                    break;

                case TelegramUtilTask.SWITCH_INFORMATION:
                    //_informations = SwitchAndGetAnswer(_informations, out telegramAnswer, "Information");
                    Array topics = Enum.GetValues(typeof(TelegramUtilInformationTopics));
                    if (textCMD.Length > 1)
                    {
                        if (textCMD[1] == "all-enable")
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                telegramAnswer     += "Enabled information topic " + niceName + "\n";
                                _information[topic] = true;
                            }
                            break;
                        }
                        else if (textCMD[1] == "all-disable")
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                telegramAnswer     += "Disabled information topic " + niceName + "\n";
                                _information[topic] = false;
                            }
                            break;
                        }
                        else
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                if (textCMD[1].ToLower() == topic.ToString().ToLower())
                                {
                                    String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                    _information[topic] = !_information[topic];
                                    telegramAnswer      = (_information[topic] ? "En" : "Dis") + "abled information topic " + niceName + "\n";
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (TelegramUtilInformationTopics topic in topics)
                        {
                            String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                            telegramAnswer += " - " + niceName + "\n";
                            telegramAnswer += " -     " + _informationDescription[topic] + "\n";
                            telegramAnswer += " -     Currently " + (_information[topic] ? "enabled" : "disabled") + "\n";
                            telegramAnswer += "\n";
                        }

                        telegramAnswer += " - all-disable\n";
                        telegramAnswer += " -     " + TranslationHandler.GetString("telegram-disable-all", "Disable all topics") + "\n";
                        telegramAnswer += "\n";

                        telegramAnswer += " - all-enable\n";
                        telegramAnswer += " -     " + TranslationHandler.GetString("telegram-enable-all", "Enable all topics") + "\n";
                        telegramAnswer += "\n";
                        break;
                    }

                    break;

                case TelegramUtilTask.RUN_FORCEEVOLVE:
                    IEnumerable <PokemonData> pokemonToEvolve = Setout.GetPokemonToEvolve(true);
                    if (pokemonToEvolve.Count() > 3)
                    {
                        Setout.UseLuckyEgg(_client);
                    }
                    foreach (PokemonData pokemon in pokemonToEvolve)
                    {
                        if (_botSettings.pokemonsToEvolve.Contains(pokemon.PokemonId))
                        {
                            var evolvePokemonOutProto = _client.Inventory.EvolvePokemon((ulong)pokemon.Id);
                            if (evolvePokemonOutProto.Result == POGOProtos.Networking.Responses.EvolvePokemonResponse.Types.Result.Success)
                            {
                                await _telegram.SendTextMessageAsync(_chatid, $"Evolved {pokemon.PokemonId} successfully for {evolvePokemonOutProto.ExperienceAwarded}xp", replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);
                            }
                            else
                            {
                                await _telegram.SendTextMessageAsync(_chatid, $"Failed to evolve {pokemon.PokemonId}. EvolvePokemonOutProto.Result was {evolvePokemonOutProto.Result}, stopping evolving {pokemon.PokemonId}", replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);
                            }
                            RandomHelper.RandomSleep(2000);
                        }
                    }
                    telegramAnswer = "Done.";
                    break;

                case TelegramUtilTask.RUN_SNIPE:
                    telegramAnswer = ProcessSnipeCommand(msgText.Replace(textCMD[0], "").Trim());
                    break;

                case TelegramUtilTask.FORCE_STOP:
                    var secs = 1;
                    if (textCMD.Length > 1)
                    {
                        int.TryParse(textCMD[1].Trim(), out secs);
                    }
                    telegramAnswer = $"Stopping bot in {secs} seconds";
                    await _telegram.SendTextMessageAsync(_chatid, telegramAnswer, replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);

                    _telegram.StopReceiving();
                    RandomHelper.RandomSleep(secs * 1000);
                    Environment.Exit(0);
                    break;

                case TelegramUtilTask.TRANSFER_POKEMON:
                    telegramAnswer = ProcessTransferPokemon(msgText.Replace(textCMD[0], "").Trim());
                    break;

                case TelegramUtilTask.GET_ITEMSLIST:
                    telegramAnswer = ProcessGetItemList();
                    break;

                case TelegramUtilTask.DROP_ITEM:
                    telegramAnswer = ProcessDropItem(msgText.Replace(textCMD[0], "").Trim());
                    break;
                }

                await _telegram.SendTextMessageAsync(_chatid, telegramAnswer, replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.ToString());
                var apiRequestException = ex as ApiRequestException;
                if (apiRequestException != null)
                {
                    await _telegram.SendTextMessageAsync(_chatid, apiRequestException.Message, replyMarkup : new ReplyKeyboardHide()).ConfigureAwait(false);
                }
            }
        }
示例#2
0
        public async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            Message message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.TextMessage)
            {
                return;
            }
            _chatid = message.Chat.Id;
            try
            {
                Logger.ColoredConsoleWrite(ConsoleColor.Red, "[TelegramAPI] Got Request from " + message.From.Username + " | " + message.Text);
                string username       = _clientSettings.TelegramName;
                string telegramAnswer = string.Empty;

                if (username != message.From.Username)
                {
                    using (System.IO.Stream stream = new System.IO.MemoryStream())
                    {
                        Properties.Resources.norights.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        stream.Position = 0;
                        await _telegram.SendPhotoAsync(_chatid, new FileToSend("norights.jpg", stream), replyMarkup : new ReplyKeyboardHide());
                    }
                    return;
                }

                // [0]-Commando; [1+]-Argument
                string[]         textCMD = message.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                TelegramUtilTask cmd     = getTask(textCMD[0]);
                switch (cmd)
                {
                case TelegramUtilTask.UNKNOWN:
                    telegramAnswer = string.Format("Usage:\r\n{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}",
                                                   @"/stats - Get Current Stats",
                                                   @"/livestats - Enable/Disable Live Stats",
                                                   @"/information <topic> - Enable/Disable Information topics",
                                                   @"/top <HowMany?> - Outputs Top (?) Pokemons",
                                                   @"/forceevolve - Forces Evolve");
                    break;

                case TelegramUtilTask.GET_STATS:
                    var inventory = await _client.GetInventory();

                    var profil = await _client.GetProfile();

                    IEnumerable <PlayerStats> stats = inventory.InventoryDelta.InventoryItems
                                                      .Select(i => i.InventoryItemData.PlayerStats)
                                                      .Where(i => i != null);
                    foreach (PlayerStats ps in stats)
                    {
                        int l = ps.Level;

                        long   expneeded     = ((ps.NextLevelXp - ps.PrevLevelXp) - StringUtils.getExpDiff(ps.Level));
                        long   curexp        = ((ps.Experience - ps.PrevLevelXp) - StringUtils.getExpDiff(ps.Level));
                        double curexppercent = (Convert.ToDouble(curexp) / Convert.ToDouble(expneeded)) * 100;
                        string curloc        = _client.CurrentLat + "%20" + _client.CurrentLng;
                        curloc = curloc.Replace(",", ".");
                        string curlochtml = "https://www.google.de/maps/search/" + curloc + "/";
                        double shortenLng = Math.Round(_client.CurrentLng, 3);
                        double shortenLat = Math.Round(_client.CurrentLat, 3);
                        string pokemap    = shortenLat + ";" + shortenLng;
                        pokemap = pokemap.Replace(",", ".").Replace(";", ",");
                        string pokevishtml = "https://skiplagged.com/pokemon/#" + pokemap + ",14";
                        telegramAnswer +=
                            "\nNickname: " + profil.Profile.Username
                            + "\nLevel: " + ps.Level
                            + "\nEXP Needed: " + ((ps.NextLevelXp - ps.PrevLevelXp) - StringUtils.getExpDiff(ps.Level))
                            + $"\nCurrent EXP: {curexp} ({Math.Round(curexppercent)}%)"
                            + "\nEXP to Level up: " + ((ps.NextLevelXp) - (ps.Experience))
                            + "\nKM walked: " + ps.KmWalked
                            + "\nPokeStops visited: " + ps.PokeStopVisits
                            + "\nStardust: " + profil.Profile.Currency.ToArray()[1].Amount
                            + "\nPokemons: " + await _inventory.getPokemonCount() + "/" + profil.Profile.PokeStorage
                            + "\nItems: " + await _inventory.getInventoryCount() + " / " + profil.Profile.ItemStorage
                            + "\nCurentLocation:\n" + curlochtml
                            + "\nPokevision:\n" + pokevishtml;
                    }
                    break;

                case TelegramUtilTask.GET_TOPLIST:
                    int shows = 10;
                    if (textCMD.Length > 1 && !int.TryParse(textCMD[1], out shows))
                    {
                        telegramAnswer += $"Error! This is not a Number: {textCMD[1]}\nNevermind...\n";
                        shows           = 10; //TryParse error will reset to 0
                    }
                    telegramAnswer += "Showing " + shows + " Pokemons...\nSorting...";
                    await _telegram.SendTextMessageAsync(_chatid, telegramAnswer, replyMarkup : new ReplyKeyboardHide());

                    var myPokemons = await _inventory.GetPokemons();

                    myPokemons = myPokemons.OrderByDescending(x => x.Cp);
                    var profile = await _client.GetProfile();

                    telegramAnswer = $"Top {shows} Pokemons of {profile.Profile.Username}:";

                    IEnumerable <PokemonData> topPokemon = myPokemons.Take(shows);
                    foreach (PokemonData pokemon in topPokemon)
                    {
                        telegramAnswer += string.Format("\n{0} ({1})  |  CP: {2} ({3}% perfect)",
                                                        pokemon.PokemonId,
                                                        StringUtils.getPokemonNameGer(pokemon.PokemonId),
                                                        pokemon.Cp,
                                                        PokemonInfo.CalculatePokemonPerfection(pokemon));
                    }
                    break;

                case TelegramUtilTask.SWITCH_LIVESTATS:
                    _livestats = SwitchAndGetAnswer(_livestats, out telegramAnswer, "Live Stats");
                    break;

                case TelegramUtilTask.SWITCH_INFORMATION:
                    //_informations = SwitchAndGetAnswer(_informations, out telegramAnswer, "Information");
                    Array topics = Enum.GetValues(typeof(TelegramUtilInformationTopics));
                    if (textCMD.Length > 1)
                    {
                        if (textCMD[1] == "all-enable")
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                telegramAnswer     += "Enabled information topic " + niceName + "\n";
                                _information[topic] = true;
                            }
                            break;
                        }
                        else if (textCMD[1] == "all-disable")
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                telegramAnswer     += "Disabled information topic " + niceName + "\n";
                                _information[topic] = false;
                            }
                            break;
                        }
                        else
                        {
                            foreach (TelegramUtilInformationTopics topic in topics)
                            {
                                String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                                telegramAnswer      = (_information[topic] ? "Dis" : "En") + "abled information topic " + niceName + "\n";
                                _information[topic] = !_information[topic];
                                break;
                            }
                        }
                    }
                    else
                    {
                        foreach (TelegramUtilInformationTopics topic in topics)
                        {
                            String niceName = topic.ToString().Substring(0, 1).ToUpper() + topic.ToString().Substring(1).ToLower();
                            telegramAnswer += " - " + niceName + "\n";
                            telegramAnswer += " -     " + _informationDescription[topic] + "\n";
                            telegramAnswer += " -     Currently " + (_information[topic] ? "enabled" : "disabled") + "\n";
                            telegramAnswer += "\n";
                        }

                        telegramAnswer += " - all-disable\n";
                        telegramAnswer += " -     " + TranslationHandler.getString("telegram-disable-all", "Disable all topics") + "\n";
                        telegramAnswer += "\n";

                        telegramAnswer += " - all-enable\n";
                        telegramAnswer += " -     " + TranslationHandler.getString("telegram-enable-all", "Enable all topics") + "\n";
                        telegramAnswer += "\n";
                        break;
                    }

                    break;

                case TelegramUtilTask.RUN_FORCEEVOLVE:
                    IEnumerable <PokemonData> pokemonToEvolve = await _inventory.GetPokemonToEvolve(null);

                    if (pokemonToEvolve.Count() > 3)
                    {
                        await _inventory.UseLuckyEgg(_client);
                    }
                    foreach (PokemonData pokemon in pokemonToEvolve)
                    {
                        if (_clientSettings.pokemonsToEvolve.Contains(pokemon.PokemonId))
                        {
                            var evolvePokemonOutProto = await _client.EvolvePokemon((ulong)pokemon.Id);

                            if (evolvePokemonOutProto.Result == EvolvePokemonOut.Types.EvolvePokemonStatus.PokemonEvolvedSuccess)
                            {
                                await _telegram.SendTextMessageAsync(_chatid, $"Evolved {pokemon.PokemonId} successfully for {evolvePokemonOutProto.ExpAwarded}xp", replyMarkup : new ReplyKeyboardHide());
                            }
                            else
                            {
                                await _telegram.SendTextMessageAsync(_chatid, $"Failed to evolve {pokemon.PokemonId}. EvolvePokemonOutProto.Result was {evolvePokemonOutProto.Result}, stopping evolving {pokemon.PokemonId}", replyMarkup : new ReplyKeyboardHide());
                            }
                            await RandomHelper.RandomDelay(1000, 2000);
                        }
                    }
                    telegramAnswer = "Done.";
                    break;
                }

                await _telegram.SendTextMessageAsync(_chatid, telegramAnswer, replyMarkup : new ReplyKeyboardHide());
            }
            catch (Exception ex)
            {
                if (ex is ApiRequestException)
                {
                    await _telegram.SendTextMessageAsync(_chatid, (ex as ApiRequestException).Message, replyMarkup : new ReplyKeyboardHide());
                }
            }
        }